Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please implement the below function in Javascript and name the source file as h2.js . Use test file h2.test.js to test your code. You need

Please implement the below function in Javascript and name the source file as h2.js. Use test file h2.test.js to test your code. You need to install the jest before testing your code.

A chainable function say() that accepts one string per call, but when called without arguments, returns the words previously passed, in order, separated by a single space.

h2.test.js

const { change, stripQuotes, scramble, say, powers, interleave, } = require('./h2'); // Helper for the scramble tests function anagramsOfEachOther(s, t) { return s.split('').sort().join('') === t.split('').sort().join(''); } // Helper for testing the callbacky problems function generatorToArray(generator, ...args) { const result = []; generator(...args, (item) => result.push(item)); return result; } describe('change', () => { it('handles zero', () => { expect(change(0)).toEqual([0, 0, 0, 0]); }); it('computes answers for small integer values fine', () => { expect(change(97)).toEqual([3, 2, 0, 2]); expect(change(8)).toEqual([0, 0, 1, 3]); expect(change(250)).toEqual([10, 0, 0, 0]); expect(change(144)).toEqual([5, 1, 1, 4]); expect(change(97)).toEqual([3, 2, 0, 2]); }); it('handles large values', () => { // This test only passes if the solution is efficient expect(change(100000000000)).toEqual([4000000000, 0, 0, 0]); }); it('throws the proper exception for negative arguments', () => { expect(() => change(-50)).toThrow(RangeError); }); }); describe('stripQuotes', () => { it('works on the empty string', () => { expect(stripQuotes('')).toEqual(''); }); it('strips quotes properly for non-empty strings', () => { expect(stripQuotes('Hello, world')).toEqual('Hello, world'); expect(stripQuotes('"\'')).toEqual(''); expect(stripQuotes('a"""\'\'"z')).toEqual('az'); }); }); describe('scramble', () => { it('scrambles properly', () => { ['a', 'rat', 'JavaScript testing', '', 'zzz', '^*^*)^'].forEach((s) => { expect(anagramsOfEachOther(s, scramble(s))).toBeTruthy(); }); }); it('is really random (produces all permutations)', () => { const possibilities = new Set('ABC ACB BAC BCA CAB CBA'.split(' ')); for (let i = 0; i < 200; i += 1) { possibilities.delete(scramble('ABC')); } expect(possibilities.size).toBe(0); }); }); describe('say', () => { it('works when there are no words', () => { expect(say()).toBe(''); }); it('works when there are words', () => { expect(say('hi')()).toBe('hi'); expect(say('hi')('there')()).toBe('hi there'); expect(say('hello')('my')('name')('is')('Colette')()).toBe('hello my name is Colette'); }); }); describe('powers', () => { it('generates sequences of powers properly', () => { expect(generatorToArray(powers, 2, -5)).toEqual([]); expect(generatorToArray(powers, 7, 0)).toEqual([]); expect(generatorToArray(powers, 3, 1)).toEqual([1]); expect(generatorToArray(powers, 2, 63)).toEqual([1, 2, 4, 8, 16, 32]); expect(generatorToArray(powers, 2, 64)).toEqual([1, 2, 4, 8, 16, 32, 64]); }); }); describe('interleave', () => { it('interleaves properly', () => { expect(interleave([])).toEqual([]); expect(interleave([1, 4, 6])).toEqual([1, 4, 6]); expect(interleave([], 2, 3)).toEqual([2, 3]); expect(interleave([1], 9)).toEqual([1, 9]); expect(interleave([8, 8, 3, 9], 1)).toEqual([8, 1, 8, 3, 9]); expect(interleave([2], 7, '8', {})).toEqual([2, 7, '8', {}]); }); });

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

Recommended Textbook for

Pro Database Migration To Azure Data Modernization For The Enterprise

Authors: Kevin Kline, Denis McDowell, Dustin Dorsey, Matt Gordon

1st Edition

1484282299, 978-1484282298

More Books

Students also viewed these Databases questions

Question

7. Understand the challenges of multilingualism.

Answered: 1 week ago

Question

5. Give examples of variations in contextual rules.

Answered: 1 week ago