Can anyone see why this is printing as false?


#1

The assignment only asked for one parameter within the function (day). When i used the one parameter the code ran correctly and printed true. To help myself learn i try little things out to see if i can spot anymore logic and improve my basic skills.

So i added a new parameter called time. Both parameters are true so should print “true” but for some reason now i have entered a time parameter it always returns as false.

Can anyone take a look? Im sure its simple and a silly question to many of you, but its eating me up, i cant see for the life of me why this would be false.

Thank you.


#2

just like in math, JS has order order of operations. and + comes before strict equality checking.

so what you get is:

'Wednesday' === 'Wednesdaymidday' === 'midday'

i resolved the + operator (with argument values of parameter)


#3

sorry i dont understand what you mean? my if statement is asking

if day===‘wednesday’ + time===‘midday’
return true

I dont understand what you mean by 'you resolved the + operator (with argument values of parameter).

Can you please explain what you mean by" with argument values of parameter", this is not a course question so dont worry about giving away answers, this is an extra im just trying. thank you.

sorry if this is so basic but im very new to JS, about as new as it gets actually so any help is great. i just need answer for JS for dummies answers. :wink:


#4

what you expect is that your comparison is checking that day equals wednesday and time equals midday.

However, you use + operator, which is resolved first

that should be: argument value for parameters

well, function parameters get there value from arguments at function call. So i used the argument values to show how your comparison is going


#5

if i write it like this:

day===‘wednesday’ && time===‘midday’
or
day===‘wednesday’, time===‘midday’

so what should i use a , or && or +? All i want to ask is if both parameters are true, and if so print true ese print false.

How would you write the code? im realy lost. When asking just (day) i get a true but when asking both it always comes back flase no matter how i write the code.


#6

which do you think it is? JS has excellent documentation on all these operators. So if you are unsure of which operator to use, that is the place to go

what is worrying me is that here you use wednesday (latest reply) and not Wednesday (screenshot), given string comparison is case sensitive, its important to pay attention to this kind of details in programming, particular if you are receiving help

because now i have two factors which might be causing problems (use of incorrect operator or case sensitivity)

and given your latest reply only contains a small snippet of code, i am unable to verify the whole thing. A mistake might be really small, and not in the snippet you provided


#7

Just got it, it was a case of using simple commas! Trial and error gets their eventually. Now i can see the logic as to why too. Its always something simple that you just cant see, i walked away had a cofee and thought of it straight away. It is odd how it seems so obvious after the fact, before i could not work it out for the life of me.
Screenshot_11


#8

the day doesn’t matter now, as long as its midday, the plants could always use water:

const plantNeedsWater = function(day, time){
  if (day === 'Wednesday', time === 'Midday'){
    return true;
  } else {
    return false;
  }
}
console.log(plantNeedsWater('Wednesday', 'Midday'))
console.log(plantNeedsWater('Thursday', 'Midday'))
console.log(plantNeedsWater('DoesntMatterWhatYouPutHere', 'Midday'))

#9

Well spotted, why is it only taking the time into account i cant see why? EDIT: just worked it out !

that solved it i think.


#10

i do:

https://stackoverflow.com/questions/5347995/why-does-javascript-accept-commas-in-if-statements

understanding the operators you use is really important. Its not just about getting to the solution, but also understanding why

furthermore, don’t just test the correct data, also test “incorrect” data.


#11

I do understand the operator, the && means both have parameters must be true. i should of spotted that. I thought i tested it but i was only changing the time and seeing what prints. The day was always okay before so i never touched it.

The link said “The comma operator chains multiple expressions together, and the result of the operation is the value of the last operand…”

That explained what i was about to ask you. thanks for pointing that out. i would be misusing the , as a shorthand for &&.

I will go through the whole functions lesson again tommorow, the operators will become second nature in time just as basic maths operators.

Thanks again


#12

no, the and operator (&&) means both conditions have to be true:

true && true // true
true && false // false
false && true // false
false && false // false

Part of programming is to check it always works under all possible conditions, so this time it means all the possible combination:

valid day and valid time
valid day and invalid time
invalid day and valid time
invalid day and invalid time

take this with you also for the further


#13

Thats what i meant, i said the && operator means both “parameters” must be true. meaning both conditions of the parameters must be true. is that correct or am i using incorrect terminology?


#14

Incorrect terminology, the parameters are part of the condition. day === 'Wednesday' is a condition, the parameter is part of the condition.


#15

So is ‘day’ a condition and Wednesday a parameter of that condition?


#16

no, using any comparison operator (like ===) makes a condition, day is a (function) parameter.

when you define a function, you can give your function parameters (you did, two parameters named: days and time).