├── 01-good-string-bad-string └── script.js ├── 02-bending-snake └── script.js ├── 03-cooking-recipe └── script.js └── 04-number-guessing-game ├── index.html └── script.js /01-good-string-bad-string/script.js: -------------------------------------------------------------------------------- 1 | // Joe has two strings, s and t, which are both made up of lowercase English letters. 2 | // He loves the string s very much and dislikes the string t. 3 | // He is looking for a string of length n such that this string consists of lowercase English letters, 4 | // s is a substring of it, and t is not a substring of it. 5 | 6 | // Help him find such a string and print a valid example. If such a string does not exist, print -1. 7 | 8 | // A string a is a substring of string b if and only if there exists a contiguous segment of string\ 9 | // b that is equal to string a. 10 | 11 | // "hamcode" is not a substring of "hamkaran" because the sequence "hamcode" does not appear 12 | // consecutively in "hamkaran" 13 | 14 | function generateMainString(len, sLen) { 15 | const chars = "abcdefghijklmnopqrstuvwxyz"; 16 | const mainString = []; 17 | const attempt = len - sLen; 18 | 19 | for (let i = 0; i < attempt; i++) { 20 | const index = Math.floor(Math.random() * len + 1); 21 | mainString.push(chars[index]); 22 | } 23 | 24 | return mainString.join(""); 25 | } 26 | 27 | function findValidString(s, t, len) { 28 | if (len < s.length) return -1; 29 | 30 | if (len === s.length) { 31 | return s.includes(t) ? -1 : s; 32 | } 33 | 34 | const generatedMainString = generateMainString(len, s.length); 35 | console.log("1: ", generatedMainString); 36 | const mainString = s + generatedMainString; 37 | console.log(mainString); 38 | 39 | if (mainString.includes(s) && !mainString.includes(t)) return mainString; 40 | else return -1; 41 | } 42 | 43 | module.exports = { 44 | findValidString, 45 | }; 46 | 47 | // npx jest good-string-bad-string.test.js 48 | -------------------------------------------------------------------------------- /02-bending-snake/script.js: -------------------------------------------------------------------------------- 1 | // there a snake in a 2 * 8 column , snake is at the row 1 column 1, snake movements are as follows: 2 | // L : In the left row, it moves to the opposite square 3 | // R : In the right row, it moves to the opposite square 4 | // F : In the same, it goes to the next square 5 | 6 | // input : a string of length 7 with L , R , F 7 | // output : two strings (because of having two rows) of length 7, 0 indicates the absence of the snake 8 | // in that square and 1 indicates the presence of it 9 | 10 | function bendingSnake(moves) { 11 | const container = [ 12 | [0, 0, 0, 0, 0, 0, 0, 0], 13 | [1, 0, 0, 0, 0, 0, 0, 0], 14 | ]; 15 | 16 | const maxMovement = 7; 17 | 18 | let row = 1; 19 | let col = 0; 20 | 21 | if (moves.length < maxMovement) return; 22 | 23 | for (let move of moves) { 24 | if (move === "L") { 25 | if (row === 0) return "DEATH"; 26 | 27 | row--; 28 | col++; 29 | container[row][col] = 1; 30 | } else if (move === "R") { 31 | if (row === 1) return "DEATH"; 32 | 33 | row++; 34 | col++; 35 | container[row][col] = 1; 36 | } else if (move === "F") { 37 | if (col >= 7) return "DEATH"; 38 | 39 | col = col + 1; 40 | container[row][col] = 1; 41 | } else return "Unknown Move"; 42 | } 43 | 44 | return container.map((item) => item.join("")).join("\n"); 45 | } 46 | 47 | module.exports = { 48 | bendingSnake, 49 | }; 50 | -------------------------------------------------------------------------------- /03-cooking-recipe/script.js: -------------------------------------------------------------------------------- 1 | Function: doneMessage; 2 | // This function is responsible for directly displaying a message in the console with the following text and will 3 | // always be used as a callback function in the upcoming functions: 4 | // [prevStep] done, next step ... 5 | 6 | // Previous steps titles: 7 | // preparing, making, serving, and eating. 8 | 9 | // Note: The output of all the upcoming functions will be a successfully resolved Promise with the message 10 | // returned by the callback function. 11 | 12 | // Function: prepare 13 | // This function is responsible for preparing the food ingredients. It takes two parameters: 14 | 15 | // An array of strings containing the ingredients. 16 | // A callback function, which must be called after a delay of 500ms. 17 | // Before executing the main process, the following text should be displayed in the console: 18 | 19 | // preparing stuffs: eggs, tomatoes 20 | 21 | // Function: cooking 22 | // This function is responsible for cooking the food. It takes a callback function as input, 23 | // which must be called after a delay of 2000ms. 24 | 25 | // Before executing the main process, the following text should be displayed in the console: 26 | 27 | // making an omelette ... 28 | 29 | // Function: serve 30 | // This function is responsible for serving and plating the food. It takes a callback function as input, 31 | // which must be called after a delay of 500ms. 32 | 33 | // Before executing the main process, the following text should be displayed in the console: 34 | 35 | // serving food ... 36 | 37 | // Function: eat 38 | // This function is responsible for eating the food! It takes a callback function as input, 39 | // which must be called after a delay of 1000ms. 40 | 41 | // Before executing the main process, the following text should be displayed in the console: 42 | 43 | // eating ... 44 | 45 | /* --------------------------------- example -------------------------------- */ 46 | 47 | // startCooking(["eggs", "tomatoes"]); 48 | 49 | // preparing stuffs: eggs, tomatoes 50 | // preparing done, next step ... 51 | // making an omelette ... 52 | // making done, next step ... 53 | // serving food ... 54 | // serving done, next step ... 55 | // eating ... 56 | // eating done, next step ... 57 | // process is done 58 | 59 | function prepare(ingredients, fn) { 60 | return new Promise((resolve) => { 61 | console.log(`preparing stuffs: ${[...ingredients].join(", ")}`); 62 | 63 | setTimeout(() => { 64 | resolve(fn()); 65 | }, 500); 66 | }); 67 | } 68 | 69 | function cooking(fn) { 70 | console.log("making an omelette ..."); 71 | 72 | return new Promise((resolve) => { 73 | setTimeout(() => { 74 | resolve(fn()); 75 | }, 2000); 76 | }); 77 | } 78 | 79 | function serve(fn) { 80 | console.log("serving food ..."); 81 | 82 | return new Promise((resolve) => { 83 | setTimeout(() => { 84 | resolve(fn()); 85 | }, 500); 86 | }); 87 | } 88 | 89 | function eat(fn) { 90 | console.log("eating ..."); 91 | 92 | return new Promise((resolve) => { 93 | setTimeout(() => { 94 | resolve(fn()); 95 | }, 1000); 96 | }); 97 | } 98 | 99 | function doneMessage(prevStep) { 100 | return () => console.log(`${prevStep} done, next step ...`); 101 | } 102 | 103 | function startCooking(stuffs) { 104 | prepare(stuffs, doneMessage("preparing")) 105 | .then(() => { 106 | return cooking(doneMessage("making")); 107 | }) 108 | .then(() => { 109 | return serve(doneMessage("serving")); 110 | }) 111 | .then(() => { 112 | return eat(doneMessage("eating")); 113 | }) 114 | .then(() => { 115 | console.log("process is done"); 116 | }); 117 | } 118 | 119 | startCooking(["eggs", "tomatoes"]); 120 | 121 | module.exports = { prepare, cooking, serve, eat, doneMessage }; 122 | -------------------------------------------------------------------------------- /04-number-guessing-game/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 |15 | We have selected a random number between 1 and 100. See if you can guess it in 10 turns or fewer. We'll tell 16 | you if your 17 | guess was too high or too low. 18 |
19 | 20 |