In: Computer Science
Mini JAVASCRIPT Algorithm Exercise
"Sum All In Array"
Objectives
Notes
Remember your data types? If the element is an integer (8), it should be added. If the element is a string with a number value ("8"), it should be added. If the element is not a number, or if it is a string with a non-number value, it should be ignored.
Use the following starter code below:
const sumAllInArray = arr => {
}
console.log(sumAllInArray(["-1", "a", 4, "-32", 8, "94"]))
//expected output: 73
# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue
# # Please consider providing a thumbs up to this question if it helps you. by doing that, you will help other students who are facing a similar issue.
//---------------OUTPUT-------------------------
73
//-------------------------------------------------------
const sumAllInArray = arr => {
let output=0;
//loops over the array length
for(let i=0;i<arr.length;i++){
// isNaN return false if a string can be converted to number
if(!isNaN(arr[i])){
//adding to the output
output=output+Number(arr[i])
}
}
return output;
}
console.log(sumAllInArray(["-1", "a", 4, "-32", 8, "94"]))