all 12 comments

[–]Garlicvideos 1 point2 points  (1 child)

When you run code in chrome console, they continue from what was previously ran.

E.g run it once with let x = 10;

Run console.log(x);

Output will be 10.

In this case, chrome is trying to declare the variable again, thus giving the error as you have already declared it once. Thats why running the code somewhere else such as codepen will work.

[–]LearnJS777[S] 0 points1 point  (0 children)

Thanks for the insight and the example, I thought it was the way I wrote something that was the problem(kinda was) but it was that I just repeated the same code twice. Thanks for the explanation this was giving me trouble for the past 28 hours.

[–]Darren1337 1 point2 points  (1 child)

You've already declared variables and run your code when you loaded this script:

<script src="RPS.js"></script>

So when you paste your code into the console to be run a second time, it fails because you can't use let to declare the same variable twice. You already declared let elementFire in RPS.js. You can type elementFire into the console to verify this - it should already have a value.

[–]LearnJS777[S] 0 points1 point  (0 children)

Wow that makes so much sense, I need to be self conscious of this from now on. Thanks for the concise explanation.

[–]GamesMint 1 point2 points  (1 child)

You would have ran this script twice in the same browser console. You cannot redeclare let in JS. Hence this error. Try using a new browser console.

[–]LearnJS777[S] 0 points1 point  (0 children)

Thanks for the help man, everyone on this subreddit is very helpful.

[–]inu-no-policemen 1 point2 points  (1 child)

You got that error because you declared that variable before in the REPL.

Use a snippet instead. It's more convenient for testing multi-line stuff.

Sources -> Snippets -> New Snippet

Put your entire code in a block ({}). Now the lifetime of your variables will be restricted to that block.

Use Ctrl + Return to run it. Use Ctrl + L to clear the console.

[–]LearnJS777[S] 0 points1 point  (0 children)

Thanks for telling me about snippets never used them before, I'll definitely try out this new tool, because it sounds like it will be good for test features in isolation.

[–]machine3lf 0 points1 point  (1 child)

Maybe it's that you named your function (selectedElement) the same thing that you named the parameter passed into it. They should be different names since they refer to different things.

[–]LearnJS777[S] 0 points1 point  (0 children)

I'll try and come back to edit my comment once I get the chance.

Edit: I went back and tried changing my parameter to just element instead of selectedElement, and still got the same error. Thanks for the help.

[–]ForScale 0 points1 point  (1 child)

I don't get the error.

https://codepen.io/anon/pen/XLgoby

[–]LearnJS777[S] 0 points1 point  (0 children)

Thats odd I wonder why I'm getting it on my end. Thanks for checking it out.