Reputation: 55
Need to find the largest of a series of numbers, but the numbers must come from user input. For this assignment the JavaScript must be written in HTML doc, this is what I have so far:
function getLargest(a,b,c,d,e){
let numArray = [b,c,d,e];
let largest = a;
for(let item of numArray){
if(item > largest){
largest = item;
}
}
return largest;
document.getElementById('largest').innerHTML = largest
}
let a = prompt("Enter 1st Number");
let b = prompt("Enter 2nd Number");
let c = prompt("Enter 3rd Number");
let d = prompt("Enter 4th Number");
let e = prompt("Enter 5th Number");
getLargest (a,b,c,d,e);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>The Largest number is:</h2>
<p id = "largest"></p>
</body>
</html>
Upvotes: 0
Views: 1910
Reputation: 115
you could write the getLargest function like this:
function getLargest(...nums) {
let max_value = Math.max(...nums);
return max_value;
}
Upvotes: 2
Reputation: 738
Your code returns largest
before it gets to document.getElementById('largest').innerHTML = largest
.
Additionally, you can just use Math.max(...numArray)
to get the maximum instead of all the code you have to find largest
.
Upvotes: 3