• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop and array  RSS feed

 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
second question: I have to put letters A-Z into an array using unicode, and a for loop. I have loaded up the unicode into variable res, but Im honestly blank on how to use that to populate the array with the for loop. Then once loaded the array is going to be printed out the the html file, but I have not got that far yet obviously. This is what I came up with...but im not sure
JavaScript:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite understanding what you are trying to do.

You create a string. Then, what is you want to do to with string with respect to the array?

What's your end goal?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I want to populate the alphabet array using a for loop, and the String.fromCharCode method. So I added the numbers to the string method which is suppose to change them from numbers to A-Z when printed. So in my mind what I have for the loop right now is that it will loop through the string, and add them to the array...but I dont know if thats right.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any reason for all this hoopla? Why not just:
?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish....but the assignment directions want it done that way
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what are the constraints of the assignment? Otherwise, we're guessing.

 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is all that is required for the array part


b. Create an array named "alphabet".
c. Populate the alphabet array using a for loop with the capital letters A – Z. (Array will have
26 members.). See hint.


To populate the alphabet array, use a for loop along with the String.fromCharCode() method. It will have 26
members when you complete it
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, thinking on that:

Does your line 2 make sense?

Answer: no, because you're creating a string with all the letters in it. Not what the assignment asks for.

Does it make sense to have to list all the character codes from 65 onward?

No. You've got a for loop that can count up, so why do it by hand?

Try again and let's see where you end up.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soooo...I could make a loop that start at 65, and goes until 90. Those would be added to the array, but I honestly cant think on where to use that string method....
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66260
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You figured out how to get the codes. Well done!

But with regards to the string method, your assignment says to add letters, not codes, to the array. So how to convert the codes one at a time?
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm so obviously the string method would have to go inside the loop, and then I guess find some way to use the index of the loop? If I remember correctly you use the index like this [i]?
Sooo my guess right now is  String.fromCharCode(i)? Or if I need the brackets String.fromCharCode([i])???
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or would it be like below? So the codes get added to the array, and then using the index of the array the method converts those to letters?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!