Can someone explain this practice question for me?

const values = [1, 10, 4, 3, 15, 3, 5, 2];

let total = 100
for (let i = values.length - 2; i >= 0; i -= 2) {
let a = values[i];
total /= a;
total += 10;
}
console.log(total);

Hi @blogwhiz50249

Welcome to the forum!
You can properly format code in posts by wrapping the code block in three backticks each:

// array to iterate through
const values = [1, 10, 4, 3, 15, 3, 5, 2];

// Initial value of total
let total = 100

// Sets the start value to the second item from the right
// Iterates until I is zero
// Counting down by two (omitting every other item in the array)
for (let i = values.length - 2; i >= 0; i -= 2) {

// assigns the current array item to the variable a
let a = values[i];

// devides the current amount of total by the current amount of a
// and reassigns total to the new amount
total /= a;

// adds 10 to the current amount of total
// and reassigns total to the new amount
total += 10;
}
console.log(total);

Hey Mirja and thank you so much for the response! You cleared it up well for me :smiley:

1 Like