0% found this document useful (0 votes)
33 views13 pages

Java Script Control Structures and Conditional Statements

Here are functions to address the exercises: 1. Function to count negative, zeros, positive values in an array using switch: function counter(arr) { let negative = 0; let zero = 0; let positive = 0; for(let i = 0; i < arr.length; i++) { switch(arr[i]) { case arr[i] < 0: negative++; break; case arr[i] == 0: zero++; break; case arr[i] > 0: positive++; break; } } return {negative, zero, positive}; } 2. Function to print first 20 Fibonacci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views13 pages

Java Script Control Structures and Conditional Statements

Here are functions to address the exercises: 1. Function to count negative, zeros, positive values in an array using switch: function counter(arr) { let negative = 0; let zero = 0; let positive = 0; for(let i = 0; i < arr.length; i++) { switch(arr[i]) { case arr[i] < 0: negative++; break; case arr[i] == 0: zero++; break; case arr[i] > 0: positive++; break; } } return {negative, zero, positive}; } 2. Function to print first 20 Fibonacci
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Java

Java Script
Script Control
Control
Structures
Structures and
and
Conditional
Conditional Statements
Statements
Conditional Statements
in JS
• The conditional statements available
in JavaScript for decision making
are:
• if statement
• if...else statement
• if...else if....else statement
• switch statement
if the conditional
statement
<html>
   <body>
     <script type="text/javascript">
        var exforsys = 20
        if (exforsys < 30)
        {         
          document.write("<b>Welcome</b>")
        }
     </script>
   </body>
</html>
If-else statement
<html>
   <body> Output of the above
     <script type="text/javascript"> script as produced in a
        var exforsys = 40 HTML page is shown
if (exforsys < 30) below:
{         
document.write("<b>Welcome</b>")
}
else
{         
document.write("<b>Thank You!</b>")
}
</script>
</body>
</html>
Conditional Statement part-2
if-else-if statement
<html>
   <body>
     <script type="text/javascript">
        var exforsys = 20
        if (exforsys < 5)
        {         
          document.write("<b>Welcome</b>")
}
else if (exforsys > 5 && exforsys < 10)
     {         
document.write("<b>Have a nice day!</b>")
      }
        else
        {         
  document.write("<b>Thank You!</b>")
}
      </script>
   </body>
</html>
switch statement
<html>
   <body>
     <script type="text/javascript">
        var exforsys = 4
        switch (exforsys)
        {
          case 1:
             document.write("<b>Hi!</b>")
       break
          case 2:
             document.write("<b>Welcome</b>")
             break
          case 3:
             document.write("<b>Thank You!</b>")
             break
          default:
             document.write("<b>Have a Great Day!</b>")
       }
      </script>
   </body>
</html>
JavaScript Iterative
Structures - Part I
• Looping can be achieved in JavaScript
using various statements:

• for loop
• while loop
• do..while loop
• for..in loop
For-loop statement
<html>
   <body>
      <script type="text/javascript">
        var exfor=1
        for (exfor=1;exfor<=15;exfor++)
        {
           document.write("Value is: " + exfor)
           document.write("<br />")
        }
      </script>
   </body>
</html>
Note : The break command is used for breaking the loop
and continuing with the execution of the code that follows
after the loop.
The command continue is used for breaking the current
loop and proceeding with the next value.
while-do
<html>
   <body>
      <script type="text/javascript">
         var exfor=1
         while (exfor<=15)
         {
         document.write("Value is: " + exfor)
         document.write("<br />")
         exfor=exfor+1
         }
      </script>
   </body>
</html>
do-while
<html>
   <body>
      <script type="text/javascript">
         var exfor=1
         do
         {
         document.write("Value is: " + exfor)
         document.write("<br />")
         exfor=exfor+1
         }
         while (exfor<1)
      </script>
   </body>
</html>
Arrays in JS
• In JavaScript, arrays are objects that have some special
functionality.
• JavaScript arrays have dynamic length.
• Defining an Array in JavaScript:
Array is defined in JavaScript by making use of the
keyword new.
• General format of defining array in JavaScript is as follows:
var varaiblename = new Array( )
eg. Var your_list=new Array(100);

• Short form of declaring array


var Exforsys=new Array(1,2,”three”,”four”)
(Note:The elements of an array need not have the same type)
Array Example
<html>
<head><title>My Java Script-page</title></head>
<script type="text/javascript">
var num = new Array(10) // definition
var i;
for (i=0; i<5; i++)
{
number=prompt("Enter a number : ","0"); This example
num[i]=number; takes the value
} from key board
document.writeln("The numbers are : "); store it in an
for (i=0; i<5; i++) array and display
{
from the array
document.writeln(num[i]);
}
/script>
<body>
</body>
</html>
Exercise (45 minutes)
• Write a function counter() to return
number of negative, zeros, positive
values in the given array. Use switch
statement
• Write a function to print first 20
Fibonacci numbers.
• Write a function add_digits() to add
the digits of a number
(Eg. 1234(i/p)->1+2+3+4=10(o/p))

You might also like