Assessment for conditional 2 test

Hello everyone.
I have written the solution for the conditional 2 test. It’s working but I really appreciate an assessment. I’m not sure I’ve done everything as required but I figured out a way to make it work.
Look forward to hearing from you.
My code:
https://codepen.io/mahmoud-fed/pen/MWjegJO

instructions are on the following page:

Conditionals 2

1 Like

elslamo alikom @Dev2

you doing great just some tips and advice

if(!(machineActive==true)){
response = "Turn machine on" 
}

when compare it recommened to use === not just == why?
cause === compare the variable and check if they are from same type but == only compare the values
for example
let x =5 let y =“5”
if we use x==y that would lead to true but if we use x===y that would be false
and we should compare with the same type cause the other one could lead to bug that really hard to find later in big projects

and for this

if(machineActive=true)  

first you should use else not if and the condition is always true cause that would set machineActive to true which will make it always true
in js any true or non 0 or not null is true

hope that help and have a nice day :slight_smile:

1 Like

thank you @justsomeone for your practical advice. In fact, I tried === first but the code didn’t work. Then it worked when I used two equal signs ==. I don’t know why.

you very welcome but not sure what could be the cause of that :slight_smile:

1 Like

I’ve taken a look at your code and i’d say it needs a bit of fixing.
First off, you’ve used two different if statements for (machine === false) and (machine === true) when in fact, the task was to make an if statement for machine on/off (true/false) and then NEST if…else if… else statements inside the (machine === true) block to check the score values.

So not this:

// first if statement
if (machine === false) {...}

// second if statement (nested)
if (machine === true) {
  ...
  else if (score...) {
  ...
  else {
    ...
  }
}

But this:

// one if statement - machine is on
if (machine === true) {
  ...
  // check score
  if (score...) {
    ...
    // check other score
    else if (score...) {...}
    // if not any of the scores above, this score 
    else {...}
  // machine is off
  else {...}

The last else {} (outer) block runs ONLY on the condition that machine === false so it is not necessary to explicitly state this since your first condition was that the machine === true.

Secondly, it is not necessary to explicitly state this:

(machine === true) or (machine === false)
All of the values in JavaScript evaluate to a “truthy” and “falsy” value - this means that JavaScript itself forces values in the background to “true” or “false” for the purposes of conditional checks.
There are 6 values in JavaScript that evaluate to a “falsy” value:

false
0
""
NaN
null
undefined

This means that your FIRST if statement could’ve been written like this:

if (machine) {
...
} else {
...
}

It means “Do this if machine === true, else do this if machine === false”.

Thirdly, you don’t check whether score is in between numbers since you’re using OR operator (||). You should instead use AND (&&). Here’s what i mean:

(score ===0 || score <=19)

OR operator only cares about whether ONE of these operands is TRUE. AND operator has to have ALL operands TRUE in order to have the whole expression evaluate to TRUE. For example:

(score >=0 || score <=19)

score = 5 ==> TRUE
score = 20 ==> TRUE
score = -1 ==> TRUE
OR operator DOES NOT check in between values. It only cares whether one of these is TRUE.

If you’ve written this with an AND operator:

(score >=0 && score <=19)

ALL conditions would’ve been checked.
score = -1 ==> FALSE
score = 5 ==> TRUE
score = 20 ==> FALSE

1 Like

Thank you very much @Chrysaoros for this detailed explanation. this is very helpful.