Can i get input to perfect my answer for Conditionals 4 test?

The live out put shows ‘machine on’ in one line and ‘log in successful’ in the second line, if the machine is on. However, when the machineResult is ‘machine is off’ the second line still appears displaying ‘log in unsuccessful’

Is that right? Isn’t the second line supposed to be nonexistant when the machine is off? https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Conditionals


let machineActive = true;
let pwd = 'cheese';

let machineResult;
let pwdResult;

// Add your code here
if (machineActive==true) {machineResult='machine is on';}
else {machineResult='machine is off';}

(machineResult=='machine is on' && pwd=='cheese')? pwdResult='log in successful': pwdResult='log in not successful';

Hello @ayanda

first i recommend that you use === for comparison why? cause it check the value and the type both must match to get true but == only check value

so for example let x = 5 if we check x===“5” it will be false but x==“5” is true
which could lead to many bug later

you code say the if will check if machine is on or off

then after that it check if machine is on and pwd is chesse then it set pwd to success but if the machine was off or pwd not cheese or even both it will set pwd result to fail

you can change the order of the code and do little modify


(machineResult=='machine is on' && pwd=='cheese')? pwdResult='log in successful': pwdResult='log in not successful';

if (machineActive===true) {machineResult='machine is on';}
else {machineResult='machine is off';
pwdResult=''}

or check this tricky tranery statment


(machineActive===true) ? ((machineResult='machine is on')=== 'machine is on' && pwd === 'cheese') ? pwdResult='log in successful' : pwdResult='log in not successful' : machineResult='machine is off';
 

hope that help and have a nice day :slight_smile:

You are a genius!! :smiley: However, for your first code I noticed there was an issue but managed to sort it.

|| instead && works the live output perfectly. I think its because by the time the computer reads machineResult for the first time, it takes into account that it is uninitialized and && stops evaluating when it reaches its first falsy value.

And for the second code, you didn’t need to add the extra ==='machine is on' line as it works fine without…

I’ve learnt so much breaking down your solutions! Thank you :slight_smile:

did it work really :thinking: i was worry it could burn your computer :joy:

glad that you figure it out but i am not genius it was luck :joy:

thanks for that now we teach each other and that’s the best part of that community

thanks again and you welcome and have a nice day :slight_smile:

Another solution would be to nest the ternary operator in the if… else:

if (machineActive) {
    machineResult = "machine ON";
    (pwd === 'cheese') ? pwdResult = 'logged in' : pwdResult = 'not logged in';
}
else {
    machineResult = "machine OFF";
}

What do you think?

1 Like

That looks like some good, condense and clean code!! Did it work when you tried it in the Live Output?

Am currently reading documentation at the moment about devtools before learning abouy functions.

1 Like

Yes, it worked! And I can’t tell you what a highlight that was after spending ages on the test before it, the Conditionals 3 :joy:

You’re telling me! I hated the 3rd test… i even wound up angry and leaving other forums when they gave wrong advice without actually attempting the test itself lol…

I am glad that i managed to truly learn at a deeper level tho…

And that is awesome that it worked. Def gonna use that way if i ever come across needing to code that way.

he he, I’m so sorry for creating such an annoying test question :wink:

But I am also so happy that we were able to help you, and that you learned it. Keep the questions coming, and happy learning.

Oh no dude, dont be sorry. Quite frankly its the reason i am much much better at coding than I was when i started. I wouldnt be if it had been easy!

Awesome, great attitude!

1 Like