You could use something simple as Math.max(5, 10);
const numOne = 5;
const numTwo = 10;
console.log(`Bigger number is: ${Math.max(numOne, numTwo)}`);
Or if you absolutely 'have to' use switch statement, you can try something like this:
const numOne = 5;
const numTwo = 10;
switch(true) {
case (numOne > numTwo):
console.log(`Bigger number is ${numOne}`);
break;
case (numOne < numTwo):
console.log(`Bigger number is ${numTwo}`);
break;
case (numOne === numTwo):
console.log(`${numOne} is equal to ${numTwo}`);
break;
default: console.log(false, '-> Something went wrong');
}
Answer from Dženis H. on Stack OverflowYou could use something simple as Math.max(5, 10);
const numOne = 5;
const numTwo = 10;
console.log(`Bigger number is: ${Math.max(numOne, numTwo)}`);
Or if you absolutely 'have to' use switch statement, you can try something like this:
const numOne = 5;
const numTwo = 10;
switch(true) {
case (numOne > numTwo):
console.log(`Bigger number is ${numOne}`);
break;
case (numOne < numTwo):
console.log(`Bigger number is ${numTwo}`);
break;
case (numOne === numTwo):
console.log(`${numOne} is equal to ${numTwo}`);
break;
default: console.log(false, '-> Something went wrong');
}
Logical operations return boolean on Javascript.
document.write writes HTML expressions or JavaScript code to a document, console.log prints the result on the browser console.
switch (num1>num2) {
case true:
console.log(num1);
break;
case false:
console.log(num2);
break;
}
Videos
You're looking for the Max function I think....
var c = Math.max(a, b);
This function will take more than two parameters as well:
console.log(Math.max(4,76,92,3,4,12,9));
//outputs 92
If you have a array of arbitrary length to run through max, you can use apply...
var arrayOfNumbers = [4,76,92,3,4,12,9];
console.log(Math.max.apply(null, arrayOfNumbers));
//outputs 92
OR if you're using ES2015+ you can use spread syntax:
var arrayOfNumbers = [4,76,92,3,4,12,9];
console.log(Math.max(...arrayOfNumbers);
//outputs 92
c = (a > b) ? a : b;
This will do the same thing. This can be really useful and a real time saver.
Try this to get the maximum:
var result = Math.max(h1,h2)
var highestNum = h1 > h2 ? h1: h2;
or if you dislike tertiary statements:
var highestNum;
if (h1 > h2) {
highestNum = h1;
} else {
highestNum = h2;
}
Edit: Interestingly enough, it appears the if-else statement runs much faster than Math.max (on Chrome 21)
http://jsperf.com/jbabey-math-max
<body>
<form>
<label>First number:</label> <input type="text" name="num1"/><br/>
<label>Second number:</label> <input type="text" name="num2"/><br/>
<input type="button" value="Compute" onclick="myResult.value=larger()"/>
<input type="text" name="result" id="myResult"/>
</form>
</body>
function larger() {
var1 = parseInt(document.getElementsByName('num1'));
var2 = parseInt(document.getElementsByName('num2'));
var max = var1>var2 ? var1 : var2;
return max;
}
Your function don´t get the values from the HTML. You don´t need to parameterize your function. You can read the values inside your function like:
var1 = document.getElementById('someId');
And add an id="someId"-Tag to your input field.