• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nested for loops?

 
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*I have to use document.write, and prompts. Others have told me to get updated, but we get points taken off if we dont use them. As far a the loop goes I think we have to use just for loops, not for each or anything like that*

So for this assignment I have use arrays and loops. Basically what I want to do is ave the user enter the last name of the author for the book given, and display the out put like
Book: War And Peace Author: Twain

I got the output showing what I want..however it prints out way to much, I used the nested for loop so I could the authorName array. Since I populated it in the first loop, I wouldnt be able to use it in the second loop without nesting right? But all I need for output is just the 5 authors on separate lines with their book next to the name just once.

for example I get this for output

EDIT: I just entered 9 when the prompt came up just to see what it would give me, the 9's will be replaced with names
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code I have now
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no reason for the inner loop. Within a loop iteration, you deal with one item at a time. So there's no need to look at any others.

If you want to look through the array again, that would be outside the loop.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also make sure that the JavaScript console in the browser is open to show you the errors in your code.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats cool! I would of never thought you could go through two arrays like that. The book does not show that, only nested arrays and for loops. Also real quick is there a way to increment the index? For my next loop I put the author, and book title into a string, and assigned that to an array (instructions said to set it up like that) so it looks like "Tolstoy", "War and Peace", ect.... for the output I want

Tolstoy wrote War and peace, and the rest for the other author and books.

Below obviously gives me "Tolstoy wrote tolstoy" I tried to increment  the second author title, but that skipped over the book names. is there a way to get to the second index without having to do authorTitle[1] but even that would loop the second index 5 times.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two different arrays. If you only reference one of them, then of course you're going to get the same results. If you want the book name, why would you try to get it out of the array of author names?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not trying to get it out of those arrays.now I'm trying to get it out of authorTitle. I put the first two arrays content into a string, and assigned the string to author title.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Cody Biggs wrote:I'm not trying to get it out of those arrays.now I'm trying to get it out of authorTitle. I put the first two arrays content into a string, and assigned the string to author title.


Why?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is just what the instructions want us to do for the next loop
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Figured it out. I got to work by using
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I need to sort it which makes no sense...of course I would be using the .sort() method, but does not even give me close to what I want. I want to sort the authorTitle array, but that has everything in there and by the way the output looks on the directions I want it sorted by author. If I just use .sort() I get "A Christmas Carol wrote Dickens" instead of "Dickens wrote A Christmas Carol" How could I fix that?

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic