# I'm getting better, but it keeps going to "bullystun."

I think my teacher helped me figure out the right code I needed for randomizing the code. The only problem is that it keeps landing on “bullystun”. Can anybody please help me figure out the problem? Here is what the code looks like:
let bullyattack = [bullydamage, bullycounter, bullystun];

function bullyturn(){
let bullyturn = bullyattack[(Math.floor(Math.random() * bullyattack.length))];
if (bullyturn == 0){
console.log(bullyturn);
bullydamage();
} else {
if (bullyturn == 1){
console.log(bullyturn);
bullycounter();
} else {
bullystun();
console.log(bullyturn);
}
}
}

this code looks like it’s in the middle of a refactor. `bullydamage, bullycounter, bullystun` these are functions, right? the `let bullyturn = ...` line looks like it’s choosing one of them. but the code below looks like it’s from a different version where `bullyturn` is not the chosen function but rather a number 0, 1, or presumably 2. so the function `bullyturn` won’t be equal to 0 nor 1, thus it does `bullystun()` from that final `else`.

if I were to guess, maybe your teacher gave you that line

`````` let bullyturn = bullyattack[(Math.floor(Math.random() * bullyattack.length))];
``````

because obviously that’s how you do it, but without any explanation or even a statement of what it does.

Well actually, he gave me the whole code. Do you know anyway of fixing it?

I’d say just rewrite it yourself in a way that your understand. from scratch, even–it’s only three cases. avoid the bug that’s in the current version. make it consistent, entirely “choose a function at random from a list and run it” or “choose a number at random in a range and branch on it.”

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.