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 The Solution

I don't get whats going on here, keep getting errors

Here is the code challenge below, and this is the error I am getting:

ode whileLoops.js
/home/treehouse/workspace/whileLoops.js:110
function print(text) s
^

SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)

here is the code

let i;
let text;

// from 0 to 4, 



print('1st Loop:');
text = '';

// Write 1st loop here:
i=0;
while (i<=4) {


text += i + '  ';

  i+= 1  ;
}

print(text); // Should print `0 1 2 3 4 `.

// 2. Write a while loop to build a string of numbers from 1 to 5, 
//    separated by spaces, and store the string in the variable `text`.
print('2nd Loop:');
text = '';

// Write 2nd loop here:

i=1;

while (i<=5) {


text += i + '  ';

  i+= 1  ; 
}


print(text); // Should print `1 2 3 4 5 `.

// 3. Write a while loop to build a string of numbers from 5 to 1, 
//    separated by spaces, and store the string in the variable `text`.
print('3rd Loop:');
text = '';



// Write 3rd loop here:


  i=5;

while (i<=0) {


text += i + '  ';

  i-= 1  ;
}

print(text); // Should print `5 4 3 2 1 `.

// 4. Write a while loop to build a string of numbers from 5 to 50--by 5's.
//    The numbers should be separated by spaces, and stored in the variable `text`.
print('4th Loop:');
text = '';

// Write 4th loop here:

  i=5;

while (i<=50) {


text += i + '  ';

  i+= 5;
}

print(text); // Should print `5 10 15 20 25 30 35 40 45 50 `.

// 5. BONUS CHALLENGE: Write a while loop that builds a string of random integers
// between 0 and 9. Stop building the string when the number 8 comes up.
// Be sure that 8 does print as the last character. The resulting string 
// will be a random length.
print('5th Loop:');
text = '';

// Write 5th loop here:
i= Math.floor(Math.random() * 10) ;

while (i !== 8 ){

  text += i + '   ';

 i = Math.floor(Math.random() * 10) ;

  text += i;
}



print(text); // Should print something like `4 7 2 9 8 `, or `9 0 8 ` or `8 `.


// Feel free to ignore this print function. It just formats the output a bit.
function print(text) s
  console.log(text);
  if (!text.endsWith(':')) {
    console.log('');
          }
        }

1 Answer

Hey so I managed to figure it out lol, I was missing an '{' at the bottom of the code for the function & there was an 's' that should not have been there..

here is the solution, going to keep it here in case it helps anyone :)

let i;
let text;

// from 0 to 4, 



print('1st Loop:');
text = '';

// Write 1st loop here:
i=0;
while (i<=4) {


text += i + '  ';

  i+= 1  ;
}

print(text); // Should print `0 1 2 3 4 `.

// 2. Write a while loop to build a string of numbers from 1 to 5, 
//    separated by spaces, and store the string in the variable `text`.
print('2nd Loop:');
text = '';

// Write 2nd loop here:

i=1;

while (i<=5) {


text += i + '  ';

  i+= 1  ; 
}


print(text); // Should print `1 2 3 4 5 `.

// 3. Write a while loop to build a string of numbers from 5 to 1, 
//    separated by spaces, and store the string in the variable `text`.
print('3rd Loop:');
text = '';



// Write 3rd loop here:


  i= 5;

while (i>=0) {


text += i + ' node  ';

  i -= 1  ;
}

print(text); // Should print `5 4 3 2 1 `.

// 4. Write a while loop to build a string of numbers from 5 to 50--by 5's.
//    The numbers should be separated by spaces, and stored in the variable `text`.
print('4th Loop:');
text = '';

// Write 4th loop here:

  i=5;

while (i<=50) {


text += i + '  ';

  i+= 5;
}

print(text); // Should print `5 10 15 20 25 30 35 40 45 50 `.

// 5. BONUS CHALLENGE: Write a while loop that builds a string of random integers
// between 0 and 9. Stop building the string when the number 8 comes up.
// Be sure that 8 does print as the last character. The resulting string 
// will be a random length.
print('5th Loop:');
text = '';

// Write 5th loop here:
i= Math.floor(Math.random() * 10) ;

while (i !== 8 ){

  text += i + '   ';

 i = Math.floor(Math.random() * 10) ;

  text += i;
}



print(text); // Should print something like `4 7 2 9 8 `, or `9 0 8 ` or `8 `.


// Feel free to ignore this print function. It just formats the output a bit.
function print(text) { 
  console.log(text);
  if (!text.endsWith(':')) {
    console.log('');
          }
        }

and here is the result

1st Loop:
0 1 2 3 4

2nd Loop:
1 2 3 4 5

3rd Loop:
5 4 3 2 1 0

4th Loop:
5 10 15 20 25 30 35 40 45 50

5th Loop:
1 33 33 33 8