js Chapter 2 Basic Grammar Cases: Sum of odd numbers within 100, Print Pyramids, Nine-Nine Multiplication Table, Calculate Circle Perimeter and Area

1. Sum of odd numbers within 100

<script>
  var i = sum = 0;
  while (i <= 100) {
    if (i % 2) {
      sum += i;
    }
    i++;
  }
  console.log('100 Sum of odd numbers within:' + sum);
</script>

2. Print pyramids.

Print a pyramid-shaped graphic using circular statements and conditional judgment statements, consisting of a triangle of spaces and stars'*'. Suppose the topmost   If a star is the first layer of the pyramid and the number of stars in each layer is odd, two rules can be drawn.

(1) The number of stars in each layer = the current number of layers *2-1. For example, if the current level is Level 4, the number of stars is 4*2-1=7.

(2)   Spaces in front of stars per layer = Pyramid Layers - Current Layers. For example, if the current row number is Layer 3, the number of spaces = 5-3 = 2.

<script>
  var level = prompt('Please set the number of layers of the pyramid');
  // Get the pure number of the input, and the rest goes to NaN
  level = parseFloat(level) && Number(level);
  // Determine whether user-entered data is legal
  if (isNaN(level)) {
    alert('Pyramid levels must be pure numbers');
  }
  // Number of layers to cycle through the pyramid
  for(var i = 1; i <= level; ++i){  
     // Output space in front of stars
    var blank = level - i;
    for(var k=0; k < blank; ++k){
      document.write('&nbsp;');
    }
    // Print Stars 
    var star = i*2 - 1;
    for(var j = 0; j < star; ++j){
      document.write('*');
    }
     // Line Break
    document.write('<br>');
  }
</script>

Operation effect:

 

 

3.99 Multiplication Table.

The Nine-Nine Multiplication Table consists of a table similar to a triangle (also known as a staircase step) and a multiplication operation. Assuming the top layer is the first layer of the multiplication table, you can get the distribution of the table as follows.
(1) The table for the 99-th multiplication table consists of nine rows of cells with a maximum of nine columns in each row
(2) The number of layers in the multiplication table = the number of rows in the table = the number of columns in each row. For example, the third layer of the multiplication table is the third row of the table and has three cells.
After analyzing the structure and layout of the Nine-Nine Multiplication Table, we will continue to analyze the rules of multiplication operation, as shown below.
(1) The multiplier ranges from 1 to the number of columns in each row. For example, the multiplier in row 3 of the table has a value between 1 and 3.
(2) The value of the multiplier = the number of rows in the table. For example, the multiplier in row 3 of the table has a value of 3.
Next, the implementation of the Nine-Nine Multiplication Table will be completed based on the above analysis.  

Style:

<style>
      table{border-collapse:collapse;}
      table td{border:1px solid #ccc;padding:3px 6px;}
</style>
<body>
    <div id="table"></div>
    <script>
      var str = '<table>';
      for (var i = 1; i < 10; ++i) {    // Traverse all rows in the table
        str += '<tr>';
        for (var j = 1; j <= i; ++j) {  // Traverse columns in each row
          // Stitching Cells
          str += '<td>' + j + '×' + i + '=' + (j * i) + '</td>';
        }
        str += '</tr>';
      }
      str += '</table>';
      // Display stitched strings on the page
      document.getElementById('table').innerHTML = str;
   </script>
</body>

Operation effect:

  4. Calculate the perimeter and area of a circle.

<body>
    <div>
      <p>The radius of the circle:<input id="r" type="text"></p>
      <p>Circumference of a circle:<input id="cir" type="text"></p>
      <p>Area of circle:<input id="area" type="text"></p>
    </div>
    <script>
      var r = prompt('Please enter the radius of the circle');
      r = parseFloat(r) && Number(r);   // Get the pure number of the input, and the rest goes to NaN
      if (!isNaN(r)) {                  // Determine if the user's input is a number
        var cir = 2 * Math.PI * r;
        var area = Math.PI * r * r;
        document.getElementById('r').value = r;
        document.getElementById('cir').value = cir.toFixed(2);
        document.getElementById('area').value = area.toFixed(2);
      } else {
        alert('Please enter the correct number');
      }
    </script>
</body>

Operation effect:

 

 

Keywords: Javascript Front-end Vue.js html

Added by stbalaji2u on Fri, 03 Dec 2021 19:31:53 +0200