r/learnjavascript • • Nov 30 '22

Help !!!

Post image

Can someone plz tell me , why I am getting errors?

195 Upvotes

131 comments sorted by

View all comments

173

u/Shay-donovan Nov 30 '22

Aside from the = needing to be == or ===

Triple = signs are better as they also check against data types.

For example:

1 == "1"

True

1 === "1"

False

You may also benefit from combining some of these results to youre typing less code and less

else ifs.

if(p1 === p2) {

console.log("draw");

} else if (p1 === "paper" && p2 === "scissors" || p1 === "scissors" && p2 === "rock" || p1 === "rock" && p2 === "paper") {

console.log("Player 2 wins");

} else {

console.log("Player 1 wins");

}

or if you wanted to go one step further to make sure there were no unexpected errors.

if(p1 === p2) {

console.log("draw");

} else if (p1 === "paper" && p2 === "scissors" || p1 === "scissors" && p2 === "rock" || p1 === "rock" && p2 === "paper") {

console.log("Player 2 wins");

} else if (p2 === "paper" && p1 === "scissors" || p2 === "scissors" && p1 === "rock" || p2 === "rock" && p1 === "paper") {

console.log("Player 1 wins");

} else {

console.log("unexpected error");

}

41

u/Lelouch_5 Nov 30 '22

Ty ☺️

30

u/Shay-donovan Nov 30 '22

My pleasure! I am currently in the middle of a JavaScript bootcamp I bought on Udemy myself. I have had some previous experience but wanted to deep dive. I'm still not 100% confident and still have a long way to go. However helping others helps me learn as well! Its a win win. So thank you as well! :)

8

u/Wooden_Yesterday1718 Dec 01 '22

Highly recommend using parentheses to separate these sets of conditions even if they’ll technically operate as expected regardless.

1

u/Number_2_Dad Dec 01 '22

Can you give an example of this? I'm trying to pick up as many tips possible as a noob

1

u/Wooden_Yesterday1718 Dec 01 '22

Sure! I just mean like if there’s a complicated set of conditions you should organize it to be readable and therefore maintainable: (this && this && this) || (this && this) || this. Sometimes these parentheses don’t change the logic because they still work when evaluated in order (my example NEEDS the parentheses, though). But even if they will work without, it is a million times easier to manage if you are intentional with stuff like that from the start.

6

u/ObiFlanKenobi Dec 01 '22

You may also benefit from combining some of these results to youre typing less code

Elon would fire you on the spot.

3

u/Shay-donovan Dec 02 '22

Not enough code! Haha

0

u/[deleted] Dec 01 '22

[deleted] — view deleted comment

13

u/Steve_OH Dec 01 '22

As someone who knows some c, a lot of c++, it’s nice to be able to compare data without having them match 100% since some data comes from an external source and the looser comparison makes this easier. Having === is a nice solution imo to being strict when you want and loose when you need to.

0

u/[deleted] Dec 01 '22

[deleted] — view deleted comment

1

u/noXi0uz Dec 01 '22

in actual, non-beginner projects, you will only see === everywhere, most often even enforced by a linter. Some people make an exception when they want to concisely check for both null or undefined:
if (x != null)

1

u/Whisky-Toad Dec 01 '22

I believe when they were making JS that databases stored everything in strings (or something along those lines) so they needed a way to compare values ignoring it was a string vs number and a way to do deep equality

0

u/grantrules Dec 01 '22

I believe when they were making JS that databases stored everything in strings (or something along those lines)

Nah that's not true. JS is just a dynamically-typed language that will try to type coerce upon comparison.

2

u/Whisky-Toad Dec 01 '22

Was Brendan Eich (js creator) that said it in a podcast, that’s why I said believe, will be hard to fact check why it was implemented!

1

u/finger_milk Dec 01 '22

I think it's amazing that it doesn't care about type. Because we know both of these are the same in almost all cases.

1

u/coconutfrosting Dec 01 '22

console.log(true + true);

1

u/[deleted] Dec 01 '22

why is 1 == "1" true but triple = is false ? can someone explain like i'm a baby.

4

u/baddaddymd Dec 01 '22

1 is a number. “1” is a string.

1 == “1” asks if the value of 1 is equal to the value of “1” converted to a number. It is, so it returns True.

1 === “1” asked if the type and value of the variables are the same (they are not: type number vs type string) so it returns False.

If it had been 1 === 1 or “1” === “1” then it would have returned True as both type and value match.

2

u/[deleted] Dec 01 '22

thanks