JS practice for the FOR Loop statement

Source: Internet
Author: User

For loop four elements: initial condition, cyclic condition, loop body, state change.

1. for (Var a=i;i<=aa;i++)

{

Loop Body (example sum=sum+i sum is output)

}

Examples

1-20 off, not a penny.

20-30 off 10 points per pass

30-40 off/off 20

40-49 off 30 points per pass

50 off 100 points

Enter the score for the number of levels:

<body>
Using the for nested if language, using if nesting for handling level issues
<input type= "number" id= "Guan"/>
<input type= "button" value= "Input off fraction" onclick= "Shuru ()"/><br/>

For nested IF statements

<input type= "number" id= "Gan"/>
<input type= "button" value= "Enter level view Score" onclick= "ABC ()"/>

If nested for statement
</body>
<script>
function Shuru ()
{
var aa=parsefloat (document.getElementById ("Guan"). Value)
if (aa!= "")
{
if (aa>0&&aa<=50&&aa%1==0)
{
Sum=0
for (Var i=1;i<=aa;i++)
{
if (aa>0&&aa<=20)
{
Sum+=i
}
else if (aa>=21&&aa<=30)
{
if (i<=20)
{
Sum+=i
}
Else
{
sum+=10
}
}
else if (aa>=31&&aa<=40)
{
if (i<=20)
{
Sum+=i
}
else if (i<30)
{
sum+=10
}
Else
{
Sum+=20
}
}
else if (aa<=49&&aa>=41)
{
if (i<=20)
{
Sum+=i
}
else if (i<30)
{
sum+=10
}
else if (i<40)
{
Sum+=20
}
Else
{
Sum+=30
}

}
Else
{
sum==880
}
}
Alert (SUM)
}
Else
{
Alert ("Please enter the correct level")
}
}
}


function ABC ()
{
var aa=parsefloat (document.getElementById ("Gan"). Value);
if (aa!= "")
{
if (aa>0&&aa<=50&&aa%1==0)
{
if (aa>0&&aa<=20)
{
var sum=0
for (Var i=1;i<=aa;i++)
{
Sum+=i
}
Alert (SUM)
}
else if (aa>=21&&aa<=30)
{
var sum=210
for (Var i=21;i<=aa;i++)
{
sum+=10
}
Alert (SUM)
}
else if (aa>=31&&aa<=40)
{
var sum=310
for (Var i=31;i<=aa;i++)
{
Sum+=20
}
Alert (SUM)
}
else if (aa>=41&&aa<=49)
{
var sum=510
for (Var i=41;i<=aa;i++)
{
Sum+=30
}
Alert (SUM)
}
Else
{
Alert (880)
}
}
Else
{
Alert ("Level input error")
}
}
}
</script>

JS practice for the FOR Loop statement

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.