# Java script code check

**URL:** <https://forum.kirupa.com/t/java-script-code-check/259030>\
**Category:** Uncategorized\
**Created:** [April 30, 2008, 3:24pm UTC](https://forum.kirupa.com/t/java-script-code-check/259030 "2008-04-30T15:24:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navarone](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@Navarone](https://forum.kirupa.com/u/Navarone)\
**Post date:** [April 30, 2008, 3:24pm UTC](https://forum.kirupa.com/t/java-script-code-check/259030/1 "2008-04-30T15:24:48Z")

</div>

Can some one tell me if my variable “mr” should be compared to a string or a number? I am using math.floor(math.random) formula. Also should I be using else if, instead of just if.

Thanks

```auto

var curcontentindex=0
var messages=new Array()
function getElementByClass(classname){
 var inc=0
 var alltags=document.all? document.all : document.getElementsByTagName("*")
 for (i=0; i<alltags.length; i++){
  if (alltags*.className==classname)
  messages[inc++]=alltags*
 }
}
function rotatecontent(){
 //get current message index (to show it):
 curcontentindex=0;    
 //set random to display groups of pics
 var mr = Math.floor(Math.random()*4);  
 if (mr=="0"){
  curcontentindex=0;
  //alert("currentindex=0");
  prevcontentindex=0;
 }
  if(mr=="1"){
  curcontentindex=1;
  //alert("currentindex=1");
  prevcontentindex=0;
 }
  if(mr=="2"){
  curcontentindex=2; 
  //alert("currentindex=2");
  prevcontentindex=1;
 }
 if(mr=="3"){
  curcontentindex=3; 
  //alert("currentindex=3");
  prevcontentindex=2;
 }    
 messages[prevcontentindex].style.display="none" //hide previous message
 messages[curcontentindex].style.display="block" //show current message
}

```

---

<div class="post-metadata">

**Author:** ![DangerousDan](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@DangerousDan](https://forum.kirupa.com/u/DangerousDan)\
**Post date:** [May 9, 2008, 1:04pm UTC](https://forum.kirupa.com/t/java-script-code-check/259030/2 "2008-05-09T13:04:32Z")

</div>

I’m pretty sure it should be an integer (number).

---

<div class="post-metadata">

**Author:** ![jwilliam](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jwilliam](https://forum.kirupa.com/u/jwilliam)\
**Post date:** [May 9, 2008, 8:06pm UTC](https://forum.kirupa.com/t/java-script-code-check/259030/3 "2008-05-09T20:06:04Z")

</div>

The floor() method returns an Integer, yes.

```auto

if(mr == 0) {

} else if(mr == 1) {

} else if(mr == 2) {

} else if(mr == 3) {

}

```

is more efficient than

```auto

if(mr == 0) {

}

if(mr == 1) {

}

if(mr == 2) {

}

if(mr == 3) {

}

```

However, this is dependent on the value of mr. If mr is 0, then the first example above will stop after the first ‘if’ statement. In other words, if the first if statement results to true, then none of the ‘else if’ statements afterwards can possibly be true so the interpreter doesn’t even bother to evaluate them.

In the second example (your code) it evaluates every if statement regardless of whether or not the value of mr has already been determined. Thus, if mr is 0 then the first code block is roughly four times more efficient. If mr is 3 then there is no difference.

Hope this helps…

---

<div class="post-metadata">

**Author:** ![kdd](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@kdd](https://forum.kirupa.com/u/kdd)\
**Post date:** [May 11, 2008, 1:30am UTC](https://forum.kirupa.com/t/java-script-code-check/259030/4 "2008-05-11T01:30:03Z")

</div>

jwilliam is correct! You should use "else if" UNLESS you are doing something like this:

```auto

if ( mr == 1 )
{
mr = 2;
//other code...
}
if ( mr == 2 )
{
//some other code...
}

```

Unusual, but it depends on your application. And here you NEED multiple if statements, and not else-if statements.  
Also, just in case you didn’t know, such questions go in the Client-Side sub-forum we have, because Java and JavaScript are different. 🙂
