WebDev001
WebDev001

Reputation: 99

Scrambling a word in javascript

I want to make a system which scrambles word, example: Hello -> llHoe. The problem is that it sometimes shows undefined in the console and I can't find any problems.

This is the code I've made:

function GenerateWord() {
    var Words = ["Hello", "Bye", "Tree"]
    var RandomNumber = Math.floor((Math.random() * Words.length));
    var CorrectAwnserString = Words[RandomNumber];
    var CorrectAwnser = CorrectAwnserString.split("");
    var WordToOrder = "";

    for(i = CorrectAwnser.length; i > 0;) {
        let RandomLetter = Math.floor((Math.random() * i));
        WordToOrder = WordToOrder + CorrectAwnser[RandomLetter];
        console.log(WordToOrder)
        CorrectAwnser.splice(RandomLetter);
        console.log(CorrectAwnser)
        i = i - 1;
    }

    document.getElementById("OriginalWord").innerHTML = CorrectAwnserString;
    document.getElementById("MessedWord").innerHTML = WordToOrder;
}
GenerateWord();
<div id="OriginalWord"></div>
<div id="MessedWord"></div>

Upvotes: 2

Views: 106

Answers (3)

Sanmeet
Sanmeet

Reputation: 1410

You can use String.prototype method as it will return an array of the string ( Cool huh no need to generate one ! ) . Also to randomise the string characters you can use Math.random function in a specific way ...

See =>

String.prototype.randomize = function() {
  let array = Array.from(this).map((val, index) => this[index]);
  array.sort(() =>  0.5 - Math.random());
  const string = array.reduce((string, word)  =>  string += word, "");
  return string;
};

let str = "Overflow";
str = str.randomize();
console.log(str);

You can use this method on any string datatype.. so I think you can handle it now

Upvotes: 0

Piash
Piash

Reputation: 1131

If you write CorrectAwnser.splice(2), it will remove 2 elements, not the 2nd element. That's why your splicing is removing more than one element, and sometimes the CorrectAwnser becomes empty before your loop ends.

You should write

CorrectAwnser.splice(RandomLetter, 1)

Upvotes: 1

Blunderchips
Blunderchips

Reputation: 564

The error seems to be with how you are splicing the string. You need to specify how many elements should be removed. I shuffled your code around a little and stuck it in some more console logs it looks like this:

for(i = CorrectAwnser.length; i > 0;) {
    let RandomLetter = Math.floor((Math.random() * i));
    WordToOrder = WordToOrder + CorrectAwnser[RandomLetter];
    console.log(CorrectAwnser,RandomLetter,CorrectAwnser[RandomLetter]);
    console.log(WordToOrder)
    CorrectAwnser.splice(RandomLetter); // should be CorrectAwnser.splice(RandomLetter, 1); 
    i = i - 1;
}
console.log({CorrectAwnserString,WordToOrder});

The code above gave the following result:

[ 'T', 'r', 'e', 'e' ] 1 r
r
[ 'T' ] 0 T
rT
[] 0 undefined
rTundefined
[] 0 undefined
{ CorrectAwnserString: 'Tree', WordToOrder: 'rTundefinedundefined' }

After updating the splice statement to CorrectAwnser.splice(RandomLetter, 1); the logs are the following:

[ 'T', 'r', 'e', 'e' ] 3 e
e
[ 'T', 'r', 'e' ] 0 T
eT
[ 'r', 'e' ] 0 r
eTr
[ 'e' ] 0 e
eTre
{ CorrectAwnserString: 'Tree', WordToOrder: 'eTre' }

Upvotes: 2

Related Questions