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 OverflowMDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › max
Math.max() - JavaScript | MDN
The largest of the given numbers. Returns NaN if any of the parameters is or is converted into NaN. Returns -Infinity if no parameters are provided. Because max() is a static method of Math, you always use it as Math.max(), rather than as a method of a Math object you created (Math is not a constructor). Math.max.length is 2, which weakly signals that it's designed to handle at least two parameters.
How to use math.max with array
I've found Mozilla Developer Network (MDN) to be a great resource for JavaScript documentation. In this particular case, the last of the three examples they give right at the top of the page for Math.max() is exactly what you're looking for. As a PSA, when looking for JS tips, skip w3schools entirely. You're welcome. More on reddit.com
How to find maximum between two numbers in javascript using switch case? - Stack Overflow
If for some reason you were given numbers you could explicitly cast them to booleans with something like case !!0: but that starts to get a little hard on the eyes. If your goal is to find the max of two numbers, Math.max(num1, num2) is hard to beat for readability. More on stackoverflow.com
JavaScript Math.max() Explained with Examples
Math Max Math.max() is a function that returns the largest value from a list of numeric values passed as parameters. If a non-numeric value is passed as a parameter, Math.max() will return NaN . An array of numeric values can be passed as a single parameter to Math.max() using either spread ... More on forum.freecodecamp.org
Create a function named max that accepts two numbers as arguments
You have solved the hard part! just Double check the syntax of writing a function in JavaScript and you should be fine. You are ask to Create a function named max that accepts two numbers as arguments. More on teamtreehouse.com
Videos
write a javascript program to find maximum between two ...
03:56
JavaScript tips — Find the maximum value in an array of numbers ...
02:49
Finding the maximum of two number in JavaScript | JavaScript - YouTube
05:23
How to Find Maximum and Minimum in an Array in JavaScript ...
05:08
Find the Biggest Number with Math.max() in JavaScript | Important ...
TutorialsPoint
tutorialspoint.com › home › javascript › javascript math.max() method
JavaScript Math.max() Method
September 1, 2008 - The Math.max() method in JavaScript accepts a set of arguments as parameters, finds the highest numeric value among it, and returns it. If no arguments are provided, -Infinity is returned, indicating that there is no maximum value.
W3Schools
w3schools.com › jsref › jsref_max.asp
JavaScript Math max() Method
The Math.max() method returns the number with the highest value. ... Math.max() is an ECMAScript1 (JavaScript 1997) feature.
Programiz
programiz.com › javascript › library › math › max
JavaScript Math max()
JavaScript Math trunc() The max() method finds the maximum value among the specified values and returns it. let numbers = Math.max(12, 4, 5, 9, 0, -3); console.log(numbers); // Output: 12 · The syntax of the max() method is: Math.max(number1, number2,...) Here, max() is a static method.
Reddit
reddit.com › r/bitburner › how to use math.max with array
r/Bitburner on Reddit: How to use math.max with array
October 28, 2021 -
I'm trying to get the maximum value in an array. What's the correct syntax to do this?
Alternative, is there a way to compare different variables and get the name of the variable with the highest value? Like
var memory = 10 var cores = 20 var level = 200 var node = 5 -> return "level"
Top answer 1 of 3
7
I've found Mozilla Developer Network (MDN) to be a great resource for JavaScript documentation. In this particular case, the last of the three examples they give right at the top of the page for Math.max() is exactly what you're looking for. As a PSA, when looking for JS tips, skip w3schools entirely. You're welcome.
2 of 3
3
You could use destructuring spread syntax: Math.max(...arr) That would pass all entries of the array as individual arguments to the function. Alternatively you could use "reduce", which would be a useful function to have in your toolbelt in general anyway. I don't remember the exact syntax rn, but I believe it looks something like: arr.reduce((a,b) => Math.max(a,b)) Someone will want to verify and/or correct me on this, and probably expand on it a bit.
Top answer 1 of 4
2
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');
}
2 of 4
1
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;
}
Top answer 1 of 3
3
HI Francesco,
It took me a minute, but I finally saw that you used const, which is for creating variables. Use function instead and it should work.
Hope this helps!
2 of 3
1
Here is another way using Arrow function expressions
```javascript
const max = (n1,n2) =>{
if( n1 > n2){
return n1;
} else {
return n2;
}
}
console.log(max(10,5));
```
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-max-method
JavaScript Math max() Method - GeeksforGeeks
September 30, 2024 - The Math.max() method in JavaScript returns the largest value from a set of zero or more numbers.
Built In
builtin.com › articles › javascript-array-max
3 Methods to Find the JavaScript Array Max | Built In
February 16, 2024 - More on JavaScriptHow to Pass an Array From Java to JavaScript · In this traditional approach, we loop through each element in the array and compare it with a previously stored maximum value. If the current element is greater, we update our maximum. let numbers = [3, 7, 2, 8, 5]; let max = numbers[0]; // initialize to the first value for (let i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } } console.log(max); // Outputs: 8
Math.js
mathjs.org › docs › reference › functions › max.html
math.js | an extensive math library for JavaScript and Node.js
math.max(2, 1, 4, 3) // returns 4 math.max([2, 1, 4, 3]) // returns 4 // maximum over a specified dimension (zero-based) math.max([[2, 5], [4, 3], [1, 7]], 0) // returns [4, 7] math.max([[2, 5], [4, 3], [1, 7]], 1) // returns [5, 4, 7] math.max(2.7, 7.1, -4.5, 2.0, 4.1) // returns 7.1 math.min(2.7, ...
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › MAX_SAFE_INTEGER
Number.MAX_SAFE_INTEGER - JavaScript | MDN
Number.MAX_SAFE_INTEGER represents ... The largest representable number in JavaScript is actually Number.MAX_VALUE, which is approximately 1.7976931348623157 × 10308....
W3Schools
w3schools.com › java › ref_math_max.asp
Java Math max() Method
The max() method returns the number with the highest value from a pair of numbers. Tip: Use the min() method to return the number with the lowest value. ... If you want to use W3Schools services as an educational institution, team or enterprise, ...