Way one.
function square(arr) {
let res=[];
arr.forEach(ele=>res.push(ele*ele));
return res;
}
Way Two.利用map
function square(arr) {
return arr.map(ele=>ele*ele);
}
Way one.
function square(arr) {
let res=[];
arr.forEach(ele=>res.push(ele*ele));
return res;
}
Way Two.利用map
function square(arr) {
return arr.map(ele=>ele*ele);
}