'JavaScript Function find and return largest number
I'm attempting to create a function that takes two numbers, finds the highest one, and returns that.
When I run my code:
<script type="text/JavaScript">
var largest = function large(a, b) {
var num1 = a;
var num2 = b;
if (num1 > num2) {
return("num1" + num1 + " is greater");
} else {
return("num2" + num2 + " is greater");
}
}
</script>
</head>
<body>
<script type="text/JavaScript">
var num1 = parseInt(prompt("Please enter a number"));
var num2 = parseInt(prompt("Please enter a number"));
largest();
document.write(large);
</script>
it returns everything, not the number. I don't understand what I'm doing wrong. I'm really sorry if this has already been posted in a slightly different way by someone else. I don't understand this well enough to know how to begin searching for what I need :/
Solution 1:[1]
- You are calling
largest()
but it's return value isn't being captured anywhere. - When you do call
largest()
you don't pass the results of the twoprompt
statements as arguments to the function, which it requires as input. - Whenever you are using
parseInt()
, you should always specify the second (optional) argument which specifies the "radix" (base numeral system) so that you don't inadvertently, get a converted value (like a hex or octal). - You are trying to output
large
, which isn't the name your function variable uses. Just use a function declaration instead. - You don't need to create new variables in the function that map to the arguments that were passed, just use the arguments.
<head>
<script type="text/JavaScript">
// No need to assign a named function to
// another variable here. Also, see how the
// function includes a and b in its declaration?
// That means that when you call it, you must pass
// two values, the first will be known as "a" within
// the function and the second will be known as "b"
function largest(a, b) {
// No need to set up new variables, just use
// the passed arguments - - they are function variables
if (a > b) {
return a + " is greater";
} else {
return b + " is greater";
}
}
</script>
</head>
<body>
<script type="text/JavaScript">
var num1 = parseInt(prompt("Please enter a number"),10);
var num2 = parseInt(prompt("Please enter a number"),10);
// Take the return value from the function call and write it
document.write(largest(num1, num2));
</script>
</body>
Now, your code also looks like it's from 1997 and you are doing a couple of things that shouldn't be done these days:
type="text/javascript"
is not requred.document.write
should be avoided in all but a few cases as it causes anything already in the document to be thrown away and a new document created. Instead, access an existing element in the DOM and update it's content.- It's generally better to place your
script
just before the closingbody
tag so that by the time the HTML parser reaches it, all the html in thebody
will have been parsed and available in memory.
<body>
<div id="output"></div>
<script>
function largest(a, b) {
if (a > b) {
return a + " is greater";
} else {
return b + " is greater";
}
}
var num1 = parseInt(prompt("Please enter a number"),10);
var num2 = parseInt(prompt("Please enter a number"),10);
// Take the return value from the function call and
// set that as the text of the <div> element in the <body>
document.getElementById("output").textContent = largest(num1, num2);
</script>
</body>
And, the easiest solution to the "which number is greater" question is to just use Math.max()
.
<body>
<div>The largest is: <span id="output"></span></div>
<script>
var num1 = parseInt(prompt("Please enter a number"),10);
var num2 = parseInt(prompt("Please enter a number"),10);
// Take the return value from the function call and
// set that as the text of the <div> element in the <body>
document.getElementById("output").textContent = Math.max(num1, num2);
</script>
</body>
Solution 2:[2]
var fun = parseInt(prompt("Write a number:"))
var fun2 = parseInt(prompt("Write another number:"))
function action(){
if (fun > fun2){
document.write("The greatest number is:" + fun);
}
else{
document.write("The greatest number is:" + fun2);
}
}
action();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Mik Jon |