0

I can't figure away around this, I want the MD array to store the name of a book and the price. I know this is easier with two arrays but really wanted to give a MD array ago in javascript. I have tried several ways but can only get the book name or the book price to display twice. Any help would be greatly appreciated.

var bookName = new Array();

for (var i = 0; i < 3; i++) 
{
bookName[i] = new Array();

    for (var j = 0; j < 1; j++) 
    {
        bookName[i] = prompt("Please enter book name!");
        bookName[[i][j]] = prompt("Please enter book price!");
        document.write(bookName[i] + bookName[[i][j]] + "</br>");
    }
}
1
  • I'm not sure what sort of structure you're wanting. Are you wanting to make it do something like [["name", "price"], ["name2", "price2"]]"? Commented Mar 30, 2012 at 14:50

7 Answers 7

5

I think you should just use an array of objects for this:

var books = [];
for(var i = 0; i < 3; i++) {
  books.push({
    name: prompt("Please enter book name!"),
    price: prompt("Please enter book price!")
  });
  document.write(books[i].name + books[i].price + "</br>");   
}

If you prefer, you can do an array of arrays instead:

var books = [];
for(var i = 0; i < 3; i++) {
  books.push([prompt("Please enter book name!"), prompt("Please enter book price!")]);
  document.write(books[i][0]+ books[i][1] + "</br>");   
}
Sign up to request clarification or add additional context in comments.

1 Comment

There are many valid solutions here, but this is probably the one I would use. Most concise and easiest to follow.
4

Here is how you build a 2D array:

var bookName = new Array();
for (var i = 0; i < 3; i++) 
{
    bookName[i] = new Array();
    bookName[i][0] = prompt("Please enter book name!");
    bookName[i][1] = prompt("Please enter book price!");
    document.write(bookName[i] + bookName[i][0] + ": " + bookName[i][1] + "</br>");
}

You could also do that with objects inside the array:

var bookName = new Array();
for (var i = 0; i < 3; i++) 
{
    bookName[i] = {};
    bookName[i]["name"] = prompt("Please enter book name!");
    bookName[i]["price"] = prompt("Please enter book price!");
    document.write(bookName[i] + bookName[i].name + ": " + bookName[i].price + "</br>");
}

2 Comments

And use object literals and array literals instead of repeated bookName[i][prop] assignments
@Peter, I like your answer, I'd certainly code it in a similar way. But on this answer, I decided to keep the code structure closer to the OP code, because I thought it would be easier for him to understand.
0

Instead of this:

bookName[i] = prompt("Please enter book name!");
bookName[[i][j]] = prompt("Please enter book price!");
document.write(bookName[i] + bookName[[i][j]] + "</br>");

Do this:

bookName[i][j][0] = prompt("Please enter book name!");
bookName[i][j][1] = prompt("Please enter book price!");
document.write(bookName[i][j][0] + bookName[i][j][1] + "</br>");

I don't think I've ever seen var[[i][j]] format before...

Comments

0

u r overwriting your bookName[i]

bookName[i] = new Array(); and bookName[i] = prompt("Please enter book name!");

try this:

var bookName = new Array();
for (var i = 0; i < 3; i++)  {
    bookName[i] = new Array();
    for (var j = 0; j < 1; j++)  {
        var name = prompt("Please enter book name!");
        var price = prompt("Please enter book price!");
        bookName[i][j] = {name:name, price:price}
        document.write(bookName[i][j].name + bookName[i][j].price + "</br>");
    }
}

2 Comments

Can you explain what this part is doing please? bookName[i][j] = {name:name, price:price}
It is assign bookName[i][j] an hashmap like object with key = name and value = (variable name), and same with price key = 'price' and value = price (variable)
0

A multidimensional array doesn't make much sense here, you probably want an array of objects.

var books = [], i = 3;

while( i-- ) {
    books.push({
        name: prompt("Please enter book name!"),
        price: prompt("Please enter book price!")
    });
}

Comments

0

I'm not sure what you are doing there. Please make yourself familiar with the concept of Arrays, Objects and accessing of properties in JavaScript.

var bookName = new Array();

for (var i = 0; i < 3; i++) 
{
bookName[i] = new Array();
// the i. item of bookname holds an empty array now

    for (var j = 0; j < 1; j++) 
    {
        bookName[i] = prompt("Please enter book name!");
// you're overwriting the array just created with a string now
        bookName[[i][j]] = prompt("Please enter book price!");
// this is absolutely invalid syntax
        document.write(bookName[i] + bookName[[i][j]] + "</br>");
// as well as this
    }
}

What you wanted to do:

var bookName = new Array();
for (var i = 0; i < 3; i++) {
    bookName[i] = new Array();
    for (var j = 0; j < 1; j++) {
        // this is absolute ugly in a for loop:
        if (j==0)
            bookName[i][j] = prompt("Please enter book name!");
        else if (j==1)
            bookName[i][j] = prompt("Please enter book price!");
    }
    // direct accessing is better:
    document.write(bookName[i][0] + bookName[i][1] + "</br>");
}

but it would be much easier so:

var bookName = [];
for (var i = 0; i < 3; i++) {
    bookName[i] = {
        name: prompt("Please enter book name!"),
        price: prompt("Please enter book price!")
    };
    document.write(bookName[i].name + bookName[i].price + "</br>");
}

Comments

0
var bookName = [];

for (var i = 0; i < 3; i++)

{
  var b = bookName[i];
  b = [];
  for (var j = 0; j < 1; j++) 
  {
    bookName[i] = prompt("Please enter book name!");
    b[j] = prompt("Please enter book price!");
    document.write(bookName[i] + b[j] + "</br>");
  }
}

This should do it..

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.