@Frey
That’s true!
!My intended solution would be for someone to initialize maxVal to -Infinity/Int.MIN_VAL. That way, it’d handle both issues like you mentioned. If the array was empty it’d just return MIN_VAL!<
@Frey
That’s true!
!My intended solution would be for someone to initialize maxVal to -Infinity/Int.MIN_VAL. That way, it’d handle both issues like you mentioned. If the array was empty it’d just return MIN_VAL!<
It’d be the initializing maxVal = 0. We are lucky that the initial array has something bigger than 0. If it was all negative, we would never find the largest value.
There’s no red squiggly lines so there’s no bug in my eyes.
Most languages already have a method for this.
!let maxVal = 0; this is the line causing the issue, if you have entirely negative numbers it will tell you 0 every time. Instead maxVal should not be defined at the start and the if statement should check if maxVal is undefined OR if the latest is greater, then update the max.!<
!Logical bug. If all array values are negative, the output will always be 0.!<
Yes, it should be >!function findMaxValue(inputArray) { return Math.max(...inputArray); }
instead.!<
I’d just use Math.max(...inputArray)
. It’d even somewhat deal with non-numeric values.
Index = 0, inputArray.length starts as 0, the check is index < inputArray.length; is this never gets inside the loop.
Hayes said:
Index = 0, inputArray.length starts as 0, the check is index < inputArray.length; is this never gets inside the loop.
What? inputArray.length doesn’t start anywhere; it’s a fixed number. In this case it would be 4 so index would go 0,1,2,3 which is correct.
@Remington
Oh shit lmao what am I saying haha. I don’t know what I was thinking and I only had 3 beers this evening. But yeah. I am totally wrong lmao.