There are a few things wrong with your code as pointed out in the comments so the question isn't 100% clear so I present you with the below approach...
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 accepts an infinite number of arguments to compare against and does not restrict to a fix number of inputs. For example, the below are all valid uses:
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
By not using the arrow function expression you can use the arguments keyword to retrieve an iterable object of all values. Alternatively, if requirements are to use arrow functions you can simply use the spread syntax (...) do...
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 full working 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(`Nilai maksimum adalah ${result}`);
elsesuppose to do?letalso wrong.number?