Reputation: 35
Hello I am in a class trying to work on the problem, of taking five inputs and giving the smallest and largest. I have some code wrote up but it doesn't work and I cannot see why. Any help would be great. Can someone point me in the wrong direction. I can write in the inputs and but when it gives me a output both largest and smallest show up as NaN
.
Here is the code.
<html>
<head>
<meta charset = "utf-8">
<title> 6.20 Homework </title>
<script type = "text/javascript">
var i1, i2, i3, i4, i5; //input numbers
var largest;
var smallest; //largest and smallest
i1=window.prompt ("Please enter first integer","0");
i2=window.prompt ("Please enter second integer","0");
i3=window.prompt ("Please enter third integer","0");
i4=window.prompt ("Please enter fourth integer","0");
i5=window.prompt ("Please enter fifth integer","0");
i1 = largest;
i1 = smallest;
if ( i2 > largest )
{ i2 = largest}
if ( i3 > largest )
{ i3 = largest}
if ( i4 > largest )
{i4 = largest}
if ( i5 > largest )
{ i5 = largest}
if (i2 < smallest )
{i2 = smallest}
if (i3 < smallest )
{i3 = smallest}
if (i4 < smallest )
{i4 = smallest}
if (i5 < smallest )
{i5 = smallest}
document.writeln(+largest+ " is the largest");
document.writeln(+smallest+ " is the smallest");
</script>
</head>
</html>
Upvotes: 1
Views: 2613
Reputation: 18299
Instead of using if
statements, you can use Math.max()
and Math.min()
:
var largest = Math.max(i1, i2, i3, i4, i5);
var smallest = Math.min(i1, i2, i3, i4, i5);
Upvotes: 2
Reputation: 69
I would probably suggest you to first create an array and an insert function which inserts the inputs into the array. By doing that , every time you get an input you insert the number in the right place of the array. Like :
var insert = function(arr,inp){
if(arr.length == 0) { arr.push(inp); return; }
var inserted = false;
for(var i=0;i<arr.length;i++){
if(arr[i] > inp){
var temp = arr[i];
arr[i] = inp;
insert(arr,temp);
return;
}
}
arr.push(inp);
}
var inputs; //array to insert inputs
var i1, i2, i3, i4, i5; //input numbers
i1=window.prompt ("Please enter first integer","0");
insert(inputs,i1);
i2=window.prompt ("Please enter second integer","0");
insert(inputs,i2);
i3=window.prompt ("Please enter third integer","0");
insert(inputs,i3);
i4=window.prompt ("Please enter fourth integer","0");
insert(inputs,i4);
i5=window.prompt ("Please enter fifth integer","0");
insert(inputs,i5);
document.writeln(+inputs[inputs.length-1]+ " is the largest");
document.writeln(+inputs[0]+ " is the smallest");
Upvotes: -1
Reputation: 7004
Try like this.use Math.max
and Math.min
for finding largest ans smallest numbers respectively.
var i1, i2, i3, i4, i5; //input numbers
var largest;
var smallest; //largest and smallest
i1=window.prompt ("Please enter first integer","0");
i2=window.prompt ("Please enter second integer","0");
i3=window.prompt ("Please enter third integer","0");
i4=window.prompt ("Please enter fourth integer","0");
i5=window.prompt ("Please enter fifth integer","0");
largest = Math.max(i1,i2,i3,i4,i5);
smallest = Math.min(i1,i2,i3,i4,i5);
console.log(largest);
console.log(smallest);
Upvotes: 1
Reputation: 12
on every line that looks like this: i1 = largest;
you are changing the value of the integer and not the value of largest
or smallest
you should switch the order there: largest = i1
Upvotes: 0