• 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

Adding a name to an array in JavaScript

 
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
So I have been messing around with arrays, and array methods. I know that java you can add stuff to arrays just using .add, like list.add(15); I was wondering if JavaScript has something like that? The closest thing I have found is the push() method, but that adds to the end of the array. I have looped through an array finding the index of values, but now I want add names in a loop to an array...and then that last part is just to print the out, so I know they were added

Something like

 
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
Perhaps you can find what you need by looking through the documentation for Array.
 
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
So I am stuck again...I want to loop through the array printing out the author last name and the book they wrote. I got the could for just printing those out of the array, but the instructions there has to be a loop used. Which is where I am stuck..with the loops. I have 5 loops I need to do with this, but I will post only the first two as to not overwhelm

First Problem: I want to use a loop asking the user for the authors last name of each book, and I have to use the length of the array in the loop according to the instructions. My confusion, and problem is the way its set up is its going to loop through each question 5 times because thats the length of my array right? I used the break because that stops after the last question, but thats not even using a loop...So whats the point of a loop here if im really not even looping? Or I guess whats a to loop through that once without using break?



Second Problem: Again I want to use a loop to display this output, and I used break because that stopped after the first time....instead of looping 5 times because thats the length of my array. Again whats the point of a loop here if im just going through it once?



 
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
First problem: you misunderstand the nature of loops. The purpose of the loops is so that you can deal with each item in the array individually -- not all at once.
 
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
So instead of 5 prompts I just need one? That way it loops through 5 times getting me all the names that I need, but I guess my question is how am I going to do that? Because each prompt has to have the book name in it..which to me sounds like I would need the 5 prompts.

The only other thing I can think of would be something like. This way the question gets asked 5 times, and each answer gets into the array, but again I need to change the book name for the second time it loops around. How do I do that without adding another prompt?



 
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
Unless somehow I can use the index of the bookNames array like below, and somehow increment that index with the loop so it uses all 5 book names if thats possible?

[code]
var firstAuthor = prompt("What is the author's last name for the book:","" + loopNames[0]) ;
[/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
WAIT!!! I think I got it, and im sorry for all these posts after posts but I did not see a edit button to add all these together. So it would actually be this??
 
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
Better...
 
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
What would be the"best" way to do it?
 
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
But still problems. Think about where the authorNames array should be scoped.

And, there is no "best" way; there are different ways, and ways that are better, or even more idiomatic. But some of those may be beyond your level at this point. (For example, I rarely use a for loop in favor of the forEach() or other array traversal methods. In this case, I'd be using map(). But that may be too advanced at this point.)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic