instructional systems
Index:
[Session12] JavaScript 2 (Basic syntax, control structure (if, for, etc.))
1 Chapter1
2 Chapter2
3 Chapter3
4 Chapter4
5 Chapter5
Your Location: Home Page  >  [3] Interactive web pages using JavaScript (on condition)  >  [Session12]JavaScript 2 (Basic syntax, control structure (if, for, etc.))  >  Chapter2  >  2.2 Write the JavaScript Programming on Your Own (2)
Write the JavaScript Programming on Your Own (2)
[The purpose of this section]
To examine the actual programming to understand JavaScript more deeply

In this section, we will confirm what differences are created by examining the examples of actual programming. Specifically we will focus on the program to display only the numbers that meet a particular condition.

Program (1) - Displaying only when k is 5

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</head>
<body>

<script language=JavaScript>
  for(k = 1;  k < 26;  k = k + 1){
    if (k == 5){
      document.write(k);
      document.write("<br>");
    }
  }
</script>

</body>
</html>
if (condition){ ... }

executes ... only when condition is met. In this case the condition is k == 5.
“==“ means the comparison of being equal or not. (Note that “==“ is written by “=“ twice. You may see the two as if they are continuous.

[Note] Is the following way correct?

 5 == k

→  This is allowed.

Program (2) - Displaying only when k is not 5

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</head>
<body>

<script language=JavaScript>
  for(k = 1;  k < 26;  k = k + 1){
    if (k != 5){
      document.write(k);
      document.write("<br>");
    }
  }
</script>

</body>
</html>

The meaning of “!=“ is the opposite of “= =“. (This is also called denial.)

Program (3) - Displaying the k divided by 4

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</head>
<body>

<script language=JavaScript>
  for(k = 1;  k < 26;  k = k + 1){
    m = k/4;
    document.write(m);
    document.write("<br>");
  }
</script>

</body>
</html>

The symbol (or operator) “/” means division.
In this program, the value of k is not displayed directly but is divided by 4 first, and then the result is put into m, which is displayed.

[Exercise 4] The operator expressing multiplication is “*”. Create a program to display the squares of 1 through 25, such as 1, 4, 9, 16, 25, 36, 49, and so on.

→ Exercise 4 :  [Check the result by the browser]  [See HTML tag]

Program (4)- Displaying the remainder of k divided by 4

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</head>
<body>

<script language=JavaScript>
  for(k = 1;  k < 26;  k = k + 1){
    m = k%4;
    document.write(m);
    document.write("<br>");
  }
</script>

</body>
</html>

The operator “%” has some special meaning to be used to obtain the remainder of division.

As you can see from the result, the calculation result goes like 1, 2, 3, 0, 1, ...and so on; you can see that every time the number is divisible by 4 you have the remainder 0.

Program (5) - Brush-up of this section

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</head>
<body>

<script language=JavaScript>
  for(k = 1;  k < 26;  k = k + 1){
    m = k%4;
    if (m == 0){
      document.write(k);
      document.write("<br>");
    }
  }
</script>

</body>
</html>

Consider how this program behaves.

→ What is displayed is the value of k, but is conditioned to provide only those values up to 26 that are divisible by 4 (with the remainder 0).

If you have all the knowledge up to here, you should be able to create the program of task 12 (combining the knowledge so far).

[Some tips for those who are interested in other programming languages]
To write a program behaving the same as the last prog5 above in the forms of C, Perl, Java, and FORTRAN,
see Chapter 4 4.1 Programming samples of languages other than JavaScript.

Copyright (c) Toshihiro Kita, Hideki Matsuda, Chisato Noguchi and Yuriko Shimizu 2002-2006, All Rights Reserved