Best JavaScript code snippet using fast-check-monorepo
functions-spec.js
Source:functions-spec.js
...173 assert('squareSum', squareSum([1, 2, 3]), 14);174 console.log('\n');175 // 6) Assume you have a method 'isSubstring' which checks if one word is a substring of another given word. 176 // Given two strings s1 y s2, write a function that check if s2 is a rotation of s1 using only one call to the isSubstring method.177 function isSubstring(str1, str2) {178 if (str2.length <= str1.length) {179 for (let i = 0; i < str1.length; i++) {180 if (str1.length - i < str2.length) {181 return false;182 }183 if (str1[i] === str2[0]) {184 for (let j = 0; j < str2.length; j++) {185 if (str1[i + j] !== str2[j]) {186 break;187 }188 if (j === str2.length - 1) {189 return true;190 }191 }192 }193 }194 }195 return false;196 }197 assert('isSubstring', isSubstring('roma', 'ma'), true);198 assert('isSubstring', isSubstring('erbottlewaterbottlewat', 'waterbottle'), true);199 assert('isSubstring', isSubstring('aslkjdfhlasdhisaackjhlkjhas', 'isaac'), true);200 assert('isSubstring', isSubstring('nadaquever', 'hola'), false);201 assert('isSubstring', isSubstring('nadaholuever', 'hola'), false);202 assert('isSubstring', isSubstring('abcdefg', 'd'), true);203 assert('isSubstring', isSubstring('a', 'abcde'), false);204 assert('isSubstring', isSubstring('a', 'a'), true);205 console.log('\n');206 function isRotation(str1, str2) {207 if (str1.length !== str2.length) {208 return false;209 }210 // Duplicates the size of the string;211 str2 += str2;212 return isSubstring(str2, str1);213 }214 assert('isRotation', isRotation('waterbottle', 'erbottlewat'), true);215 assert('isRotation', isRotation('pikachu', 'achupik'), true);216 assert('isRotation', isRotation('pikachu', 'achuppik'), false);217 assert('isRotation', isRotation('pokemon', 'mompoke'), false);218 console.log('\n');219 // 7) Write a function that receives a matrix M x N, and that returns a new matrix following the next rules:220 // - If any element of the [M][N] matrix is a 0 (zero), then that entire column and row is set to 0 (zero).221 function convertMatrix(matrix) {222 let result = [];223 // copy the matrix224 let indexes = [];225 for (let y = 0; y < matrix.length; y++) {226 result[y] = [];...
stringRotation.js
Source:stringRotation.js
...7// C: only use one call to isSubstring method8// E: empty strings, spaces, equal strings9const stringRotation = (s1, s2) => {10 const double = s1.concat(s1);11 return isSubstring(double, s2);12};13// I: two strings14// O: a boolean15// C: none16// E: empty strings -> index of '' in 'aNon-emptyString' === 017// determine if s2 is a substring of s118const isSubstring = (s1, s2) => s1.indexOf(s2) !== -1;19const assertEquals = (actual, expected, testname) => {20 if (actual === expected) {21 console.log(`passed ${testname}`);22 } else {23 console.log(`FAILED ${testname}: expected "${expected}", but got "${actual}"`);24 }25};26// tests27console.log('TESTS FOR: isSubstring()');28assertEquals(isSubstring('waterbottle', 'bottle'), true, 'should return true if s2 is substring of s1');29assertEquals(isSubstring('waterbottle', 'terbots'), false, 'should return false if s2 is not substring of s1');30assertEquals(isSubstring('', ''), true, 'should return true when passed two empty strings');31assertEquals(isSubstring('waterbottle', ''), true, 'should return true when s2 is empty string and s1 is not');32console.log('');33console.log('TESTS FOR: stringRotation()');34assertEquals(stringRotation('waterbottle', 'erbottlewat'), true, 'should return true when one string is a rotation of the other');35assertEquals(stringRotation('waterbottle', 'erbotstlewat'), false, 'should return false when one string is not a rotation of the other');36assertEquals(stringRotation('', ''), true, 'should return true when given two empty strings');37assertEquals(stringRotation('', 'watterbottle'), false, 'should return false when given an empty string and a non-empty string');38assertEquals(stringRotation('waterbottle', 'waterbottle'), true, 'should return true when both strings are equal');...
ctci-1.09.test.js
Source:ctci-1.09.test.js
1const { stringRotation } = require('./ctci-1.09.js');2const isSubstring = jest.fn().mockImplementation((str1, str2) => {3 return str1.includes(str2);4});5describe('Check if two strings are rotations of each other, calling isSubstring only once', () => {6 test('Return true if strings are rotations', () => {7 isSubstring.mockClear();8 expect(stringRotation('waterbottle', 'erbottlewat', isSubstring)).toBe(true);9 expect(isSubstring).toHaveBeenCalledTimes(1);10 });11 test('Return false if strings are not rotations', () => {12 isSubstring.mockClear();13 expect(stringRotation('foo', 'bar', isSubstring)).toBe(false);14 expect(isSubstring).toHaveBeenCalledTimes(1);15 });...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!