There seems to be some issues in your code based on the comments provided, which makes the question unclear. Here is an alternative approach that may help...
function getMax() {
const nums = arguments;
var max = null;
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (max === null || num > max) {
max = num;
}
}
return max;
}
This function allows you to compare an infinite number of arguments and is not limited to a fixed number of inputs. You can use it like this:
const validExample1 = getMax(33, 72, 189, 4, 1); // Output: 189
const validExample2 = getMax(2, 2); // Output: 2
const validExample3 = getMax(320, 1, 8); // Output: 320
const validExample4 = getMax(-1, -200, -34); // Output: -1
Instead of using the arrow function expression, you can utilize the arguments
keyword to access all values. If arrow functions are required, you can opt for the spread syntax (...) as shown below:
const getMax = ( ...nums ) => {
var max = null;
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (max === null || num > max) {
max = num;
}
}
return max;
}
A complete example:
const number1 = 3;
const number2 = 72;
const number3 = 189;
function getMax() {
const nums = arguments;
var max = null;
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
if (max === null || num > max) {
max = num;
}
}
return max;
}
const result = getMax(number1, number2, number3);
console.log(`The maximum value is ${result}`);