1

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

2 Answers 2

2

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your answer!
1

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

1 Comment

Thanks! Its really helped me.

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.