3

I am working through a precourse challenge and I've tried a few different things to try and get this to work. But I am at a loss as to what I am doing wrong. This is the challenge:

Initialize a variable addThis to 0 and a variable sum to 0. Use a while loop to repeat a code block as long as addThis is less than 10. In the code block, add the value of addThis to sum, then increment addThis by 1. After the while loop runs, the value of sum should be the sum of the numbers 0 through 9.

The Challenge Error: expected 0 to equal 45

And my code:

let addThis = 0;
let sum = 0;
while (addThis < 10) { 
  addThis += sum;      
  addThis++
}
// Uncomment the line below to check your work!
console.log(sum);
2
  • 2
    You have to add to sum like this : sum += addThis;. That's all. Commented Jun 15, 2020 at 20:42
  • Thank you so much. Commented Jun 15, 2020 at 20:48

1 Answer 1

2
let addThis = 0;
let sum = 0;
while (addThis < 10) { 
    sum += addThis
    addThis++
}

console.log(sum);

You made a little mistake with your adding order x += y will assign x add y to x. Not the other way around.

Sign up to request clarification or add additional context in comments.

1 Comment

oh my gosh. I see. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.