1

Currently learning ES 6 & 7, i wrote a function to mix an array and i wanted to train by "translating" this function to es 6, it seems to not work and i don't find my mistake, can anyone help me ?

const arr = [1, 2, 3, 4, 5]

var mixer = array => {
    for (let i = 0; i < array.length; i++) {
        let randomized = Math.round(Math.random() * array.length)
        [array[randomized], array[i]] = [array[i], array[randomized]]
    }
    return array
}

mixer(arr)

here's the error i receive from google chrome

Uncaught ReferenceError: randomized is not defined at mixer (prog.js:6) at prog.js:11

Thank you for your help :)

  • 3
    Define what exactly a "mixer" does, please? Is that a *shuffle*? – deceze Jun 22 '17 at 08:23
  • Thank you very much Weedoze, i just realise the function isn't working as i expected, sometimes it's telling me undefined like this [1, 4, 2, undefined, undefined, 3, 5] can you help me on this ? edit : yes that's right deceze it's a shuffle, maybe there's already a method to mix an array but i wanted to make a function for it for practicing –  Jun 22 '17 at 08:34
  • @A.S check updated answer – Weedoze Jun 22 '17 at 08:54

2 Answers2

1

Problem A : randomized is not defined

You forgot the ;'s at the end of the lines

Problem B : undefined values

Start iterating by the end of the array

const arr = [1, 2, 3, 4, 5]

var mixer = array => {
    for (let i = array.length; i; i--) {
        let randomized = Math.floor(Math.random() * i);
        [array[i - 1], array[randomized]] = [array[randomized], array[i - 1]];
    }
    return array;//here
}

console.log(mixer(arr))
Community
  • 1
  • 1
Weedoze
  • 12,306
  • 1
  • 32
  • 52
0

TL;DR: Whenever you start a line with ( or [ make sure to end the previous instruction with a semicolon if possible.

I just want to add to Weedoze's answer.
You actually only really need the first semicolon that he suggested.
This can be confusing because semicolons are optional 99% of the time.
It can help a lot to ask yourself what your code would look like if you would put it into one line. Especially when you start a line with ( or [. And as Bergi suggested in the comments, also watch out for lines starting with /, +, - and ` .

For instance take this code:

test = 123
a = test
(b = 456)

It will throw an error saying something like test is not a function. Why? Well, because

a = test
(b = 456)

is equal to

a = test(b = 456)

If you would have written

test = 123
a = test;
(b = 456)

then your code would have been clear and still have the same meaning, even if you put line 2 and 3 on the same line:

a = test;(b = 456)
Forivin
  • 12,200
  • 21
  • 77
  • 171
  • Thank you i understand i knew about the semicolon but from the person i learned javascript, he was never using semicolon so i got the reflex to not use it either and now i see again how important it is –  Jun 22 '17 at 10:16
  • `(` and `[`? Or `/`, `+`, `-` and `\``! – Bergi Jun 22 '17 at 10:41
  • @Bergi True. I never found myself in a situation where I found myself starting a line with one of those, though. Can you think of a scenario where this would happen? – Forivin Jun 22 '17 at 13:34
  • 1
    @Forivin [`\`…\`.split(…)` has happened](https://stackoverflow.com/q/42034763/1048572). Also [IIFEs sometimes start with `+`](https://stackoverflow.com/q/13341698/1048572). And `/…/.exec(…).forEach(…)` isn't unbelievable either. – Bergi Jun 22 '17 at 13:47