2

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>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
WebDev001
  • 91
  • 1
  • 1
  • 6
  • Could this be what you are after? https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array – moo Jul 04 '21 at 15:44
  • Does this answer your question? [Looping through array and removing items, without breaking for loop](https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop) – Hassan Imam Jul 04 '21 at 15:48
  • The OP wants to know why *their code* is not working, not how to copy/paste something to achieve the goal. – nicholaswmin Jul 04 '21 at 15:49
  • Should'nt it be CorrectAwnser.splice(RandomLetter, 1); ? – Adam P. Jul 04 '21 at 15:58

3 Answers3

2

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' }
Blunderchips
  • 534
  • 4
  • 22
1

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)
Nazem Mahmud Piash
  • 1,053
  • 1
  • 13
  • 34
0

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

Sanmeet
  • 1,191
  • 1
  • 7
  • 15
  • @WebDev001 I think you are writing too much of unnecessary code . This will make your code dry a lot !.. hope it helps you – Sanmeet Jul 04 '21 at 17:29