0

so this is what i did, if anyone has some free time maybe you could see what i did wrong? i cant seem to figure it out and i'm very curious :c

let mayor = 0;

function max(a, b, c) {
  for (let i = 0; i <= max.length; i++) {
    if (max[i] > mayor) {
      mayor = max[i];
    }
    return mayor;
  }
}

let mayorFin = max(5, 2, 6);

console.log(mayorFin); // 6 // 6

it returns the 0 so it never changes mayor to the max in between parenthesis.

I'm in the process of learning and i believe there's no such thing as a dumb question, any help is much appreciated form the bottom of my heart cause' your time is valuable, thanks!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    `max` is a function, not an array. Why are you trying to iterate over it? Did you think that using the function name as an array would access the parameters? – Barmar Mar 08 '22 at 21:09
  • Also, your return statement is inside the for-loop, but most likely should be after the for-loop instead. – Endzeit Mar 08 '22 at 21:10
  • so i need to use a different word? would that work? i dont know yet what "iterate" is, but i will google it – froxen1995 Mar 08 '22 at 21:10
  • iterate means loop -- it's what `for` does. – Barmar Mar 08 '22 at 21:11
  • Does this answer your question? [Find the min/max element of an array in JavaScript](https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript) – Endzeit Mar 08 '22 at 21:14
  • Btw, "mayor" means "the elected head of a city, town, or other municipality", while "ma*j*or" is "important, serious, or significant". – FZs Mar 08 '22 at 21:30

2 Answers2

0

max is not an array. create an array inside function and use it. like this:

let mayor = 0;

function max(a, b, c) {
  let arr = [a, b, c]

  for (let i = 0; i <= arr.length; i++) {

    if (arr[i] > mayor) {

      mayor = arr[i];

    }
  }
  return mayor;

}

let mayorFin = max(5, 2, 6);

console.log(mayorFin); // 6 // 6
Vugar Taghiyev
  • 413
  • 3
  • 9
  • Use the "Tidy" button in the snippet editor to fix the indentation. – Barmar Mar 08 '22 at 21:12
  • oh! this is so helpful!! so we had to set it as an arrey to fix it, and use 'return' after the 'for' – froxen1995 Mar 08 '22 at 21:13
  • @CamiloAndrésCuraRojas yeah exactly – Vugar Taghiyev Mar 08 '22 at 21:15
  • this is exactly what i was trying to do, there might be other ways, better ways, but this is what i was trying, thanks a lot! – froxen1995 Mar 08 '22 at 21:17
  • 1
    @CamiloAndrésCuraRojas so while this fixes the problem, there's still another bug in this. you probably want to move `let mayor = 0` inside the `max` function because otherwise it's going to "remember" it's state the next time you run this function, so if you did `max(4,5,6)` it will return 6 then you do `max(1,2,3)` it will return 6 again. there's also another bug where if you feed `max(-1,-2,-3)` it will return 0. can be fixed by setting `let mayor = a; let arr = [b, c];` – bryan60 Mar 08 '22 at 21:20
  • @bryan60 thanks!! this is going the extra mile! – froxen1995 Mar 08 '22 at 21:31
0

You may use "..." operator like this. It allow you insert any amount of arguments into your function.

function max(...args) {
  let mayor = 0;

  for (let arg of args) {
    if (arg > mayor) mayor = arg;
  }
  return mayor;
}

let mayorFin = max(5, 2, 6);

console.log(mayorFin); // 6 // 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 08 '22 at 22:43