0

I dont understand why my code is not working, when I read it logically I feel that it should work, but what it does is return 3,4,2 as opposed to the highest number of the 3 (i.e. 4)

const array2 = ['a', 3, 4, 2] // should return 4

for(items of array2){
    if(items > 0) {
        console.log(Math.max(items));
}

What am I doing wrong? What have I misinterpreted? Please don't give me the answer, just tell me why my logic does'nt work

LOIC YATOU
  • 15
  • 1
  • 1
    Log the value of items. That should hopefully cause you to rename it from `items` (because it's a single value, not multiple values). – jarmod Sep 02 '22 at 19:02
  • 1
    Try to print `console.log(items, Math.max(items));` instead, I guess then you'll know why it prints 3, 4 and 2 – A_A Sep 02 '22 at 19:02
  • 1
    Also, you could learn how to use a debugger and step through the code. This will help you in this case, and in many others too – A_A Sep 02 '22 at 19:04

5 Answers5

1

in for-loop, items is just one item actually. Each time, you print the current item. it is basically the same thing with this

const array2 = ['a', 3, 4, 2] // should return 4

for(items of array2){
    if (items > 0) {
        console.log(items);
    }
}

you can do it with this only

const array2 = ['a', 3, 4, 2] // should return 4

console.log(Math.max(...array2.map(s => +s || Number.MIN_SAFE_INTEGER)));

check out: https://stackoverflow.com/a/44208485/16806649

if it was integer-only array, this would be enough:

const array2 = [3, 4, 2] // should return 4

console.log(Math.max(...array2));
codemode
  • 478
  • 3
  • 11
1
  • You just need to filter the array.
  • Below example I am using filter() method of array and then just pass that filteredarray to Math.max() function.
  • isNan() function returns false for valid number.
  • Math.max(...filteredArr) is using spred operator to pass the values.

const arr = ['a', 3, 4, 2];
const filteredArr = arr.filter(val => {
  if (!isNaN(val)) return val;
})
console.log(Math.max(...filteredArr));
Nexo
  • 2,125
  • 2
  • 10
  • 20
0

I don't think you need the "For(items of arr)" instead just if the length of the array is greater than 0, then console.log(Math.max(...arr) should work.

See document ion below:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

harp
  • 79
  • 7
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 08 '22 at 07:02
0

It is returning 3,4,2 because you are taking array2, and iterating through with each individual element of the array. items is not the entire array, it is the individual element and that is why Math.max of an individual element is just getting the same value.

0

just you need fliter you're array then get max value, also in arrayFilters function use to removes everything only return numbers of this array

  function arrayFilters(array){
const number = array.filter(element => typeof element === 'number')
 return number;
}

function getMaxValue(number){
   return Math.max.apply(null,number)
}
const arr = ['a',2,3,4];
console.log(getMaxValue(arrayFilters(arr)))