4 Level Nested for loop in Javascript
4 Level Nested for loop in Javascript is the today topic of discussion in our tutorial. Suppose we are asked to write the four-level nested for loop program that displays the following stars;
*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%
4 Level Nested for loop Program in Javascript
><html> <head> <title>Demonstration of 4 LEVEL NESTED FOR LOOP</title></head> <body> <center><h1> NESTED LOOP OF * </h1> <button onclick="FourLevelNestedLoop()">Please click here</button></center> </body> <script language="javascript"> function FourLevelNestedLoop() { var i; var j; var q; var k; for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(q=0;q<2;q++) { for(k=0;k<2;k++) { document.write("*****"); document.write("%*%"); } } } } } </script> </html>
Algorithm of 4 Level Nested for loop in Javascript
- Start
- Add<html>tag
- Add <head> tag
- Add<title> tags in between <head> tags
- Write the name of the title
- Close<head> tag
- Start <body>
- Give heading in <h1> tags
- Make a button
- End< body> tag
- Start <script> tag
- Make a function that calls on the button
- Declare variables in function as i,j,q,k
- Set Four loops to make it 4 levels nested for loop
- Place every loop in the body of the first loop
- Give command of printing *****and%*% according to conditions of the loop
- End </script>
- End </html>
Output
*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%*****%*%