0

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>

6
  • 1
    Could you show your code with the example if..else statements please? Commented Apr 19, 2017 at 22:48
  • if (condition) { "code if true" } else { "code if false" } Commented Apr 19, 2017 at 22:58
  • if statements are Computer Programming 101 material. How can you know how to program and not know how to write a simple if? Commented Apr 19, 2017 at 22:58
  • @Barmar Everyone starts somewhere Commented Apr 19, 2017 at 23:06
  • OT but try and use "08" or "09" for one of your inputs. Then add a radix to your parseInt (parseInt( firstNumber, 10 )) Commented Apr 20, 2017 at 10:10

3 Answers 3

1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Link can expire over time. Please add the relevant code/info to your post so that future SO users can also benefit.
0

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.

Comments

0

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>" );

1 Comment

Do note that when the sum is equal to 1, no message will be written. This may or may not be what is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.