# Highest Integer in Array (Recursion)

**URL:** <https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242>\
**Category:** General\
**Created:** [November 19, 2020, 8:11pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242 "2020-11-19T20:11:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.yj.cha](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/peter.yj.cha/32/182529_2.png) [@peter.yj.cha](https://forum.freecodecamp.org/u/peter.yj.cha)\
**Post date:** [November 19, 2020, 8:11pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/1 "2020-11-19T20:11:29Z")

</div>

Hi,  
I’ve watched several videos on recursion and feel like I have an okay grasp on it. However I do have some questions about this code I’ve been studying. Pretty simple, just return the highest integer in the array.

```auto
function findHighest(arr){
  if (arr.length==1)
		{		
		return arr[0];
		}
	else 
		{
		var res = findHighest(arr.slice(1));
		var ret= res>=arr[0]? res:arr[0];			
			return ret;
		}
}

```

So… I understand the `if` portion of it. But confused on the `else` part. What does the slice function have to do with the array? I thought all slice(1) would do is cut out arr[0] and return everything from arr[1]…arr[n]. What exactly is calculated when it says `res >= arr[0]` ?

Thanks!

---

<div class="post-metadata">

**Author:** ![peter.yj.cha](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/peter.yj.cha/32/182529_2.png) [@peter.yj.cha](https://forum.freecodecamp.org/u/peter.yj.cha)\
**Post date:** [November 20, 2020, 7:29pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/3 "2020-11-20T19:29:38Z")

</div>

I’m still not sure to be honest.  
For example if we are given [1,2,3,4]  
It would skip the if statement since arr.length \> 1 and execute the else part. This would then call the function findHighest(arr.slice(1)) which would slice the array into [2,3,4].  
The part I don’t get is the variable ret, which would be  
`[2,3,4] >= 1`. How would I know if this is true or not? Is it based off the length of the array or is there a value I should be looking for?

Is it going to test every element of the array ? That’s the only thing I can think of right now.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 20, 2020, 7:44pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/4 "2020-11-20T19:44:04Z")

</div>

```auto
function findHighest(arr){
  if (arr.length == 1) {		
    return arr[0];
  } else {
    var res = findHighest(arr.slice(1)); // What is res, in words?
    var ret = res >= arr[0] ? res : arr[0]; // What does this simpler version of the code do: var big = a >= b ? a : b		
    return ret;
  }
}

```

I added a couple of comments.

I’m not sure why you think that `res >= arr[0]` means `[2, 3, 4] >= 1`. What is `res`? What will `findHighest(arr.slice(1))` return?

---

<div class="post-metadata">

**Author:** ![peter.yj.cha](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/peter.yj.cha/32/182529_2.png) [@peter.yj.cha](https://forum.freecodecamp.org/u/peter.yj.cha)\
**Post date:** [November 20, 2020, 8:04pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/5 "2020-11-20T20:04:58Z")

</div>

Isn’t `res` just a variable that contains another function?  
I always thought `slice` just returns an array from a start index to an end index.  
So `arr.slice[1]` would return the `arr` starting from the 2nd element until the end.  
Is that wrong?

And for `var big = a >= b ? a : b` It is asking if `a` is greater than or equal to `b` then return `a` and if it is false return `b`

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 20, 2020, 8:15pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/6 "2020-11-20T20:15:23Z")

</div>

`res` holds the result of calling the function on a slice of the original array. It does not hold a slice.

---

<div class="post-metadata">

**Author:** ![peter.yj.cha](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/peter.yj.cha/32/182529_2.png) [@peter.yj.cha](https://forum.freecodecamp.org/u/peter.yj.cha)\
**Post date:** [November 20, 2020, 8:32pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/7 "2020-11-20T20:32:41Z")

</div>

In that case… I think it would keep calling the function findHighest until it meets the if condition.

I’ll keep watching videos/reading online and try to learn more.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:36pm UTC](https://forum.freecodecamp.org/t/highest-integer-in-array-recursion/431242/8 "2021-06-01T18:36:46Z")

</div>

Closed on June 1, 2021, 6:36pm UTC


