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

**URL:** <https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485>\
**Category:** Learn\
**Created:** [August 27, 2020, 5:29am UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485 "2020-08-27T05:29:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayanda](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ayanda/32/40549_2.png) [@ayanda](https://discourse.mozilla.org/u/ayanda)\
**Post date:** [August 27, 2020, 5:29am UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/1 "2020-08-27T05:29:25Z")

</div>

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](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Conditionals)

```auto

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';

```

---

<div class="post-metadata">

**Author:** ![justsomeone](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/justsomeone/32/67787_2.png) [@justsomeone](https://discourse.mozilla.org/u/justsomeone)\
**Post date:** [August 27, 2020, 7:53am UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/2 "2020-08-27T07:53:12Z")

</div>

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

```auto

(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

```auto

(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 🙂

---

<div class="post-metadata">

**Author:** ![ayanda](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ayanda/32/40549_2.png) [@ayanda](https://discourse.mozilla.org/u/ayanda)\
**Post date:** [August 27, 2020, 9:39am UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/3 "2020-08-27T09:39:02Z")

</div>

> [@justsomeone](#):
>
> ```auto
> (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=''}
> 
> ```

You are a genius!! 😃 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 🙂

---

<div class="post-metadata">

**Author:** ![justsomeone](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/justsomeone/32/67787_2.png) [@justsomeone](https://discourse.mozilla.org/u/justsomeone)\
**Post date:** [August 27, 2020, 9:47am UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/4 "2020-08-27T09:47:14Z")

</div>

did it work really 🤔 i was worry it could burn your computer 😂

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

> [@ayanda](#):
>
> And for the second code, you didn’t need to add the extra `==='machine is on'` line as it works fine without…

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 🙂

---

<div class="post-metadata">

**Author:** ![agathasta](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/agathasta/32/40771_2.png) [@agathasta](https://discourse.mozilla.org/u/agathasta)\
**Post date:** [August 28, 2020, 4:59pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/5 "2020-08-28T16:59:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ayanda](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ayanda/32/40549_2.png) [@ayanda](https://discourse.mozilla.org/u/ayanda)\
**Post date:** [August 28, 2020, 5:26pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/6 "2020-08-28T17:26:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![agathasta](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/agathasta/32/40771_2.png) [@agathasta](https://discourse.mozilla.org/u/agathasta)\
**Post date:** [August 28, 2020, 5:33pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/7 "2020-08-28T17:33:53Z")

</div>

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 😂

---

<div class="post-metadata">

**Author:** ![ayanda](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ayanda/32/40549_2.png) [@ayanda](https://discourse.mozilla.org/u/ayanda)\
**Post date:** [August 28, 2020, 5:43pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/8 "2020-08-28T17:43:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrisdavidmills](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/chrisdavidmills/32/21688_2.png) [@chrisdavidmills](https://discourse.mozilla.org/u/chrisdavidmills)\
**Post date:** [August 28, 2020, 5:49pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/9 "2020-08-28T17:49:18Z")

</div>

> [@ayanda](#):
>
> You’re telling me! I hated the 3rd test

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

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.

---

<div class="post-metadata">

**Author:** ![ayanda](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ayanda/32/40549_2.png) [@ayanda](https://discourse.mozilla.org/u/ayanda)\
**Post date:** [August 28, 2020, 6:11pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/10 "2020-08-28T18:11:38Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![chrisdavidmills](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/chrisdavidmills/32/21688_2.png) [@chrisdavidmills](https://discourse.mozilla.org/u/chrisdavidmills)\
**Post date:** [August 28, 2020, 6:16pm UTC](https://discourse.mozilla.org/t/can-i-get-input-to-perfect-my-answer-for-conditionals-4-test/66485/11 "2020-08-28T18:16:45Z")

</div>

Awesome, great attitude!
