Example of% in JavaScript for remainder or modulo

Source: Internet
Author: User
Tags time and seconds

Take the remainder in JS


0%4
0
1%4
1
2%4
2
3%4
3
4%4
0
5%4
1

Example

Gets the day, hour, and minute contained in minutes, based on the number of minutes

function Getcosttime (minutes) {
var day=parseint (minutes/1440);
var hour=parseint (MINUTES/60);
var minute=parseint (minutes%60);
var string_description=day>0?day+ "Day": "";
String_description+=hour>0?hour+ "Hour": "";
String_description+=minute>0?minute+ "Minutes": "";
return string_description;
}

Perform
(function () {
var minutes=1426;
var time=getcosttime (minutes);
alert (time);
})();
Results
23 hours, 46 minutes.

example, JS modulus (for remainder) interlace color

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<TITLE>JS-mode interlace color </title>
<script type= "Text/javascript" >
Window.onload=function ()
{
var Oul=document.getelementbyid (' Ul1 ');
var ali=oul.getelementsbytagname (' Li ');

for (var i=0; i< aLi. length; i++)
{
if (i%2==0)//modulo remainder
{
ali[i].style.background= ' Red ';
}
}
}
</script>
</script><body>
<ul id= "UL1" >
<li>webtall</li>
<li>webtall</li>
<li>webtall</li>
<li>webtall</li>
<li>webtall</li>
</ul>
</body>

Examples of alternating discoloration (% remainder or modulo)

The color of a small instance of interlacing before the first understanding of the remainder (modulo)% operation, this is the remainder of the operation, not division operations.
12 divided by 5=2, the remainder is 2, that is 5*2+2=12, so 12%5=2
7 divided by 3=2, the remainder is 1, that is 3*2+1=7, so 7%3=1
1 divided by 2, but the remainder is 1.
2*0+1=1
/*
Alert (0%1) = = 0
Alert (1%1) = = 0
Alert (2%1) = = 0
Alert (3%1) = = 0
Alert (4%1) = = 0

--no remainder, so 0---


Alert (0%2) = = 0
Alert (1%2) = = 1
Alert (2%2) = = 0

Alert (0%3) = = 0
Alert (1%3) = = 1
Alert (2%3) = = 2
Alert (3%3) = = 0

Alert (0%3) = = 0
Alert (1%3) = = 1
Alert (2%3) = = 2
Alert (3%3) = = 3

Alert (0%4) = = 0
Alert (1%4) = = 1
Alert (2%4) = = 2
Alert (3%4) = = 3
Alert (4%4) = = 0

Alert (0%5) = = 0
Alert (1%5) = = 1
Alert (2%5) = = 2
Alert (3%5) = = 3
Alert (4%5) = = 4
Alert (5%5) = = 0
......   */


small instances of interlaced discoloration


JS:
window.onload=function ()  

    var ali= document.getElementsByTagName (' li '); 
     
   /*
     for (var i=0 i<ali.length; i++)
    {
         if (i%2==0)
        {
             ali[i].style.background= ' red ';
       }
   }
    <--  Complementary methods  -->
    */ 
 
 
  ;
    for (var i=0 i<ali.length; i+=2)  
    { 
         ali[i].style.background= ' red '; 
   }  /*for Loop method */ 
 
}; 


HTML:


<ol>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>


conversion of time and seconds using the mode of taking


Var inum=605;  //sec  
 
//alert (5<=5); //true 
 
Alert ( parseint (INUM/60) + ' min ' +inum%60+ ' seconds ');  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.