1. js 实现一个函数,完成超过范围的两个大整数相加功能
主要思路是通过将数字转换为字符串,然后每个字符串在按位相加。
function bigNumberAdd ( number1, number2 ) {
let result = "" ,
carry = false ;
number1 = number1. split ( "" ) ;
number2 = number2. split ( "" ) ;
while ( number1. length || number2. length || carry) {
carry += ~ ~ number1. pop ( ) + ~ ~ number2. pop ( ) ;
result = carry % 10 + result;
carry = carry > 9 ;
}
return result;
}
2. js 如何实现数组扁平化?
function flattenArray ( array ) {
if ( ! Array. isArray ( array) ) return ;
let result = [ ] ;
result = array. reduce ( function ( pre, item ) {
return pre. concat ( Array. isArray ( item) ? flattenArray ( item) : item) ;
} , [ ] ) ;
return result;
}
function flattenArray ( array ) {
return array. toString ( ) . split ( "," ) . map ( function ( item ) {
return + item;
} )
}
3. js 如何实现数组去重?
function unique ( array ) {
if ( ! Array. isArray ( array) || array. length <= 1 ) return ;
var result = [ ] ;
array. forEach ( function ( item ) {
if ( result. indexOf ( item) === - 1 ) {
result. push ( item) ;
}
} )
return result;
}
function unique ( array ) {
if ( ! Array. isArray ( array) || array. length <= 1 ) return ;
return [ ... new Set ( array) ] ;
}
4. 如何求数组的最大值和最小值?
var arr = [ 6 , 4 , 1 , 8 , 2 , 11 , 23 ] ;
console. log ( Math. max . apply ( null , arr) )
5. 如何求两个数的最大公约数?
function getMaxCommonDivisor ( a, b ) {
if ( b === 0 ) return a;
return getMaxCommonDivisor ( b, a % b) ;
}
6. 如何求两个数的最小公倍数?
基本思想是采用将两个数相乘,然后除以它们的最大公约数
function getMinCommonMultiple ( a, b ) {
return a * b / getMaxCommonDivisor ( a, b) ;
}
7. 实现 IndexOf 方法?
function indexFun ( array, val ) {
if ( ! Array. isArray ( array) ) return ;
let length = array. length;
for ( let i = 0 ; i < length; i++ ) {
if ( array[ i] === val) {
return i;
}
}
return - 1 ;
}
8. 判断一个字符串是否为回文字符串?
function isPalindrome ( str ) {
let reg = / [\W_] / g ,
newStr = str. replace ( reg, "" ) . toLowerCase ( ) ,
reverseStr = newStr. split ( "" ) . reverse ( ) . join ( "" ) ;
return reverseStr === newStr;
}
9. 实现一个累加函数的功能比如 sum(1,2,3)(2).valueOf()
function sum ( ... args) {
let result = 0 ;
result = args. reduce ( function ( pre, item ) {
return pre + item;
} , 0 ) ;
let add = function ( ... args) {
result = args. reduce ( function ( pre, item ) {
return pre + item;
} , result) ;
return add;
} ;
add. valueOf = function ( ) {
console. log ( result) ;
}
return add;
}
10. 使用 reduce 方法实现 forEach、map、filter
function forEachUseReduce ( array, handler ) {
array. reduce ( function ( pre, item, index ) {
handler ( item, index) ;
} , null ) ;
}
function mapUseReduce ( array, handler ) {
let result = [ ] ;
array. reduce ( function ( pre, item, index ) {
let mapItem = handler ( item, index) ;
result. push ( mapItem) ;
} , null ) ;
return result;
}
function filterUseReduce ( array, handler ) {
let result = [ ] ;
array. reduce ( function ( pre, item, index ) {
if ( handler ( item, index) ) {
result. push ( item) ;
}
} , null ) ;
return result;
}
11. 设计一个简单的任务队列,要求分别在 1,3,4 秒后打印出 “1”, “2”, “3”
class Queue {
constructor ( ) {
this . queue = [ ] ;
this . time = 0 ;
}
addTask ( task, t ) {
this . time += t;
this . queue. push ( [ task, this . time] ) ;
return this ;
}
start ( ) {
this . queue. forEach ( item => {
setTimeout ( ( ) => {
item[ 0 ] ( ) ;
} , item[ 1 ] ) ;
} )
}
}
12. 怎么查找一篇英文文章中出现频率最高的单词?
function findMostWord ( article ) {
if ( ! article) return ;
article = article. trim ( ) . toLowerCase ( ) ;
let wordList = article. match ( / [a-z]+ / g ) ,
visited = [ ] ,
maxNum = 0 ,
maxWord = "" ;
article = " " + wordList. join ( " " ) + " " ;
wordList. forEach ( function ( item ) {
if ( visited. indexOf ( item) < 0 ) {
let word = new RegExp ( " " + item + " " , "g" ) ,
num = article. match ( word) . length;
if ( num > maxNum) {
maxNum = num;
maxWord = item;
}
}
} ) ;
return maxWord + " " + maxNum;
}