interview questions
I remember a question from my previous interview: I don't remember the complete question, but I only remember the general requirements
- Basic question: write a method: pass parameter word, if pass parameter 3, generate a table similar to the following
1 | 1 | 1 |
---|---|---|
2 | 2 | 2 |
3 | 3 | 3 |
If you pass parameter 4, the following table will be generated
1 | 1 | 1 | 1 |
---|---|---|---|
2 | 2 | 2 | 2 |
3 | 3 | 3 | 3 |
4 | 4 | 4 | 4 |
and so on
My idea is:
num
In order to pass parameters, first initialize an array with a length of 1num
, and the array elements are filled with 1 ; initialize an array of a certain length:new Array(length)
, and the array is filled with values.fill()
;- Traverse the array, initialize each element as an array of length
num
, and fill the index value + 1 ; themap
traversal method is used here, whichmap
requiresreturn
function arr(num){
return new Array(num).fill(1).map((item,i)=>{
return new Array(num).fill(i+1);
});
}
复制代码
The results are as follows:
2. Extended question On the basis of 1, transform the content of the generated table as follows:
1 | 2 | 3 |
---|---|---|
6 | 5 | 4 |
7 | 8 | 9 |
1 | 2 | 3 | 4 |
---|---|---|---|
8 | 7 | 6 | 5 |
9 | 10 | 11 | 12 |
16 | 15 | 14 | 13 |
By analogy my thinking is:
-
On the basis of 1, initialize
num
the array of length, the array is filled with 1 -
map
method, re-initialize an array with a length of 1 for the elementsnum
in the array, the default is 1 , and the first is preset索引*num + 1
. It should be noted here that a value must be preset for the array, otherwise,new Array(num)
the resulting array is actuallyempty*num
empty. The traverser cannot traverse to get theindex
followitem
, as shown below -
Traverse the two-dimensional array again
map
, remove the first value, and add +1 to the remaining values -
Judging odd-numbered and even-numbered lines, starting from 0, odd-numbered lines are required
reverse()
, and odd-even numbers pass the对2取余
judgment -
return arry;
Return the concatenated array
function arr(num){
let array = new Array(num).fill(1).map((item,i)=>{
let arrItem = new Array(num).fill(1).fill(i*num + 1, 0 ,1);
let res = arrItem.map((val, key) => { return key === 0 ? val : key+arrItem[0];});
return i%2 === 1 ? res.reverse() : res;
});
return array;
}
复制代码
The results are as follows:
reflection
这次笔试题,写完后自己在console
里面试了下发现有问题,forEach
和 map
用错了,reduce
方法也用错了~
基础还是不够扎实,于是趁这次机会要来巩固下数组字符串切割等的各种方法了~
map
遍历
不会改变原来的数组,返回一个新数组
forEach
遍历
用于对数组中元素进行操作,不改变原数组的值,返回值为undefined
filter
过滤器 筛选数组
filter
用来筛选符合条件的值,返回一个新的数组,不会对原来数组进行修改
reduce
求数组总和
常见用法如下:
- 不额外传参:
reduce((sum,item,index)=>{return sum+item})
该方法表示从数组的第1个(即索引值index
为1)开始循环,sum
初始值为数组下标为0的值 - 传初始值:
reduce((sum,item,index)=>{return sum+item}, initData)
该方法表示,从数组第0个(即索引值index
为0)开始循环,sum
初始值为initData
fill
方法 给数组批量填充值
fill
用于给数组初始化赋值,常见用法有以下
array.fill(data)
: 只传一个参,表示给数组每个元素赋值data
array.fill(data, startIndex, endIndex)
: 表示给数组索引值为startIndex
到endIndex
之间的赋值data
array.fill(data, startIndex)
: 表示给数组从startIndex
索引值开始赋值data
splice
方法
array.splice(index, howmany ,item1……itemX)
删除或新增元素,会修改到原有数组,若删除,返回删除元素组成的数组
arr.splice(index, 0, item1……itemX)
表示从index
索引后插入item……itemX
数据
arr.splice(index, howmany)
表示从index
索引开始删除howmany
个值,返回被删除元素组成的数组
arr.splice(index, howmany, item1……itemX)
表示从index
索引开始删除howmany
个值,并插入item……itemX
数据,返回被删除元素组成的数组
split
方法
str.split()
, 指定分隔符将字符串分割成数组,分隔符传参可为字符串或者正则
slice
方法
slice(start[, end])
, start
end
为正整数或负整数,用于切割字符串,不改变原有字符串
substring
substring(start[, end])
, start
end
均为非负整数,用于切割字符串,不改变原有字符串
substr
substr(start[, length])
, start
可为负数,表示倒数第几个,用于切割字符串,不改变原有字符串
总结
1、会改变原有数组或字符串的方法
splice
:原有数组进行新增或删除操作,并且返回被删除元素组成的数组fill
: 原有数组预填值
2、不会改变原有数组或字符串的方法
slice(start[, end])
: 返回切割的字符串,start
和end
可为负数substring(start[, end])
: 返回切割的字符串,start
和end
不可为负数substr(start[, length])
: 返回切割的字符串,start
可为负数split()
: 指定分隔符分割成数组,分隔符可用字符串或正则filter
: 过滤器,返回符合要求的数组forEach
: 遍历数组,对每个元素进行操作,返回值为undefined
reduce
: 求和方法map
: 对原有数组进行操作,返回一个新数组
3、记一些别的常用的方法
findIndex
: 返回符合要求的第一个索引find
: 返回符合要求的第一个item
some
: 若有满足条件的值则返回true
,否则返回false
every
: 若所有值都满足条件返回true
,否则返回false
includes
: 数组中若包含某一项返回true
,否则返回false