Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

my code isnt work Here are the test cases 'use strict'; import * as reducers from ' . / reducers . mjs ' ; test

my code isnt work Here are the test cases '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);
}); do you see my attached code? Here it is written out.... /*
* 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, currentValue){
const num = parseFloat(currentValue);
// Check if currentValue is not a number
if (isNaN(num)){
throw new TypeError('My error message');
}
// Otherwise, add numeric value to previousValue
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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

How effective is each pitch?

Answered: 1 week ago

Question

Analyse the process of new product of development.

Answered: 1 week ago

Question

Define Trade Mark.

Answered: 1 week ago