Hello,
I’m working on writing a javascript console program for a quiz that takes user input. I’m using arrays for questions and answers and loops to iterate through the questions. I’m having issues with the score calculations not working correctly. i’m trying to figure out a way to construct an array of user answers and compare it to the correct answer key.
i’m also wanting to figure out how to make the input case insensitive so ‘sally ride’ and ‘Sally Ride’ will both be accepted.
here is what i have with notes.
const input = require(“readline-sync”);
//ask candidate name
let candidateName = input.question(“What is your name?”);
console.log(“Candidate Name:”, candidateName);
//build question bank
let question1 = (1) True or false: 5000 meters = 5 kilometers.
);
let question2 = (2) (5 + 3)/2 * 10 = ?
);
let question3 = (3) Given the array [8, "Orbit", "Trajectory", 45], what entry is at index 2?
);
let question4 = (4) Who was the first American woman in space?
);
let question5 = (5) What is the minimum crew size for the International Space Station (ISS)?
)
let questionBank = [question1, question2, question3, question4, question5];
//build answer key
let ans1 = true;
let ans2 = 40;
let ans3 = “trajectory”;
let ans4 = “Sally Ride”;
let ans5 = 3;
let answerKey = [ans1, ans2, ans3, ans4, ans5];
//run quiz
let candidateResponse=;
let correctResponses = 0
let i = 0;
while(i < questionBank.length) {
//outer loop
input.question((questionBank[i])+"\nYour Response: ");//KEEP
console.log("Correct Answer:",answerKey[i], "\n");
//inner loop
candidateResponse = candidateResponse + input.question;
//for()
i++;
if (candidateResponse[i] === answerKey[i]){
correctResponses = correctResponses +1;}
}
for (i = 0; i < 5; i++) {
if (typeof candidateResponse[1] !== Number) {
candidateResponse[i].toLowerCase();
} else {
console.log(candidateResponse[i]);
}
}
let scoreCounter = 0;
let percentage = (scoreCounter)/(answerKey.length)*100
console.log(>>> Overall Grade: ${percentage}% (${scoreCounter} of 5 responses correct) <<<
);
if (percentage > 70) {
console.log(">>>Status: PASSED <<<")
} else {
console.log(">>> Status: FAILED <<<")
}
while(i < answerKey.length) {
if (candidateQuizAnswer[i] === answerKey[i]){
correctResponses = correctResponses +1;
} i++
}