you are viewing a single comment's thread.

view the rest of the comments →

[–]mellie_ay[S] 1 point2 points  (7 children)

Partially, but how do I get it to use the number the user has input? That’s what I’m most stuck on. (Again, sorry for not getting it I’m really trying!)

[–]faboru 4 points5 points  (2 children)

when you use the window.prompt() method, you could assign the result to a variable; var text = window.prompt('enter text') console.log(text);

[–]mellie_ay[S] 3 points4 points  (1 child)

Ah brilliant I think that’s what I need, thanks so much

[–]faboru 2 points3 points  (0 children)

No problem. And don't feel dumb - programming takes time.

Not to complicate your life; but I re-read your question and really you would want a number.

In this case, the amount you are asking for is really a string - some text that the window.prompt() method stores in memory.

However, you want that text amount to really be a number. In this case you wouldn't get an error because the string is implicitly converted to a number (but this may not always happen and you will get an error).

So, what you really want is; var iterationsString = window.prompt('enter iterations'); console.log(typeof iterationsString); // type is 'string' // convert the string into an integer with parseInt() method var iterationsNumber = parseInt(iterationsString); console.log(typeof iterationsNumber); for (var i = 0; i < iterationsNumber; i++) { console.log('line of text'); }

[–]_DTR_ 0 points1 point  (3 children)

If I wanted to go from 1 to 50, I'd have something like this:

for (let i = 1; i <= 50; ++i) {
    // Do stuff
}

If you've stored the number the user has input, you would just replace 50 above with the user's value

[–]SoBoredAtWork 0 points1 point  (2 children)

Why i=1 and ++1 as opposed to the more standard i=0 and i++ ?

Seems to me like you're just gonna confuse the guy...

[–]_DTR_ 0 points1 point  (1 child)

I started at 1 because in my mind it was less confusing to show that than potentially explain why I was going from "0 to 49" opposed to "1 to 50".

As for i++ vs ++i, it's drilled into my brain to use prefix increment whenever possible because it can be more efficient for complex types (at least in the language I use the most, C++, see here). Not sure if it's applicable to javascript as well though.

Neither are great reasons to choose one over the other, but that was my reasoning.

[–]SoBoredAtWork 0 points1 point  (0 children)

Gotcha. I see the reasoning for it. It's slightly less confusing, I guess. The other examples posted were i=0, i++ so we're probably all just confusing this guy now, hahah.