1. 程式人生 > >第一章JS複習

第一章JS複習

//js   forEach  every  same  reduce不產生新陣列
function square(num){
	console.log(num,num*num);
}

let nums=[1,2,3,4,5,6,7,8,9];
nums.forEach(square);
console.log('nums:',nums);

function isEven(num){
	return num %2==0;
}
let nums=[2,4,6,8,9];
// let even=nums.every(isEven);
let even=nums.some(isEven);
if (even) {
	console.log("all numbers are even");
}else{
	console.log("not all numbers are even");
}

function add(runningTotal,currentValue){
	return runningTotal+currentValue;
}

let nums=[1,2,3,4,5,6,7,8,9,10];
let sum=nums.reduce(add);
console.log("add:",sum);

function concat(accumulatedString,item){
	return accumulatedString+item;
}
let words=['the ','quick ','brown ','fox '];
let sentence=words.reduce(concat,'haung ');
console.log(sentence);