Weekly Developer Newsletter

!The bug would be setting maxVal to 0? It sets a max value that might not exist in the inputArray!<.

Emory said:

!The bug would be setting maxVal to 0? It sets a max value that might not exist in the inputArray!<.

Bingo! Although you might want a spoiler on your answer gif

!It’d be easy to miss in this scenario since the correct answer of 20 would be returned, but if anyone sent an array of only negative numbers, it’d incorrectly return 0. !<

Sounds like a great idea. You can do it!

One small thing I thought when I saw this example question was “Yes, the bug is in line… umm” So lines are missing :slight_smile:

West said:
Sounds like a great idea. You can do it!

One small thing I thought when I saw this example question was “Yes, the bug is in line… umm” So lines are missing :slight_smile:

Haha that’s fair and a good thing to add! I have the answer at the bottom of the newsletter for the weekly “Spot the Bug” section

It looks like it works for me, but it wouldn’t work with all negative numbers so I guess instead of doing let maxVal = 0 you could just do let maxVal = inputArray[0]; to assign it the first value and loop through the array from there?

@Jamie
Yes that’d work!

!Traditionally with these kind of coding problems you either assign to the first value in the array like you mentioned (although you’ll have to also add a check to make sure the array isn’t empty), or you use something like Int.MIN_VALUE, INT_MIN, etc (depends on the language) to get the smallest number an Int can possibly be!<

@Gray
Oh that’s interesting — I didn’t know that was a thing. When I was thinking about it I thought it would be nice to have a feature like an absolute minimum value! :joy:

@Gray
-Infinity also an option.

@Jamie
You also have to handle the edge case where the array is empty (return undefined, maybe).

Drew said:
@Jamie
You also have to handle the edge case where the array is empty (return undefined, maybe).

Definitely!

Hey folks!

I’ve recently been working on a Newsletter aimed at helping newer developers. It includes things like:
- Programming Advice
- Notable tech updates
- Helpful programming tools/apps
- Cool open source projects to work on
- Small programming quizzes
- and more!

I just released the first edition and I’d love to get some thoughts and feedback!

I really want to create “Something I wish I had when I first started programming”, so I would love to know if newer devs find this helpful :slight_smile:

https://beyond-code.beehiiv.com/p/don-t-git-confused

The real bug here is using a variable named index. I hate overly terse coding but even I respect the holy i.

!console.log(Math.max(...[-10, 20, -5, -1]))!<

Suki said:

!console.log(Math.max(...[-10, 20, -5, -1]))!<

Haha not the intended answer but I’d take that answer in an interview gif

Call it findMaxValuePositiveNumbersOnly and it would be bug-free.

Hadi said:
Call it findMaxValuePositiveNumbersOnly and it would be bug-free.

Is 0 a positive number though? Not really right? I hate ambiguity in my code so even your funny function rename still has a bug.

@Sidney
If you are entering 0 in a function findMaxValuePositiveNumbersOnly then it’s on you.

Hadi said:
@Sidney
If you are entering 0 in a function findMaxValuePositiveNumbersOnly then it’s on you.

It’s an array getting passed. If you already know what’s in it, why call the function?

I would argue there are 2 bugs. The intended one is probably the fact that it will return 0 if all numbers are negative. There’s many ways of handling this. For example starting maxVal as inputArray[0] and starting the index at 1.

This leads to the other bug, which is proper handling of an empty array. Right now it would return 0, but it should likely return a different value like NaN or, as Math.max does, Infinity.