Question: I encountered a problem in writing JS program. The program is about finding the maximum mapped value in an array. And return the original value
I encountered a problem in writing JS program.
The program is about finding the maximum mapped value in an array. And return the original value in the array. The element whose mapping value is `NaN`, `undefined` or `null` will be ignored. Following is my code:
But I encounter a test that I cannot pass that there's an object in it.
Following is the test code:
describe('when calling maxMap()', () => {
it('should return original value', () => {
const input = [
{ name: 'John', age: 10 },
{ name: 'Tom', age: 20 },
{ name: 'Bob', age: 30 }
];
expect(maxMap(input, value => value.age)).toEqual({ name: 'Bob', age: 30 });
});
});
export default function maxMap (array, mappingFunc) { // TODO: Please implement the function. // each !== null && each !== undefined && !Number. isNaN(each))) { return undefined;| You, 2 hours ago Uncommitted changes return mappedArray. reduce((maxValue, currentValue) => { if (Number.isNaN(currentValue)) { return maxValue; } return maxValue > currentValue ? maxValue : currentValue; }, Number.NEGATIVE_INFINITY); // --end--> }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
