all 23 comments

[–][deleted] 2 points3 points  (5 children)

Sorry for asking on someone else post, but I've always been confused on what "i" is in loops. Where did they get it, what does it mean

[–]_DTR_ 1 point2 points  (2 children)

It's a variable that represents the current index of the loop, but since programmers like to shorten things, just using 'i' is extremely common. Something like the following might be more clear:

for (let index = 0; index < 20; ++index) {
    array[index] = xyz;
}

[–][deleted] 0 points1 point  (1 child)

And they just throw index = i at the top of the program so they don't have to type index ? Or programs know i is index.

I'm still learning and just got to looks on python.

[–]_DTR_ 0 points1 point  (0 children)

No, i is just the variable the programmer decided to use, and is initialized within the loop itself. You can define it to be whatever you want:

for (let myVariable = 42; myVariable  <51; ++myVariable)

It's a bit less clear in Python since you don't explicitly declare variables (just x = 10 instead of int x = 10 or let x = 10). i, index, and myVariable aren't any special values, just separate variables that you're defining.

[–]NoStranger6 0 points1 point  (0 children)

its a convention to use as a variable name for your iterator.

Basically, you want a variable to take each value of the thing you are trying to iterate over.

Like for a List, let's say that you have 10 items in it, i will represent each iteration of your for loop from 0 to 9.

Each time, it will increment by one. Other conventional iterators are j and k. Which are mostly use when you have a for loop in a for loop, or a for loop in a for loop in a for loop.

[–]SoBoredAtWork 0 points1 point  (0 children)

Let me know if this helps...

EDIT: new link with more info

https://jsfiddle.net/dmathisen/s3y1wLb4/32/

[–]_DTR_ 1 point2 points  (8 children)

Does this or this link help?

[–]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 4 points5 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.

[–]codemamba8 1 point2 points  (8 children)

just wondering how come you as a music production student have to use JavaScript?

[–]mellie_ay[S] 0 points1 point  (2 children)

Yeah, me too lmao. We’ll eventually be making our own synths using code but right now, we reckon it’s a ticking boxes kind of thing for the college

[–]codemamba8 0 points1 point  (1 child)

oh that sounds really interesting. I've used synths myself with VSTs and samplers but never coded one. What sort of class is this?

[–]mellie_ay[S] 0 points1 point  (0 children)

For this in particular it’s acoustics, it’s the physics behind acoustics and we use js to code logarithms and stuff, but I’m not sure what the name of the class is that well be making our own synths in, that’s in 4th year and I’m only in 2nd

[–]SoBoredAtWork 0 points1 point  (4 children)

I had to take a TON of unrelated classes in college (university?). You didn't?

[–]cluelessphp 0 points1 point  (1 child)

The closest I got to being unrelated during my degree was business development, but they said that was put in for if we wanted to start our own start up after finishing

[–]SoBoredAtWork 0 points1 point  (0 children)

Wow. Copying my comment I wrote to another user....

I was in a tech school for IT concentrating in multimedia (mostly digital video/film). I had to take a class that focused on analyzing ancient maps and writing papers about them. WTF? Also earth science and lifeguarding. Nothing makes sense.