Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check three given numbers

I would like to check 3 numbers:

  • If the three numbers are same I have to display 30,
  • If two numbers are same I display 40,
  • Otherwise, I display 20.

When, I enter 3 numbers for example:

8
8
8

The output displays

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

30
40

Normally, the output is 30. Why is the value 40 added also?

enter image description here

let readline = require("readline-sync");

let nb1 = parseInt(readline.question("Enter nb1 please : "));
let nb2 = parseInt(readline.question("Enter nb2 please : "));
let nb3 = parseInt(readline.question("Enter nb3 please : "));


if (nb1 == nb2 && nb2 == nb3) {
    console.log(30);
}

if (nb1 == nb2 || nb2 == nb3 || nb3 == nb1) {
    console.log(40);
}

else {
    console.log(20);
}

Thanks

>Solution :

Use the second if in an else if block.
Both of the first two ifs are true when 3 numbers are equal.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading