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
}

fds

Love it? Share it!

DevCanvas DevCanvas Logo

Online Editor with a collection of awesome frontend code and code snippets for developers of all levels.

Legal & Support

Stand with Palestine 🇵🇸! DO NOT BE SILENCED

© 2025 DevCanvas. All rights reserved.