0

been working on this code. I want it to produce my array so it looks like this:

[0] cat

[1] dog

[2] *Whatever value entered by user*

Currently, it produces this

Pushed: *User Entered Value*

cat,dog,*user entered value*

Here's my code.

var array = ["cat", "dog"];
window.onload = function menu(){

var selection = prompt("Please use this menu, and type 1 to push an item on the array, 2 to pop an item off the array, and 3 to exit the menu.");

  if (selection == '1'){
    var push = prompt("What item would you like to push onto the array?");
      while (push != null) {    
        array.push(push);
        document.getElementById("pushed").innerHTML = "Pushed: " + "<em>" + push + "</em>" + "<br>" + "<br>" + array.toString() + "<hr>";
        menu();
        }
    } 
} 
3
  • 3
    Consider: while (push != null). This will only enter the loop if the test is true. You don't change the value of push inside the loop, so its value will not change and so it will never exit the loop. Commented Feb 16, 2016 at 6:02
  • I don't understand if you want it to display array like in the first code why are you writing "Pushed: ....." in your code ? Commented Feb 16, 2016 at 6:34
  • @Burawi The "Pushed: ...." Just shows what item the user pushed, then I want it to add onto the array, and display as I showed above. Right now I just have it displaying in a single line. Commented Feb 16, 2016 at 16:58

3 Answers 3

1

First of all, the above code would result in infinite loop as push is never reinitialized to null.

Here is what I come up with:

<script>
var array = ["cat", "dog"];
window.onload = function menu(){

var selection = prompt("Please use this menu, and type 1 to push an item on the array, 2 to pop an item off the array, and 3 to exit the menu.");
var push = null;
var index = 0;
  if (selection == '1'){
     push = prompt("What item would you like to push onto the array?");
      while (push != null) {    
        array.push(push);
       // document.getElementById("pushed").innerHTML = "Pushed: " + "<em>" + push + "</em>" + "<br>" + "<br>" + array.toString() + "<hr>";

       var html = "Pushed: <em>" + push + "</em><br>";
       var list = '';
       var len = array.length;
       for(var i = 0;i<len;i++){
       list = list +"["+i+"] "+array[i]+'<br/>';
       }
       document.getElementById("pushed").innerHTML = html+list;
         // make push null to avoid infinite loop.
       push= null;
        menu();
        }
    } 
} 

</script>

Here is a fiddle, which can help you.

https://jsfiddle.net/vozhuuq4/

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that is exactly how I wanted to do that! Thanks dude I really appreciate your help.
1

I would change the:

while (push != null) {}

for an if, because that piece of code is never executing.

if (push != null) {}

1 Comment

Thank you for that suggestion.
-1

You can store value in array as properties

var array = [];
window.onload = function menu(){

var selection = prompt("Please use this menu, and type 1 to push an item on the array, 2 to pop an item off the array, and 3 to exit the menu.");

if (selection == '1'){
var push = prompt("What item would you like to push onto the array?");
  while (push != null) {    
    array[array.length-1] = push;
    document.getElementById("pushed").innerHTML = "Pushed: " + "<em>" + push + "</em>" + "<br>" + "<br>" + array.toString() + "<hr>";
    menu();
    }
 } 
} 

2 Comments

I have to start out my array with having the cat and dog in it. And I want it to show the index of the array in a list with the item it correlates too, but I appreciate your help.
for innitializing array try like this var array = {"0": "Dog", "1": "cat"};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.