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 trial

JavaScript

Philip Simmons
PLUS
Philip Simmons
Courses Plus Student 2,256 Points

I need help on this Challange: Terminate A Loop

The for loop in script.js runs as many times as the length value of the string assigned to the message variable. It logs the current value of i to the console, and the rest of the program continues when the loop completes.

Add the statement that immediately terminates the for loop if the value of i is equal to message / 2 (half the length value).

let message = "supercalifragilisticexpialidocious"; message = message.length;

for ( let i = 1; i < message; i++ ) { if ( i === message / 2 ) { console.log('The loop has terminated...'); } console.log(Logging the number ${i}); }

console.log('The program continues...');

I'm sure where to add "break" to solve it. All of my answers come back wrong based on the hints I have used to help solve the challenge.

4 Answers

Logging the number 1
Logging the number 2
Logging the number 3
Logging the number 4
Logging the number 5
Logging the number 6
Logging the number 7
Logging the number 8
Logging the number 9
Logging the number 10
Logging the number 11
Logging the number 12
Logging the number 13
Logging the number 14
Logging the number 15
Logging the number 16
The loop has terminated...
Logging the number 17
Logging the number 18
Logging the number 19
Logging the number 20
Logging the number 21
Logging the number 22
Logging the number 23
Logging the number 24
Logging the number 25
Logging the number 26
Logging the number 27
Logging the number 28
Logging the number 29
Logging the number 30
Logging the number 31
Logging the number 32
Logging the number 33
The program continues...

after adding the template literals `` to the last console.log this what returned i don't see any error in your program

How did you solve it. I tried to follow your code. I'm getting it wrong

I need help with code let message = "supercalifragilisticexpialidocious"; message = message.length;

for ( let i = 1; i <= message; i++ ) { if ( i === message / 2 ) { break; console.log('The loop has terminated...'); } console.log(Logging the number ${i}); }

console.log('The program continues...');