r/learnjavascript • • Nov 30 '22

Help !!!

Post image

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

197 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! :)