I need to tell the program to search an array(that’s meant to be only numbers) if it contains any element that’s a string.
Also, the array is made up of the arguments to a function. Can someone please help? I’ve been trying to figure this out for at least an hour! This is what i did so far:
JavaScript
x
9
1
const sumAll = function(…args){
2
const newArray = Array.from(args)
3
for(let i = 0; i < newArray.length; i++){
4
if(newArray[i] === NaN){
5
return “ERROR”
6
}
7
}
8
}
9
Advertisement
Answer
You’re looking for the function isNaN
JavaScript
1
12
12
1
const sumAll = function(args){
2
const newArray = Array.from(args)
3
for(let i = 0; i < newArray.length; i++){
4
if(isNaN(newArray[i])){
5
return "ERROR"
6
}
7
}
8
}
9
10
console.log(sumAll(1,2,3)) // no output - undefined
11
12
console.log(sumAll(1,"two",3)) // error