Ad

(Javascript) Take Three Integers From User To Display Sum, Average, Product, Smallest, And Largest Number

- 1 answer

The first issue that I'm having is with displaying the smallest and largest of the three numbers. Two separate alerts are appearing - the first alert says that the second-largest number is the largest (because it hasn't yet taken the third number into consideration), and the second alert correctly states that the largest number of the three is the largest. Not sure why this is happening - I just need one alert.

The second issue that I'm having is with displaying the sum, average, and product. When I try to run this code the page just goes blank - it doesn't work at all. Right now this portion of the code is commented out. Please help if you know what the problem is. I will need all of this information to display in the same alert. Thank you. JSFiddle: https://jsfiddle.net/HappyHands31/xzsf8ca4/8/

<!DOCTYPE html>

<!-- Fig. 6.14: welcome5.html -->
<!-- Using equality and relational operators. -->
<html>
   <head> 
    <meta charset = "utf-8">
    <title>Compare Numbers</title>
    <h1 style = "text-align:center">Compare the Integers!</h1>
    <script>

     var firstNumber; // first string entered by the user
     var secondNumber; // second string entered by the user
     var thirdNumber; // third string entered by the user
     var sum; // add the integers
     var average; // add the integers, divide by 3
     var product; // multiply the integers

     // enter first number from user as a string
     firstNumber = window.prompt("Enter first integer");

     // enter second number from user as a string
     secondNumber = window.prompt("Enter second integer");

     // enter third number from user as a string
     thirdNumber = window.prompt("Enter third integer");

     // convert numbers from strings to integers
     firstNumber = parseInt(firstNumber);
     secondNumber = parseInt(secondNumber);
     thirdNumber = parseInt(thirdNumber);

     //determine result
     if (firstNumber > secondNumber && thirdNumber)
        window.alert(firstNumber + " is largest");

     if (secondNumber > firstNumber && thirdNumber)
        window.alert(secondNumber + " is largest"); 

     if (thirdNumber > firstNumber && secondNumber)
        window.alert(thirdNumber + " is largest");
    //determine sum, average, and product
    /*
     sum (firstNumber + secondNumber + thirdNumber)
        window.alert("the sum is "); 
     average ((firstNumber + secondNumber + thirdNumber) / 3))
        window.alert("the average is ");
     product (firstNumber * secondNumber * thirdNumber)
        window.alert("the product is");
    */ 


// end if
</script>
</head>
<body>
</body>
</html>
Ad

Answer

Please throughout this answer. I embed logic to a function and call once all 3 numbers found and logic also change

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- Fig. 6.14: welcome5.html -->
<!-- Using equality and relational operators. -->
<html>
    <head>
        <title>Compare Numbers</title>
        <script type="text/javascript">
            var firstNumber, // first string entered by the user
                secondNumber, // second string entered by the user
                thirdNumber, // third string entered by the user
                sum, // add the integers
                average, // add the integers, divide by 3
                product; // multiply the integers

            // enter first number from user as a string
            firstNumber = window.prompt("Enter first integer");

            // enter second number from user as a string
            secondNumber = window.prompt("Enter second integer");

            // enter third number from user as a string
            thirdNumber = window.prompt("Enter third integer");

            // convert numbers from strings to integers
            firstNumber = parseInt(firstNumber);
            secondNumber = parseInt(secondNumber);
            thirdNumber = parseInt(thirdNumber);
            findLargNum(firstNumber,secondNumber,thirdNumber);

            function findLargNum(firstNumber,secondNumber,thirdNumber){
                //determine result
                if (firstNumber > secondNumber) {
                    if(firstNumber > thirdNumber) window.alert(firstNumber + " is largest");
                }
                else if(secondNumber > thirdNumber) window.alert(secondNumber + " is largest");
                else window.alert(thirdNumber + " is largest");

                var sum =  firstNumber + secondNumber + thirdNumber;
                window.alert("the sum is " + sum); 
                var average = (firstNumber + secondNumber + thirdNumber) / 3;
                window.alert("the average is " + average);var product = firstNumber * secondNumber * thirdNumber;window.alert("the product is" + product);
            }
        </script>
    </head>
    <body>
        <h1 style = "text-align:center">Compare the Integers!</h1>
    </body>
</html>

var firstNumber, // first string entered by the user
	secondNumber, // second string entered by the user
	thirdNumber, // third string entered by the user
	sum, // add the integers
	average, // add the integers, divide by 3
	product; // multiply the integers

// enter first number from user as a string
firstNumber = window.prompt("Enter first integer");

// enter second number from user as a string
secondNumber = window.prompt("Enter second integer");

// enter third number from user as a string
thirdNumber = window.prompt("Enter third integer");

// convert numbers from strings to integers
firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);
thirdNumber = parseInt(thirdNumber);
findLargNum(firstNumber,secondNumber,thirdNumber);

function findLargNum(firstNumber,secondNumber,thirdNumber){
	//determine result
	if (firstNumber > secondNumber) {
		if(firstNumber > thirdNumber) window.alert(firstNumber + " is largest");
	}
	else if(secondNumber > thirdNumber) window.alert(secondNumber + " is largest");
	else window.alert(thirdNumber + " is largest");
	
	var sum =  firstNumber + secondNumber + thirdNumber;
	window.alert("the sum is " + sum); 
	var average = (firstNumber + secondNumber + thirdNumber) / 3;
	window.alert("the average is " + average);var product = firstNumber * secondNumber * thirdNumber;window.alert("the product is" + product);
}
<h1 style = "text-align:center">Compare the Integers!</h1>

Ad
source: stackoverflow.com
Ad