String.prototype.toCharArray = function () {
return Array.from({ length: this.length }).map((_it, index) => this.charAt(index));
}
String.prototype.reverse = function () {
return this.toCharArray().reverse().join('');
}
const str = '1234';
console.log(str.reverse())
VM59:8 4321
这里有个有趣的小学题目
智力题:ABCD乘以9等于DCBA请问A、B、C、D各等于多少?
可以根据上面方法通过算法得到。
for(let i = 1000; i < 9999 ; i++) {
const iString = "" + i; //toString()或者String()更优解
const jString = iString.reverse();
if(i * 9 == iString.reverse()) {
console.log(i);
}
}
VM289:5 1089
if (Array.find) {
Array.prototype.find = function (predicate) {
'use strict';
if (this == null) {
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
for (let i = 0, value; i < this.length; i++) {
value = this[i]
if (predicate.call(this, value)) {
return value
}
}
return undefined
}
}
续待…