-1

I am making a little test that people can use to check their knowledge of hiragana.

It randomly selects 4 hiragana from a 2nd array and one correct spelling of the hiragana.

It looks pretty much exactly as it should with one twist. The correct answer is in the same place every time! The second hiragana always shows as correct. This is the code that I have used to randomise. Thanks in advance for your help!

var first = Math.floor((Math.random() * 46));
var second = Math.floor((Math.random() * 46));
var third = Math.floor((Math.random() * 46));
var fourth = Math.floor((Math.random() * 46));
var selector = Math.floor((Math.random() * 4));
var firstHiragana = hiraganaSet[first][0];
var secondHiragana = hiraganaSet[second][0];
var thirdHiragana = hiraganaSet[third][0];
var fourthHiragana = hiraganaSet[fourth][0];
alert(selector)
if (selector = 0){
    var romaji = hiraganaSet[first][1];
    var romajiData = hiraganaSet[first][0];   
}
else if (selector = 1){
    var romaji = hiraganaSet[second][1];
    var romajiData = hiraganaSet[second][0];
}
else if (selector = 2){
    var romaji = hiraganaSet[third][1];
    var romajiData = hiraganaSet[third][0];
}
else if (selector = 3){
    var romaji = hiraganaSet[fourth][1];
    var romajiData = hiraganaSet[fourth][0];
}

http://jsfiddle.net/jB6cp/1/

GôTô
  • 7,974
  • 3
  • 32
  • 43

1 Answers1

2

Most likely because you're assigning values rather than doing a comparison check:

if (selector = 0)

should be

if (selector == 0)

or

if (selector === 0)

depending on how strict you want your check to be.

= assigns the value to the variable.

== does a comparison for value only.

=== does a comparison for value and type.

There's some more information here.

Community
  • 1
  • 1
Andy
  • 61,948
  • 13
  • 68
  • 95