dimanche 16 septembre 2018

Comparison of javascript variables?

I'm new into javascript and struggeling with some basics: comparing strings / integers.

I have the following code, where randomly an index of an array is selected. Afterwards I want to have an if, looking if it's a concrete index.

var a = " Lorem ipselum a"
var b = "Lorem ipselum b"
var c = "Lorem ipselum c"
var values = [a, b, c]
function myfunction() {
   var  index1 =  Math.floor(Math.random() * values.length);
   document.getElementById('span1').innerText = values[index1];
   ...

// if I have  b or c 
if (index1 == 2 || index1 == 1 ) {


}
 else {
}

But the comparison doesn't seem to work properly, it's failing at "index1 == 2". Is it because of math random? I also tried to declare the var outside, but it's still not working




Aucun commentaire:

Enregistrer un commentaire