Can someone explain this one line


#1

In particular the parts in bold

 for (var j = i; **j < (myName.length + i)**; j++)

I had been incorrectly using

 for (var j = i; **j < myName.length; j++**)

Edit - it looks like I cant bold code (makes sense) so instead look out for the bits in between ** ** in both code snippets


#2

Well, this is what I would do:

for (var j = 0; j < myName.length; j++)

Because I do not know what i means in your code, I would not add it in my for loop. I do not know why you put your i in there, so I will just explain using mine. Okay? Now... I'll explain:

First, you put a for loop. In the for loop, there are three parts, separated by semi colons:

- defining a variable
- showing a greater, less, or equal to with another variable/object
- what the variable should do until the statement(above) is true

What you do first is defining the variable. What is the variable? In this case, it is j. j is equal to 0 (defining it).

Now, you must show the variable and compare it with another variable/object that has already been declared and defined. In this case, j < myName.length is this part. It shows that j should be smaller than myName.length. The variable will go to the closest amount it can that will agree with this.

How? Why, showing how to do it (part 3)! This part is j++, in my code. This tells the variable j to go up one at a time until it can go closest to part 2, which is that j < myName.length.

This is what the for loop explains. I have explained it as best as I can, and I hope it helps you to understand it! :smiley:


#3

The idea is the following your outer for loop goes through your text and currently you are at position i in your text. Now the letter at text[i] is the first letter of your name so you get in the second loop that's where the var j=i comes from. Now as + is commutativ (a+b = b+a) let's write it like this (I guess it makes more sense and I've no idea why the exercise does it differently):

j < i+myName.length

Although the change is really small you should see the idea. You start from i and go to i+myName.length so what you're actually doing is looping myName.length times starting from the current letter where you found the first letter of your name.

Using just j < myName.length might not work because i could already by bigger than myName.length and this might lead to doing nothing or searching at the wrong position.


#4

Why does it make sense? Just curious...lotsa sites don't allow any styling of code and I've never understood why not...a technical reason involved, I'm sure, having to do with the forum-comment text-editor, but still....


#5

It's part of this lesson, 5/7, using two for-loops at once, having the two interact (well, specifically, one "building on" the other, the latter on the former)....

Which is also why it's critical to have the "+i" (as in, "myName.length + i" [wish there was a way to style code!!] and not just "myName.length")....


#6

I think it's just the convention, that's all (i.e., coding style). But yeah, writing it the way you did does make it more explicit, the idea; makes what's going on more clear.


#7

Well have you tried to write 3*4*5 ouside of code or 2**3+4**8 in languages that support ** as power operator? I guess they strictly want to distingush design and function. Just recently saw a post that got completely messed up because the _ in a variable name was interpreted as some style command.

Test:
Maybe like this but only works for inline code...
for (var j = i; j < (myName.length + i); j++)


#8

I still don't see why forum-comment text-editors couldn't be programmed to distinguish intent the way spreadsheets are: if you enter in a date in a spreadsheet, you could instruct the spreadsheet application to treat this date as text instead of as a set of numbers...similarly, why can't forum-comment text-editor functionality be programmed to allow for the mere styling of code, as opposed to treating all code as something to be carried out??

Sure that's technically more difficult but not impossible....


#9

You currently have 12 basic option to format your text and not even 20% of the user even realize that there is a code formatting function, do you think this will get better when you increase the functionality to such a level it could compete with professional tools? I guess it's a question of efficiency, currently it's probably light weight but has the basic stuff you need, doesn't it?


#10

Sorry, my mistake: bold does work on code (code can be bolded), contrary to the OP's assertion...I had assumed that claim true because I do know that my customary way of distinguishing code, by placing it within , does not work here in the Academy's forums.

But thanks for the reminder that others ways do exist!