This article analyzes the use of JavaScript using break's inner layer to jump out of the outer layer. Share it for your reference. The specific analysis is as follows:
Unlike php, JavaScript loops out of break and needs to add additional tags. Take "a hundred money to buy a hundred chickens" as an example (search Zhang Qiujian, a hundred money to buy a hundred chickens). If you need to calculate everything, then the following code:
Copy the code as follows: <html>
<head>
</head>
<body>
<script type="text/javascript">
for(var i = 1;i<=18;i++){
for(var j=1;j<=33;j++){
var k = 100 - i - j;
if((i+j+k)==100 && (5*i + 3*j +k/3) ==100){
document.write("rooster"+i+"; hen"+j+"; chick"+k+";<br />");
}
}
}
</script>
</body>
</html>
If only one answer is needed, break break breaks out of the loop when the first result is calculated:
Copy the code as follows: <html>
<head>
</head>
<body>
<script type="text/javascript">
baiji:
for(var i = 1;i<=18;i++){
for(var j=1;j<=33;j++){
var k = 100 - i - j;
if((i+j+k)==100 && (5*i + 3*j +k/3) ==100){
document.write("rooster"+i+" one, hen"+j+" one, chick "+k+" one;<br />");
break baiji;
}
}
}
</script>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.