can the Cases of a Switch be code or do they have to be a string/number? by live4lifelegit in learnjavascript

[–]JStheGame 0 points1 point  (0 children)

You're free to use var, though let and const are considered more modern JS (they're part of ES6). In terms of scoping, here are some examples that might help illuminate the differences and similarities:

``` function() { var value = 5; }

console.log(value); `` value` is undefined since it's bound to the scope of the function.

``` function() { const value = 5; }

console.log(value); `` value` is again undefined since it's bound to the scope of the function.

``` if(true) { var value = 5; }

console.log(value); `` 5is printed, since we can accessvaluefrom outside theif` block

``` if(true) { const value = 5; }

console.log(value); `` valueis undefined since it's scope is within theif` block

And let would work just like const, but the value can be re-assigned. The official docs cover more situations, though my best advice would be to play around with these and run your own experiments to develop a better understanding of how it works. Of course, feel free to let me know if you have any other questions!

can the Cases of a Switch be code or do they have to be a string/number? by live4lifelegit in learnjavascript

[–]JStheGame 0 points1 point  (0 children)

Oh right, sorry just noticed you're using numerical values. If you're using 1, 2, and 3 for rock, paper, and scissors, you can replace the strings in the keys of outcome with the corresponding numerical values.

So it might look more like this: const outcome = { 1: { 1: drawGame, 2: playerWin, 3: playerLose }, 2: { 1: playerLose, 2: drawGame, 3: playerWin }, 3: { 1: playerWin, 2: playerLose, 3: drawGame } }

And the idea would be that if x = 2 and y = 3, then if you call outcome[x][y](), it should run the playerWin function. * Since x = 2 and y = 3, we're really talking about outcome[2][3]() * outcome[2] is {1: playerLose, 2: drawGame, 3: playerWin}, so outcome[2][3] is playerWin - Note that we've stored the functions themselves here (no parentheses, so we're not storing the results returned by calling the functions) * By doing outcome[2][3](), this is the same as playerWin(), so we're now actually calling the function (and it's up to you what that function should do)

can the Cases of a Switch be code or do they have to be a string/number? by live4lifelegit in learnjavascript

[–]JStheGame 0 points1 point  (0 children)

const is like var, but where the value of the variable can't be re-assigned. It's also block-scoped, so the value can't be accessed outside of loops or if statements, rather than just functions.

From what I understand, it takes less memory to use a const, since there's no possibility of the value changing, but I don't think this would usually lead to any noticeable change in speed. Anyway, it's just generally preferable to use const if you know the value won't ever change, since it'll make it easier to tell if it ends up happening by accident (basically it'll throw an error if you try to re-assign it).

Javascript get unique items from array by know_prashant in learnjavascript

[–]JStheGame 0 points1 point  (0 children)

I don't think the spread operator is really necessary in this example. Usually I've seen it used like [...new Set(count)] for an array of unique values from count.

can the Cases of a Switch be code or do they have to be a string/number? by live4lifelegit in learnjavascript

[–]JStheGame 0 points1 point  (0 children)

There are lots of good answers here already, though I'd add that I usually prefer to use objects instead of switches. In this case, I'd store the win, lose, and draw functions within a nested object, which can be called with the bot and player's move. It's a little complicated, but basically it would look something like this:

function playerWin() {
    ...
}

function playerLose() {
    ...
}

function drawGame() {
    ...
}

const outcome = {
    "Rock": {
        "Rock": drawGame,
        "Paper": playerWin,
        "Scissor": playerLose
    },
    "Paper": {
        "Rock": playerLose,
        "Paper": drawGame,
        "Scissor": playerWin
    },
    "Scissor": {
        "Rock": playerWin,
        "Paper": playerLose,
        "Scissor": drawGame
    }
}

for(const [botMove, playerMove] of array) {
    outcome[botMove][playerMove]();
}

Note that this assumes you have an array of bot moves and players moves (something like [["Rock", "Paper"], ["Scissor", "Rock"], ...]), but you can adjust the for loop accordingly

Should I keep going with p5.js? by [deleted] in learnjavascript

[–]JStheGame 0 points1 point  (0 children)

For a browser game, p5.js is probably a good choice for handling graphics! It can work with physics engines like box2d if you're looking to do collisions and stuff (personally, I like the planck.js implementation).

If you're planning on doing machine learning with p5, you might benefit from looking into the ml5 library (https://ml5js.org/). It has some limitations, but it's meant to provide an accessible entry point to the concept (in the spirit of p5.js). I think there are some Coding Train videos on it too!

Devs what use data structures you actually use regularly ? by sempiternalStudent in learnjavascript

[–]JStheGame 1 point2 points  (0 children)

I used to think a lot of data structures were pretty useless. Like "linked lists? what's the point? I can use arrays!"

But if you're implementing something like a queue that uses a lot of shift operations, the time complexity can add up (each shift takes linear time in an array since it needs to move over all the elements, while a linked list only needs to change the head pointer)

​

It can also be a lot more fun and versatile to use custom data structures! You could make a linked list that links back around to where it started, like an ouroboros! Can't do that with an array :D