Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please implement the function below in Javascript and name the source file as h2.js . A test file h2.test.js is provided below! You need to

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

image text in transcribed

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  { 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', {}]); }); });
2. A function stripQuotes () that accepts a string and returns a new string equivalent to the argument but with all single quotes and double quotes removed. > stripQuotes (\a\""""\'to\"\"\"\"''2") atoz' 2. A function stripQuotes () that accepts a string and returns a new string equivalent to the argument but with all single quotes and double quotes removed. > stripQuotes (\a\""""\'to\"\"\"\"''2") atoz

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

What is the general goal of trend analysis?

Answered: 1 week ago

Question

=+What kinds of problems need to be overcome?

Answered: 1 week ago

Question

=+Describe an important trade-off you recently faced

Answered: 1 week ago