While I was working on a problem, I encountered a situation that I was able to resolve, but I must admit, I don't fully comprehend how it works.
var array = [3, 6, 2, 56, 32, 5, 89, 32];
var largest = 0;
//Snippet of my code
for (var i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
I'm unsure about how my array assigns a value to my variable within the if statement.
Could you explain why this alternative version doesn't produce the desired result?
array[i] = largest;
As someone who is relatively new to programming, I believe grasping fundamental concepts, no matter how small, is crucial for aspiring programmers to develop their skills effectively.