Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help fixing my code to meet these test cases, the cases are correct the code needs to be debugged. Do not change test

I need help fixing my code to meet these test cases, the cases are correct the code needs to be debugged. Do not change test case, change code. 'use strict';
import * as reducers from './reducers.mjs';
test('Test 1. sum of an array of numbers using reducer1',()=>{
expect([1,2,3].reduce(reducers.reducer1)).toBe(6);
});
test('Test 2. reducer1 skips all non-numeric values', ()=>{
expect([1,2, true, 'a',4, true].reduce(reducers.reducer1)).toBe(7);
});
test('Test 3. reducer1 skips a non-numeric value even if it is the first element', ()=>{
expect(['a',1,2,3].reduce(reducers.reducer1)).toBe(6);
});
test('Test 4. reducer1 returns 0 if all the elements are non-numeric', ()=>{
expect(['a','b'].reduce(reducers.reducer1)).toBe(0);
});
test('Test 5. sum of an array of numbers using reducer2',()=>{
expect([1,2,3].reduce(reducers.reducer2)).toBe(6);
});
test('Test 6. reducer2 throws TypeError exception for a non-numeric value', ()=>{
expect(()=>{[1,2, true, 'a',4, true].reduce(reducers.reducer2)}).toThrow(TypeError);
});
test('Test 7. reducer2 throws TypeError exception for a non-numeric value even when it is the first element', ()=>{
expect(()=>{['a',1,2,3].reduce(reducers.reducer2)}).toThrow(TypeError);
}); MY CODE is attached and here /*
* Don't change the declaration of this function.
*/
function reducer1(previousValue, currentValue){
const num = parseFloat(currentValue);
// Check if currentValue is not a number
if (isNaN(num)){
// If currentValue is not numeric, return previousValue unchanged
return previousValue;
}
// Otherwise, add numeric value to previousValue
return previousValue + num;
}
/*
* Don't change the declaration of this function.
*/
function reducer2(previousValue =0, currentValue){
const num = parseFloat(currentValue);
if (isNaN(num)){
throw new TypeError('My error message');
}
return previousValue + num;
}
// Don't add or change anything below this comment.
export { reducer1, reducer2};
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions