educative.io

Educative

If else javascript

let nb1 = Number(prompt(“Enter nb1:”));
let nb2 = Number(prompt(“Enter nb2:”));
let nb3 = Number(prompt(“Enter nb3:”));

if (nb1 > nb2) {
nb1 = nb3 * 2;
} else {
nb1++;
if (nb2 > nb3) {
nb1 += nb3 * 3;
} else {
nb1 = 0;
nb3 = nb3 * 2 + nb2;
}
}
console.log(nb1, nb2, nb3);

Try to guess the final values of variables nb1 , nb2 and nb3 depending on following initial values:

nb1 = 4

nb2 = 4

nb3 = 4

My doubt is how to go through these type of question where , program is given and we have to solve it withour performing.

Hi Dhruv,
You can try to dry run such programs by making a table with variables as columns and statements as rows. for above program, make 3 columns nb1,nb2 and nb3 and after each statement update the value of all three variables in that statements row.