I understand that a loop repeats itself for a specified number of times, but how do I make so that it reads through the entire array and returns a certain value? I'm trying to return the largest value for two test questions, but the only result I'm getting is the first value of the array.
function question1(numberArray) {
for (let i = 0; i < numberArray.length; i = i + 1) {
number = numberArray[i];
return number;
}
}
function testQuestion1() {
let testArray = [10, 45, 33, 67, 433, 33];
let answer1 = question1(testArray);
if(question1(testArray) == 433) {
console.log("Question 1:Found the largest");
} else {
console.log("Question1: For array ", testArray, " returned ", answer1, "Which is not the largest");
}
let testArray2 = [10, -900, 3000, 22, 33, 67, 433, 33];
let answer2 = question1(testArray);
if(question1(testArray2) == 3000) {
console.log("Question 1: Found the largest");
} else {
console.log("Question 1: For array ", testArray2, " returned ", answer2, "Which is not the largest");
}
}