Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMark Carrel
9,873 Pointshow do i make a while loop
i forgot the syntax please send help
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0
// Enter your code below
while counter < numbers.length
1 Answer
jcorum
71,830 PointsHere it is, step-by-step:
//start with **while() {**, then put a boolean expression inside the parentheses
//the loop will run as long as the condition is true
while(counter < numbers.count) {
//add up the numbers in the numbers array
sum += numbers[counter]
//increment the loop counter
counter++
//close the code block
}
In this challenge they want the loop to sum up the numbers in the array, so the condition needs to be written so the the loop will run once for each number. Making the condition counter < numbers.count will ensure that the loop gets numbers[0], numbers[1], ..., numbers[6], and then stop.