0

I have a problem alerting out 2 arrays on same line in Javascript. The user should write Movie name and movie rating(1-5) 5 times, and printMovies() function should print out users (movie)name and (movie)rating in a single line something like this:

Movie             Rating

Star Wars         5

Lord of the Rings 4

Casino            4

Movie4            3

Movie5            2

How do I alert out all of five inputs in a single line (movie + rating) per line, AFTER they have got input from user?

//CODE

var title;

var rating;

var a = [];

var movies = [];

var ratings = [];

function buttonAddMovie()//Button onclick
{

    addMovie(title, rating)
    addMovie(title, rating)
    addMovie(title, rating)
    addMovie(title, rating)
    addMovie(title, rating)
}

function addMovie(title, rating) {

    do{
    title = prompt("Enter movie: ");
    }
    while (title == "");

    do {
    rating = parseInt(prompt("Enter rating 1-5 on movie " + (title)));
    }
    while (rating > 5 || rating < 1);

    movies.push(title);//Pushing title to movies
    a.push(movies);//Pushing movies to a array
    ratings.push(rating);//Pushing rating to ratings
    a.push(ratings);//Pushing ratings to a array
    printMovies()
}

function printMovies() {

    for (var i = 0; i < a.length; i++) {
    alert(a[0][0] + " " + a[0][1]);//Here is my biggest problem!

    }
}
0

3 Answers 3

1

You problem is in the addMovie function your push the array to array. that means structure of the a is

a = [['title'] , ['rating'] , ['title','title1'],['rating',ratting1],......]

Try this with json object.

  var movies = [];
       function addMovie(title, rating) {
            var movie = {};
            do {
                title = prompt("Enter movie: ");
            }
            while (title == "");

            do {
                rating = parseInt(prompt("Enter rating 1-5 on movie " + (title)));
            }
            while (rating > 5 || rating < 1);
            movie.title = title;
            movie.ratings = rating; movies.push(movie);
        }
        function printMovies() {
            for (var i = 0; i < movies.length; i++) {
                alert(movies[i].title + " " + movies[i].ratings); 
            }
        }

function buttonAddMovie()//Button onclick
{

    addMovie(title, rating);
    addMovie(title, rating);
    addMovie(title, rating);
    addMovie(title, rating);
    addMovie(title, rating);
    printMovies();
}
Sign up to request clarification or add additional context in comments.

3 Comments

there's no reason to have the for loop in printMovies
now the movie object is never pushed to movies -> movies.lenght == 0 :)
Thanks, is there any chance of "collecting" five inputs and then alert all of them in a list at the end? Instead of alert them one per time...
0

<html>

<head>
  <script>
    var arr = [{
      "Movie": "Movie1",
      "Rating": 1
    }, {
      "Movie": "Movie2",
      "Rating": 2
    }, {
      "Movie": "Movie3",
      "Rating": 2
    }, {
      "Movie": "Movie5",
      "Rating": 4
    }, {
      "Movie": "Movie4",
      "Rating": 5
    }, ];

    var str = "";

    for (var i = 0; i < arr.length; i++) {
      str += "Movie Name: " + arr[i].Movie + " || Rating: " + arr[i].Rating + "\n";
}
      alert( "Json Iterated Output \n" + str);
  </script>
</head>

</html>

Comments

0

The reason it wasn't working was you had an array, a.

You pushed the movie name to 'a'

a = ['Movie name'];

But then you pushed the rating to 'a' separately

a = ['Rating'];

So the movie's name was replaced with rating, hence the alerts saying '(movie's name) undefined', then '(movie's rating) undefined'.

What I've done is removed the rating array and pushed the movie name and rating at the same time.

Also, I've changed the for loop to display the current movie by changing

alert(a[0][0] + " " + a[0][1]);

to

alert(a[i][0] + " " + a[i][1]);

otherwise it will always display the first movie rating.

    var title;

    var rating;

    var a = [];

    var movies = [];

    function buttonAddMovie()//Button onclick
    {
        addMovie(title, rating, document.getElementById('quantity').value)
    }

    function addMovie(title, rating, amount) {
      for(var count = 0; count < amount; count++){
        do{
        title = prompt("Enter movie: ");
        }
        while (title == "");

        do {
        rating = parseInt(prompt("Enter rating 1-5 on movie " + (title)));
        }
        while (rating > 5 || rating < 1);

        movies.push(title,rating);//Pushing title to movies
        a.push(movies);//Pushing movies to a array
        movies = [];
      }
      printMovies();
    }

    function printMovies() {

        for (var i = 0; i < a.length; i++) {
          alert(a[i][0] + " " + a[i][1]);
          document.getElementById('movielist').innerHTML+= "<li>"+a[i][0]+" - "+a[i][1]+"</li>"; // Adds movies to the <ul>
        }
    }
<h2>Add</h2>
<input id='quantity' placeholder='Amount of movies you want to add' /><br>
<button onclick='buttonAddMovie();'>Add Movies</button>
<h2>Movie list</h2>
<ul id='movielist'></ul>

Now you can stack as many as you want by writing the number in the input tag.

The movies are then displayed at the end in an unformatted list.

Very basic code, but I hope it helps.

5 Comments

Thanks, is there any chance of "collecting" five inputs and then alert all of them in a list at the end? Instead of alert them one per time...
Changed it so that movie array is emptied every time, so it doesn't push the same data every time, because movie would equal [ ['movieone', 5], ['movie2', 2] ] and the first array ['movieone', 5] would be pushed every time. Then, I've looped everything the amount of times specified in the input, so it'll ask you for the data for all the movies in a big block, then display it.
Also, check out this fiddle. It's a version of this with a table instead of a ul
If you find that my answer works best, press the green checkmark next to my answer to approve of my answer. Until then, I will not have received any reputation from my answer. Thanks.
Hello? You there? If you find this useful, please accept my answer by pressing the green checkmark.

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.