darla_sud
darla_sud

Reputation: 359

implementing funny game with Js

I am trying to implement a functionality which ask several qs to store them return funny sentence with those inputs. But it's not working, there are no errors shown, but list not saving the 3 inputs from the user itself, and noticed that each time it saves the input in alist array..it replaces the previous list item,,but I want to save all of the three.

for(i = 0; i < 3; i++) {
        var qlist = ["adjective?","Verb?", "noun?"];
        var alist = [];
        alist[i] = prompt(qlist[i] + "[questions number-" + i + "]");
    }
    var statement = document.write("There once was a "+ alist[0] + " programmer who once wanted to use javascript to " + alist[1] + " the " + alist[2]);    
    alert(statement);

Upvotes: 1

Views: 44

Answers (2)

Jacek Pietal
Jacek Pietal

Reputation: 2019

alist needs to be initialised outside of loop. otherwise you overwrite it.

also, dont alert(statement) the type is undefined either way.

also, dont init qlist 3 times.

let alist = [];
let qlist = ["adjective?","Verb?", "noun?"];
for(let i = 0; i < 3; i++) {
    alist[i] = prompt(qlist[i] + "[questions number-" + i + "]");
}
let statement = document.write("There once was a "+ alist[0] + " programmer who once wanted to use javascript to " + alist[1] + " the " + alist[2]);

Upvotes: 2

OneOfOne
OneOfOne

Reputation: 99351

Your alist is defined inside the for loop, you need to put it outside:

var qlist = ["adjective?","Verb?", "noun?"];
var alist = [];
for(i = 0; i < 3; i++) {
    alist[i] = prompt(qlist[i] + "[questions number-" + i + "]");
}
var statement = document.write("There once was a "+ alist[0] + " programmer who once wanted to use javascript to " + alist[1] + " the " + alist[2]);    
alert(statement);

Upvotes: 1

Related Questions