dimanche 18 septembre 2016

Need assistance with coding assignment! How to stop my if else statement so it only runs once

I need help with how to make my code's if else statement to only run once and I was wondering if I followed all the instructions that were asked for this assignment. Also, i may have some errors or placement issues. I am a beginner javascript user as I never used the program before this. The instructions are below as followed:

Run an if/else statement to see if age is less than 60, then display alerts depending on the result Push a third question to the questions array Push the corresponding array of answers to the answers array Add if and else if statements to the score-checking to increment the score at different levels for each answer: 3 points, 2 points, or 1 point depending on the user’s choice. These should be implemented for all three questions. Try to use a for loop to cycle through all the questions and answers, and consider using the index in the array for the scoring system. Add ‘
Score: ‘ plus the user’s score to their paragraph (id=’myParagraph’) by using document.getElementById to change the innerHTML Add an additional prompt asking the user to calculate the results of a basic math question Increment the user’s score by 5 if their answer to the math question is equal to the results of the same math question as an expression: make sure the equation is part of your if statement.

Code below:

<!doctype html>
<html>
<head>
<title>Quiz</title>
</head>
<body>
        <br id="myScore"> <p id="myParagraph">A paragraph!</p></br>
</body>
<script>
    var question = ['What is your quest?','What is the airspeed of an unladen swallow?','What planet\'s moon can more liquid water than all of Earth\'s oceans?'];
var score = 0;
var answers = [['To seek the grail','I don\'t know that','To be in Monty Python'],['African or European?','92','24'],['Europa','Titan','Pandora']];
    console.log(answers);
    console.log(answers[1][0]);
var name = prompt('What is your name?');
//alert('A message;);
document.getElementById('myParagraph').innerHTML = 'Hello ' + name + '!';
document.getElementById('myScore').innerHTML = 'You scored ' + score + 'points!';   
    console.log(typeof name);
var age = prompt('How old are you?');
    console.log(typeof age);
    age = Number(age);
    console.log(typeof age);
        if (age < 60) {
    alert(age); 
}
    for (var i=0; i<=age; i++) {
    document.write(i);
    document.write(' ');
}

var math = prompt('What is 16+2-1*3=?');
console.log(typeof math);
    math = Number(math);
if (math === 15) {
    alert(math);
}else{
    alert('You are incorrect');
}
for (var j=0; j<=math; i+5) {
    document.write(j);
    document.write(' ');
}
var guess = prompt(question[0] + '\n\u2022 ' + answers[0][0] + '\n\u2022 ' + answers [0][1] + '\n\u2022 ' + answers[0][2]);
if (guess === answers[0][0]) { 
    score += 3;
    console.log(score + ' ' + 'points earned!');

} else if(guess === answers[0][1]) {
    score += 2;
    console.log(score + ' ' + 'points earned!');

} else if(guess === answers[0][2]) {
    score += 1;
    console.log(score + ' ' + 'points earned!');    
}

var guess = prompt(question[1] + '\n\u2022 ' + answers[1][0] + '\n\u2022 ' + answers [1][1] + '\n\u2022 ' + answers[1][2]);
if (guess === answers[1][0]) { 
    score += 1;
    console.log(score + ' ' + 'points earned!');

}else if(guess === answers[1][1]) {
    score += 2;
    console.log(score + ' ' + 'points earned!');

}else if(guess === answers[1][2]) {
    score += 3;
    console.log(score + ' ' + 'points earned!');    
}

var guess = prompt(question[2] + '\n\u2022 ' + answers[2][0] + '\n\u2022 ' + answers [2][1] + '\n\u2022 ' + answers[2][2]);
if (guess === answers[2][0]) { 
    score += 3;
    console.log(score + ' ' + 'points earned!');

}else if(guess === answers[2][1]) {
    score += 2;
    console.log(score + ' ' + 'points earned!');

}else if(guess === answers[2][2]) {
    score += 1;
    console.log(score + ' ' + 'points earned!');    
}


</script>   
</html>

Aucun commentaire:

Enregistrer un commentaire