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 Introducing the Practice

Problem with workspace?

I'm using Chrome and in my workspace, when I view the console and type "node forLoops.js" it doesn't appear in the console the way it appears in the videoβ€”or how it should appear at all, for that matter. I changed nothing in the original, just added my code where the directions said I should. I know my code works because I tried it in jsfiddle (replacing the "print" with "document.write" throughout) and it all appears as it should - yay! But I cannot figure out why it won't appear correctly in the workspace. Instead, this is all I get:

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

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

Here's the original code with my answers added:

/* * * INSTRUCTIONS

To run this file, go to "View" in the menu, and select "Show console",
if it doesn't appear in the window below. At the prompt in the console,
type `node forLoops`.

You can clear the console by typing `clear` and pressing enter.

If your program is stuck in an infinite loop, and need to break out of the program,
you can do that by typing ctrl + C.

* * */

let text;

// 1. Write a for loop to build a string of numbers from 0 to 4, 
//    separated by spaces, and store the string in the variable `text`.
print('1st Loop:');
text = '';

// Write 1st loop here:
for(let i = 0; i <= 4; i += 1) {
  text += i + ' ';
}
print(text); // Should print `0 1 2 3 4 `.

// 2. Write a for 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:
for (let i = 1; i <= 5; i += 1) {
  text += i + ' ';
}
print(text); // Should print `1 2 3 4 5 `.

// 3. Write a for 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:
for (let i = 5; i > 0; i -= 1) {
  text += i + ' ';
}
print(text); // Should print `5 4 3 2 1 `.

// 4. Write a for 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:
for (let i = 5; i <= 51; i += 5) {
  text += i + ' ';
}    
print(text); // Should print `5 10 15 20 25 30 35 40 45 50 `.

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

// Write 5th loop here:
for (let i = 50; i > 0; i -= 5) {
  text += i + ' ';
}
print(text); // Should print `50 45 40 35 30 25 20 15 10 5 `.


// 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('');
  }
}

Update: Had similar issue with whileLoops.js, but doWhileLoops.js console is working as expected.

3 Answers

There is no print() function in JavaScript. (It's in Python.) You can use cosole.log() to print to the console.

Right, except that was the pre-written code that came with the practice. I didn't write that part of the code or change it at all, and there was nothing in the instructions that said to change it. And I'm not quite sure how that would affect only some of the loops while the others appear as expected. Maybe instructor Dave McFarland can shed some light?

Sorry, I missed the print function at the bottom. I pasted in your code to workspaces and ran the file and it worked perfectly. Can you see the command you entered in the console? If not, you're only seeing the last two because the other output is above. You should see the whole thing if you scroll up in the console or make the console window bigger by dragging up the window.