简体   繁体   English

在同一行javascript中警报2个数组

[英]alert 2 arrays in same line javascript

I have a problem alerting out 2 arrays on same line in Javascript. 我在用JavaScript在同一行上警告2个数组时遇到问题。 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 name和Movie rating(1-5)写入5次,并且printMovies()函数应在一行中打印出用户(电影)名称和(电影)评级,如下所示:

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!

    }
}

You problem is in the addMovie function your push the array to array. 您的问题是在addMovie函数中将数组推入数组。 that means structure of the a is 这意味着a的结构是

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

Try this with json object. 尝试使用json对象。

  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();
}

 <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> 

The reason it wasn't working was you had an array, a. 它不起作用的原因是您有一个数组。

You pushed the movie name to 'a' 您将电影名称推为“ a”

a = ['Movie name'];

But then you pushed the rating to 'a' separately 但后来您将评级分别推为“ a”

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 此外,我还更改了for循环以通过更改显示当前电影

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. 非常基本的代码,但希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM