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
5 Answers 5
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));
-
1
array2.filter
seems more accurate for cases like['a', -1, -2]
A_A– A_A2022年09月02日 19:06:29 +00:00Commented Sep 2, 2022 at 19:06 -
makes sense. thanks. updated with
Number.MIN_SAFE_INTEGER
:)codemode– codemode2022年09月02日 19:07:27 +00:00Commented Sep 2, 2022 at 19:07 -
I would avoid calling it
items
(plural) - it should probably beitem
(singular), and properly declared withconst
(in this case).Andy– Andy2022年09月02日 19:12:19 +00:00Commented Sep 2, 2022 at 19:12 -
@Andy agree. to show the confusion, I made minimal changes in the original code.codemode– codemode2022年09月02日 19:14:14 +00:00Commented Sep 2, 2022 at 19:14
- You just need to filter the array.
- Below example I am using
filter()
method of array and then just pass thatfilteredarray
toMath.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));
-
2Or just:
const filteredArr = arr.filter((val) => !isNaN(val));
jarmod– jarmod2022年09月04日 21:41:19 +00:00Commented Sep 4, 2022 at 21:41
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
-
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.2022年09月08日 07:02:17 +00:00Commented Sep 8, 2022 at 7:02
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.
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)))
items
(because it's a single value, not multiple values).console.log(items, Math.max(items));
instead, I guess then you'll know why it prints 3, 4 and 2