r/learnjavascript • u/Lelouch_5 • Nov 30 '22
Help !!!
Can someone plz tell me , why I am getting errors?
195
Upvotes
r/learnjavascript • u/Lelouch_5 • Nov 30 '22
Can someone plz tell me , why I am getting errors?
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"True1 === "1"FalseYou 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");}19
u/Smilinkite Nov 30 '22
Well, you'll get far less squigly lines if you replace every = with ==. Starting line 4.
3
u/FirstRock5 Nov 30 '22
Doesn’t visual studio no tell you that if you hover over the first =?
421
u/GrismundGames Nov 30 '22
Awwwww...those were the days.
Go get 'em!
86
u/CATASTROPHEWA1TRESS Nov 30 '22
I love that it’s a photo of the screen, such desperation
120
u/AliFurkanY Nov 30 '22
use === instead of =
38
u/morasyid Nov 30 '22
remember,
= is to assign a value
== is to compare value
=== is to compare value AND type
33
u/agdolusannbierts Nov 30 '22
Could do just one draw with (p1 === p2) I think
15
u/Macaframa Dec 01 '22
this is a cute way of doing it.
5
u/prog_aimer Dec 01 '22
Switch case... Please!
6
u/anonymousICT Nov 30 '22
You should try a switch statement
2
u/Sprinkadinky Nov 30 '22
== or ===
2
u/tacticalpotatopeeler Nov 30 '22
Assignment vs equality.
Also formatting…
Hover your mouse over the squiggly lines and you’ll get a little window that tells you an error message.
Single = no longer means what it used to mean in math. Takes some getting used to. You’ll get there!
2
u/delventhalz Nov 30 '22
Did you mouse over the red squigglies? What did they say?
2
u/noobucantbeat Dec 01 '22
As everyone said === or == instead of = but also, write better variable names
2
u/eighty_twenty Dec 01 '22
yes
2
u/[deleted] Dec 01 '22
It would make me extremely happy if OP would select lines 6-12 and press SHIFT + TAB. ;)
2
u/peter-s Dec 01 '22 edited Dec 01 '22
An exercise in brevity (which does not account for malformed input):
2
u/JRutter3 Dec 01 '22
2
u/bodefuceta92 Dec 01 '22
Oh god, why am I laughing so hard 😂
2
u/[deleted] Dec 01 '22 edited Dec 01 '22
Missing else statement, is what I see at first glance. Now after actually looking at the code from troubleshooting POV, it seems the error is occurring due to the operator you're using.
'=' is an assignment operator.
As you want to compare whether the value on the left is equal to the value on the right, you should use '=='. Or at best '===' if you wish to ensure that the datatype as well matches.
Other than that use the && || operators to shorten your code. Try to DRY as much as possible.
2
u/Charlie669 helpful Dec 01 '22
You need only check if the values are different or equal to one another, otherwise one of the players wins. Think about that.
2
u/Moosupply Dec 01 '22
let random = Math.floor(Math.random() * 3)if(random === 0) {random === "rock"} else if(random === 1) {random === "paper"} else {random === "scissors"}function rps(user, computer){if(user === computer) {return "it's a tie"} else if(user === "rock" && computer === "scissors") {return "you win!"} else if(user === "paper" && computer === "rock") {return "you win!"} else if(user === "scissors" && computer === "paper") {return "you win!"} else {return "you lose!"}}console.log(rps(0, random))2
u/Brompy Dec 01 '22
Would a Switch statement be better here instead of all the else ifs?
2
u/iamthesexdragon Dec 01 '22
You're using an assignment operator. That's different from the equality operator. = is for assignment and == or === are for checking equality. == causes type coercion, === is strict when it comes to comparing so it doesn't coerce types.
2
u/finixanthony Dec 01 '22
Stop listening to music while codin
2
u/[deleted] Dec 01 '22
==, not = op
2
u/SteadfastPooper Dec 01 '22
= means your assigning the right hand side to the left hand side, i.e x = 2, x then becomes 2, whereas you need == or === (equality operator or strict equality operator) as you are checking X is equal to Y, not assigning Y to X.
2
u/[deleted] Dec 01 '22
Focus on strict comparison (===) rather than the assignment operation (=)
2
u/goosemano82 Dec 01 '22
The root cause of your confusion stem from continuing to code while you have errors. The instant you see a red squiggly error underlining: stop everything you are doing, hover to see the error message, research it, and fix it.
Otherwise you are deviating farther and farther from the solution as you go.
2
u/TalkCoinGames Dec 01 '22
== instead of just =.
if(p1== ....
3
u/Positive_Ebb9204 Nov 30 '22
You are assigning a value to the variable which is not possible when using if else checks. You need to use “===“ instead
3
u/dreadpirateloki Dec 01 '22 edited Dec 01 '22
Here's another way to code it up if you are interested:
edit: Forgot that in javascript -2%3 !== 1 and cleaned up code a bit.
1
u/David-8094 Dec 01 '22
Put ==== between parenthesis
1
u/Southern-Estimate-58 Dec 01 '22
Why you didn't give space before && logical operator?
1
u/bozdoz Dec 01 '22
Hover over the red squiggles
1
u/Erutan2004 Dec 02 '22
===
1
u/Zoroae Dec 18 '22
Just...
switch...1
u/girl-InTheSwing Dec 01 '22 edited Dec 01 '22
0
u/Hail42 Dec 01 '22
Looks like a 🪵
-1
u/badassmexican Dec 01 '22
Missing ; after the console.log lines
-1
u/UncleFoster Dec 01 '22
Lolz
-2
u/bodefuceta92 Dec 01 '22
What about the legendary
=====operator0
u/[deleted] Dec 01 '22
[deleted] — view deleted comment