Home > Mobile >  I want the "YOU ENTERED LEVEL X" message to be displayed only once. Please read the full q
I want the "YOU ENTERED LEVEL X" message to be displayed only once. Please read the full q

Time:07-18

I need help for this code. Let me tell what the problem is. For example, after running the program , if the score is 4, as soon as user scores 4, it will be printed Congratulations!! You entered level 1 of the game and the same goes for level 2 and level 3 for the score 7 and 9 respectively. Now, the problem is- if user scores 4, then the line will be printed for Level 1 and if user fails to answer the next question, then again this line will be printed. What I want is that the line to be printed only once the user enters the score. I don't want it to be printed again and again if the user fails to pass the score for which the message is written. I hope I am able to explain the problem properly. If you don't get what I am trying to say, please tell me. I'll try to explain my problem more elaborately. Thank You. Below is the code

var readlineSync = require('readline-sync');

var userName = readlineSync.question("May we know your name please? ");

console.log("Hello "   userName   " to the neog.camp fun quiz game!!\n");

console.log("Please answer 4 questions correctly to reach Level 1 of the game,7 to reach Level 2 of the game, and 9 to reach Level 3 of the game.\nALL THE BEST :) \n\n");


var currentScore = 0;
var highScores =
{
  azhar: 10,
  bhargav: 7
};

function ask(question, answer) {
  var userAnswer = readlineSync.question(question);
  if (userAnswer === answer) {
    console.log("Correct!!");
    currentScore  ;
    console.log("score: ", currentScore);
  }
  else {
    console.log("Wrong!!");
    console.log("score: ", currentScore);
  }
  if(currentScore>=4 && currentScore<5)
     {
    console.log("Congrats!! You entered LEVEL 1 of the game!!")
     }
  if(currentScore>=7 && currentScore<8)
     {
    console.log("Congrats!! You entered LEVEL 2 of the game!!")
     }
  if(currentScore>=9 && currentScore<10)
     {
    console.log("Congrats!! You entered LEVEL 3 of the game!! Yippeee ;) ")
     }
}

var questions =
  [
    {
      question: "What is the capital of INDIA? ",
      answer: "New Delhi"
    },
    {
      question: "What is the full name of MS Dhoni? ",
      answer: "Mahendra Singh Dhoni"
    },
    {
      question: "Who founded Amazon ?",
      answer: "Jeff Bezos"
    },
    {
      question: "Which is the largest country in Asia? ",
      answer: "China"
    },
    {
      question: "How many sides does a quadrilateral have? ",
      answer: "4"
    },
    {
      question: "Which Indian Cricketer did hit six sixes in six balls against England in 2007? ",
      answer: "Yuvraj Singh"
    },
    {
      question: "What is the full form of CS GO? ",
      answer: "Counter Strike Global Offensive"
    },
    {
      question: "How many players are there in a football team excluding the goal keeper? ",
      answer: "10"
    },
    {
      question: "Which language is called the mother of all programming languages?",
      answer: "C"
    },
    {
      question: "What is the name of the highest mountain in the world? ",
      answer: "Mount Everest"
    }
  ];

for (var i = 0; i < questions.length; i  ) {
  var currentQuestion = questions[i];
  ask(currentQuestion.question, currentQuestion.answer);
}



console.log("\nYour final score is : ", currentScore);

if (currentScore >= highScores.azhar) {
  console.log("Congratulations!! You are the new highest scorer!! \t Kindly send the screenshot of the score to us.\nThank You")
}
else {
  console.log("Oops!! You failed to beat the highest scorer!!\nBetter Luck Next Time")
  console.log("High Scores :- \n"   "Azhar : "   highScores.azhar   "\nBhargav : "   highScores.bhargav);
}

CodePudding user response:

I think adding a return statement in the ask function after they have entered a wrong answer will do the trick.

else {
    console.log("Wrong!!");
    console.log("score: ", currentScore);
    return;   //<-- this line
  }

It stops further execution of the ask function.

If you would like to refactor that function a bit more, you can do an early return check like this. You won't need the else statement, because your function returns early when the answer is wrong.

You can return whatever you like, in this case a null/void is returned.

//...
if (userAnswer !== answer) {
    console.log("Wrong!!");
    console.log("score: ", currentScore);
    return;
}
console.log("Correct!!");
currentScore  ;
console.log("score: ", currentScore);

CodePudding user response:

Looks like you're looking for something that stores the "state" (I will be murdered by frontend devs for calling it this) of the level.

Storing something like that could be as simple as

let currentLevel = 0 // or 1, depending on where you want them to start

With the current code, you would only need to add in an if statement which checks the level

if(currentScore>=4 && currentScore<5 && !currentLevel === 1)
     {
    console.log("Congrats!! You entered LEVEL 1 of the game!!")
    currentLevel = 1
     }

CodePudding user response:

The reason why your program outputs the string when the user fails, is because your program will check the score and print the string, regardless of whether the answer is correct or not.

In order to change this you must specify an exit condition in the case where the answer is wrong

function ask(question, answer) {
  var userAnswer = readlineSync.question(question);
  if (userAnswer === answer) {
    console.log("Correct!!");
    currentScore  ;
    console.log("score: ", currentScore);
  }
  else {
    console.log("Wrong!!");
    console.log("score: ", currentScore);
    return

Unrelated to your question, but still an existing bug is the way you use userName inside your console.log. Presently it will output "username" as a string, rather than the value of the variable. This needs to be re-written using backticks as follows:

console.log(`Hello ${userName} to the neog.camp fun quiz game!!\n`);
  • Related