Scramble a string - Javascript

Shared by: devcanvas

javascript

1
function scrambleWord(word) {
2
 const wordArray = word.split("");
3
 for (let i = wordArray.length - 1; i > 0; i--) {
4
  const j = Math.floor(Math.random() * (i + 1));
5
  [wordArray[i], wordArray[j]] = [wordArray[j], wordArray[i]];
6
 }
7
 return wordArray.join("");
8
}