mardi 23 février 2016

How to put user input into an array? Can for statements go inside an if/else statement? (Javascript)

I understand this may contain completely inane methods, but I am just playing around here to learn. My question is whether or not one can use an if statement inside an if/else statement like I have done. Lastly, how would you go about prompting for input and putting it into an array?

var inputArr1 = prompt('Type a noun');
var inputArr2 = prompt('Type another noun');
var inputArr3 = prompt('Type another noun');
var inputArr4 = prompt('Type a final noun');

document.getElementById("inputArr1").value;
document.getElementById("inputArr2").value;
document.getElementById("inputArr3").value;
document.getElementById("inputArr4").value;

var recordedArray = [ inputArr1, inputArr2, inputArr3, inputArr4 ];

if (recordedArray.length == 3){
    for (var i = 0; i <= recordedArray.length; i++){
         console.log("I like to eat " + recordedArray[i] + " with ketchup!");
}
else {
    console.log("You did not type a proper entry or something. Try again.");
     }
}

Aucun commentaire:

Enregistrer un commentaire