Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a for Loop

for loop

I'm stuck on this challenge task. It keeps giving me error. But if I copy the code to an external editor, it runs fine. Am I doing something wrong or is there a bug in the challenge?

var number = " ";

for (var i = 4; i <= 156; i += 1) { number += " " + i + " "; } console.log (number);

script.js
var number = " ";

for (var i = 4; i <= 156; i += 1) {
  number += " " + i + " ";
}
console.log (number);

Here's the result I get when running the code from an external editor. The result is what the challenge is asking.

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

1 Answer

Thomas Fildes
Thomas Fildes
16,201 Points

Hi Ihengsiho,

You are over complicating the for loop. Here is the code I used to pass the challenge:

for (var i = 4; i <= 156; i++) {
  console.log(i);
}

This loop logs all numbers starting from 4 and ending with 156 because it loops through the variable "i" and increments by 1 everytime it passes the condition of being less than or equal to 156.

Hope this helps! Happy Coding!!!

Thanks Thomas. Appreciate your help.

The output of your code is practically the same as mine. Don't understand why it didn't let me pass.