Использование массива reduce()
и charCodeAt()
function maxNum(array, str = -Infinity) {
return array.reduce((max, item) => {
let itemc = isNaN(item) ? item.charCodeAt() : item;
let maxc = isNaN(max) ? max.charCodeAt() : max;
return max = (itemc > maxc) ? item : max;
}, str)
}
console.log(maxNum([1, 2, 3, 5], 10))
console.log(maxNum([1, 2, 3, 'a'], 10))
console.log(maxNum([1, 2, 3, 'a','c'], 'b'))
console.log(maxNum([1, 2, 3, 'a'], 'b'))
Ваш подход с использованием charCodeAt()
function maxNum(array, str = -Infinity) {
var max = str;
var a = array.length;
for (counter = 0; counter < a; counter++) {
let itemc = isNaN(array[counter]) ? array[counter].charCodeAt() : array[counter];
let maxc = isNaN(max) ? max.charCodeAt() : max;
if (itemc > maxc) {
max = array[counter];
}
}
return max;
}
console.log(maxNum([1, 2, 3, 5], 10))
console.log(maxNum([1, 2, 3, 'a'], 10))
console.log(maxNum([1, 2, 3, 'a','c'], 'b'))
console.log(maxNum([1, 2, 3, 'a'], 'b'))