# JavaScript Part 1: 22-Exercise 6- Get Max

**URL:** <https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393>\
**Category:** JavaScript\
**Created:** [July 1, 2022, 6:02am UTC](https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393 "2022-07-01T06:02:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wly21](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wly21](https://forum.codewithmosh.com/u/wly21)\
**Post date:** [July 1, 2022, 6:02am UTC](https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393/1 "2022-07-01T06:02:47Z")

</div>

I have two questions about this exercise. It is an exercise about finding the largest number in the array.

First question:  
I wrote

```auto
const numbers = [1,2,3,4,100];
const max = getMax(numbers);
console.log(max);

function getMax(array){
    if (array.length === 0) return undefined;

    return Math.max(...array);
}

```

and it works. Just want to know is there anything wrong or anything that I should consider with my solution?  
Here is Mosh’s answer:

```auto
function getMax(array){
    if (array.length === 0) return undefined;

    let max = array[0];
    for (let i = 1; i < array.length; i++)
    if (array[i] > max)
        max = array[i];

    return max;
}

```

Second question:  
In the middle of the video when Mosh is writing the code using the reduce method, he wrote:

```auto
function getMax(array){
    if (array.length === 0) return undefined;

    array.reduce((accumulator, current) =>{
        if (current > accumulator) return current;
        return accumulator;
    });
}

```

this doesn’t work, and

```auto
function getMax(array){
    if (array.length === 0) return undefined;

    array.reduce((accumulator, current) =>{
        return (current > accumulator)?current : accumulator;
    });
}

```

this doesn’t work too.

The code only works until he simplified it into this.

```auto
function getMax(array){
    if (array.length === 0) return undefined;

   return array.reduce((a,b) => (a,b) ? a:b;
}

```

Can anyone explain why the two didn’t work and only the last one works? Am I missing something here?

---

<div class="post-metadata">

**Author:** ![wly21](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wly21](https://forum.codewithmosh.com/u/wly21)\
**Post date:** [July 1, 2022, 1:10pm UTC](https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393/2 "2022-07-01T13:10:39Z")

</div>

Second question solved. I forgot to put return keyword in front of array.reduce.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [July 1, 2022, 7:39pm UTC](https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393/3 "2022-07-01T19:39:31Z")

</div>

> [@wly21](#):
>
> Just want to know is there anything wrong or anything that I should consider with my solution?

Nothing wrong, I think Mosh was just trying to show you how to actually do it yourself rather than relying on an existing function (ie. `Math.max`) to do it for you. Frankly, if existing common tooling can do it for you, there is never a need to write it yourself. Knowing how to write it yourself is still potentially useful (at least pedagogically).

* * *

And I am happy you were able to resolve the second part on your own.

🦆

> **[Rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging)**
>
> In software engineering, rubber duck debugging (or rubberducking) is a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line by line, to the duck. Many other terms exist for this technique, often involving different (usually) inanimate objects, or pets such as a dog or a

---

<div class="post-metadata">

**Author:** ![ayub](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@ayub](https://forum.codewithmosh.com/u/ayub)\
**Post date:** [July 6, 2022, 11:41am UTC](https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393/4 "2022-07-06T11:41:06Z")

</div>

Hi, I am having issues with this code execution;

et productdisplay = new Surfexcel(33, ‘a’);  
let productdisplay1 = new Surfexcel(33, ‘a’);  
console.log(areEqual(productdisplay));  
console.log(areSame(productdisplay1));

function Surfexcel(age, name) {

```
this.age = 30;
this.name = 'ayub';

```

}

function areEqual(productdisplay, productdisplay1) {  
return productdisplay1.age === productdisplay.age && productdisplay1.name === productdisplay.name;  
}

function areSame(productdisplay, productdisplay1) {

```
return productdisplay1 === productdisplay;

```

}

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [July 6, 2022, 10:59pm UTC](https://forum.codewithmosh.com/t/javascript-part-1-22-exercise-6-get-max/13393/5 "2022-07-06T22:59:17Z")

</div>

Looks like you forgot to call `areEqual` and `areSame` with both parameters:

```javascript
console.log(areEqual(productdisplay, productdisplay1));
console.log(areSame(productdisplay, productdisplay1));

```

And please create a new post for distinct questions like this. Thanks!
