2

first thing first i would like to say thank you so much for dropping by. I'm a beginner at Javascript. And now dealing with some issue, and i need help to identify what is wrong with my lines. So here it is, i want to find out the highest number from the variable using conditional statement inside a function, I'm stuck right now.

EDITED : Thankyou guys for helping me out, now I have a clear vision about what I should learn more.

const number1 = 103;
const number2 = 72;
const number3 = 189;

const getMax = (a, b, c) => {
  let max = a;
  
  if(b > max) {
    let max = b;
  } else if (c > max){
    let max = c;
  };
  
  return max;
}

getMax(number1, number2, number3);

console.log(`Max number is ${getMax}`);```
4
  • what the else suppose to do? Commented Mar 22, 2021 at 15:42
  • and the let also wrong. Commented Mar 22, 2021 at 15:43
  • Where did you define number? Commented Mar 22, 2021 at 15:43
  • I define the number at getMax(number1, number2, number3); Commented Mar 22, 2021 at 15:51

4 Answers 4

1

Variables declared with the let keyword have a block scope, that is, they are only accessible within the block they were defined in. You are first setting max to a, then inside your if statement, you are creating another local variable with the same name and assigning it the value b. What you should do is remove the let declarations inside your if/else statement.

Second, your if block is being exited when the first condition is satisfied. You should use another if statement (which will run regardless of whether or not the condition in the first one is met) as opposed to an else statement (which will only run if the first condition is not met).

Third, you are passing an object reference to your function in your console.log (all functions are objects in javascript) which is equivalent to the function text. You need to pass the function call instead.

That's all you need to do, besides not using undefined variables.

const number1 = 54;
const number2 = 72;
const number3 = 189;

const getMax = (a, b, c) => {
  let max = a;
  
  if(b > max) {
    max = b;
  }
  
  if (c > max){
    max = c;
  };
  
  return max;
}

console.log(`Nilai maksimum adalah ${getMax(number1, number2, number3)}`);

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou so much for your help, it is clear now what I should learn more
0

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}`);

Comments

0

You need ot omit the else part, because if b is not greater than a, you need still a comparison with c.

The other two points are:

  1. No need to declare a blockwise max, because the outer does not get the value from the condition.

  2. You never use the result of the call of getMax.

const getMax = (a, b, c) => {
    let max = a;

    if (b > max) max = b;
    if (c > max) max = c;

    return max;
}



console.log(`max value: ${getMax(2, 72, 189)}`);

Comments

0

I see many errors. First, you initialize max 2 times. let max = a; and then you can use max. But the else if is also wrong.

What if c < b < a? Then only the first if applies although c is greater than a

1 Comment

Can you perhaps show the final code that the question asker would need to use?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.