1

I started to learn javascript and I need some help.
I need to insert prompt inputs into array and read from it.

var array = [
    ["Cat","Black","Awesome"],
        ["Dog","White","Funny"],
        ["Chicken","Brown","Delicious"]
    ];

array.push(prompt("Animal name:"));
array.push(prompt("Animal color:"));
array.push(prompt("Type or something:"));   

If I do something like that and I want to write it out, the output is:

Cat,Black,Awesome
Dog,White,Funny
Chicken,Brown,Delicious

And my prompt output is (example):

Elephant
Blue
Big

How would I insert prompt into array that the output would be as above.

Thank you very much for your answer.
I know it's some basic stuff but I've checked a lot of websites and couldnt find an answer.

3
  • haven't you already done that?? you have already pushed it into the array? Commented Mar 20, 2017 at 12:27
  • @OusmaneMahyDiaw I think OP want's to push to another array inside array. Commented Mar 20, 2017 at 12:28
  • Yes, exactly. How to do that, and how to read from it? Commented Mar 20, 2017 at 12:30

1 Answer 1

2

You are not pushing an array into your first array only value, that's why it does not look the same. You could simply retrieve the prompt value into a first array and then push this array into your other one.

var firstArray = [
    ["Cat","Black","Awesome"],
    ["Dog","White","Funny"],
    ["Chicken","Brown","Delicious"]
];

var tempArray = [];

tempArray.push(prompt("Animal name:"));
tempArray.push(prompt("Animal color:"));
tempArray.push(prompt("Type or something:"));   

firstArray.push(tempArray);
Sign up to request clarification or add additional context in comments.

Comments

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.