Jay Huang
Jay Huang

Reputation: 21

how to add if..else statement in my code

In my code, how can I add the if statement so it print different statements depend on different situtation. for example, I like to have a statement for the sum < 1, and else sum > 1. I try w3 school but the example if statement does not work for some reason

<!DOCTYPE html>
<!-- Network Latency Calculator -->
<html>
<head>
  <meta charset = "utf-8">
  <title>Network Latency Calculation</title>
  <script>

     var firstNumber; // first string entered by user
     var secondNumber; // second string entered by user
     var thirdNumber; // third string entered by user
     var fourthNumber; // fourth string entered by user
     var number1; // first number to add
     var number2; // second number to add
     var number3; // third number to add
     var number4; // fourth number to add
     var sum; // sum of number1 and number2 and number3 and number4



     // read in first number from user as a string
     firstNumber = window.prompt( "Enter the Propagation time  (in milliseconds)" );

     // read in second number from user as a string
    secondNumber = window.prompt( "Enter the Transmission time  (in milliseconds)" );

    // read in third number from user as a string
     thirdNumber = window.prompt( "Enter the Queuing time  (in milliseconds)" );

    // read in fourth number from user as a string
     fourthNumber = window.prompt( "Enter the Propagation delay  (in milliseconds)" );

     // convert numbers from strings to integers
     number1 = parseInt( firstNumber ); 
     number2 = parseInt( secondNumber );
     number3 = parseInt( thirdNumber );
     number4 = parseInt( fourthNumber );

     sum = number1 + number2 + number3 + number4; // add the numbers





     // display the results
     document.writeln( "<h1>The network latency is " + sum + "</h1>" );



  </script>

Upvotes: 0

Views: 2251

Answers (3)

Phara0h
Phara0h

Reputation: 19

First of all you might want to look here: https://www.w3schools.com/jsref/met_doc_writeln.asp

Right now you writing in the head of the html not in the body

<body>

<p>Note that write() does NOT add a new line after each statement:</p>

<pre>
<script>
var NowDate = new Date();
var number1 = NowDate.getHours(); //added current hour 0-23
var number2 = 5; // second number to add
var number3 = 0.3; // third number to add
var sum = number1+number2*number3;
if (sum > 5){
    document.write("That's a");
    document.write(" big Sum ("+sum+")");
} else if (sum === 4) {
    document.write("Sum =");
    document.write(" 4");
}else{
    document.write("Sum is ");
    document.write("small ("+sum+")");
}
</script>
</pre>


<p>Note that writeln() add a new line after each statement:</p>

<pre>
<script>
document.writeln("Hello World!");
document.writeln("Have a nice day!");
</script>
</pre>

</body>

Upvotes: 1

slevy1
slevy1

Reputation: 3832

After you get the sum, you could add an if-statement similar to this code if you so desire:

if (sum < 1) {
   document.write ("The sum is less than one");
} else if (sum > 1) {
   document.write( "The sum is more than one"); 
}

If you have more questions about if-conditional statements O'Reilly has a number of excellent technical books dealing with JavaScript.

var firstNumber; // first string entered by user
 var secondNumber; // second string entered by user
 var thirdNumber; // third string entered by user
 var fourthNumber; // fourth string entered by user
 var number1; // first number to add
 var number2; // second number to add
 var number3; // third number to add
 var number4; // fourth number to add
 var sum; // sum of number1 and number2 and number3 and number4



 // read in first number from user as a string
 firstNumber = window.prompt( "Enter the Propagation time  (in milliseconds)" );

 // read in second number from user as a string
secondNumber = window.prompt( "Enter the Transmission time  (in milliseconds)" );

// read in third number from user as a string
 thirdNumber = window.prompt( "Enter the Queuing time  (in milliseconds)" );

// read in fourth number from user as a string
 fourthNumber = window.prompt( "Enter the Propagation delay  (in milliseconds)" );

 // convert numbers from strings to integers
 number1 = parseInt( firstNumber ); 
 number2 = parseInt( secondNumber );
 number3 = parseInt( thirdNumber );
 number4 = parseInt( fourthNumber );

 sum = number1 + number2 + number3 + number4; // add the numbers

if (sum < 1) {
   document.write ("The sum is less than one");
} else if (sum > 1) {
   document.write( "The sum is more than one"); 
}

 // display the results
document.writeln( "<h1>The network latency is " + sum + "</h1>" );

Upvotes: 0

Manav Dubey
Manav Dubey

Reputation: 780

First off, judging by your short description on what you want, I understand you want the statement of whatever you want to say after you calculate the total. To do that, it is really easy.

Example:

sum = number1 + number2 + number3 + number4; // add the numbers

if(sum > 1){
    //Your code
} else {
    //Your code
}

The reason I didn't put else if because if the sum is greater than one, it runs whatever statement you want it to run. If it isn't, then it will run the other, the else statement.

If you want to look at more if/else examples, you can go on this StackOverflow post and check the examples they have and how to use it.

Upvotes: 0

Related Questions