From 618605641bda3f62bf0435727045ea66e4d178ba Mon Sep 17 00:00:00 2001 From: philipObinna Date: Wed, 16 Aug 2023 12:31:51 +0100 Subject: [PATCH] mini challenge 2 --- .husky/lessons/gitone | 0 .husky/lessons/gittwo | 0 .husky/lessons/inex.js | 137 ++++++++++++++++++++++++++++++++ package-lock.json | 48 +++++------ src/isolate-duplicates/index.js | 43 +++++++++- src/morse/index.js | 48 ++++++++++- src/remove-dulplicates/index.js | 32 +++++++- 7 files changed, 278 insertions(+), 30 deletions(-) create mode 100644 .husky/lessons/gitone create mode 100644 .husky/lessons/gittwo create mode 100644 .husky/lessons/inex.js diff --git a/.husky/lessons/gitone b/.husky/lessons/gitone new file mode 100644 index 0000000..e69de29 diff --git a/.husky/lessons/gittwo b/.husky/lessons/gittwo new file mode 100644 index 0000000..e69de29 diff --git a/.husky/lessons/inex.js b/.husky/lessons/inex.js new file mode 100644 index 0000000..95877e2 --- /dev/null +++ b/.husky/lessons/inex.js @@ -0,0 +1,137 @@ +// ## Morse + +// The Morse code encodes every character as a sequence of "dots" and "dashes". + +// For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−. + +// The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. + +// For example, the message `Decadev` in Morse code is -.. . -.-. .- -.. . ...- + +// NOTE: Extra spaces before or after the code have no meaning and should be ignored. In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words. +// Your task is to implement a function that would take the morse code as input and return a decoded human-readable string. + +// For example. + +// ```js +// decodeMorse("-.. . -.-. .- -.. . ...-"); +// //should return "DECADEV" +// ``` + +// Use the constant `MORSE_CODE` below to translate. + +// ```js + +// const morseCode = { +// "-.-.--": "!", +// ".-..-.": '"', +// "...-..-": "$", +// ".-...": "&", +// ".----.": "'", +// "-.--.": "(", +// "-.--.-": ")", +// ".-.-.": "+", +// "--..--": ",", +// "-....-": "-", +// ".-.-.-": ".", +// "-..-.": "/", +// "-----": "0", +// ".----": "1", +// "..---": "2", +// "...--": "3", +// "....-": "4", +// ".....": "5", +// "-....": "6", +// "--...": "7", +// "---..": "8", +// "----.": "9", +// "---...": ":", +// "-.-.-.": ";", +// "-...-": "=", +// "..--..": "?", +// ".--.-.": "@", +// ".-": "A", +// "-...": "B", +// "-.-.": "C", +// "-..": "D", +// ".": "E", +// "..-.": "F", +// "--.": "G", +// "....": "H", +// "..": "I", +// ".---": "J", +// "-.-": "K", +// ".-..": "L", +// "--": "M", +// "-.": "N", +// "---": "O", +// ".--.": "P", +// "--.-": "Q", +// ".-.": "R", +// "...": "S", +// "-": "T", +// "..-": "U", +// "...-": "V", +// ".--": "W", +// "-..-": "X", +// "-.--": "Y", +// "--..": "Z", +// "..--.-": "_", +// "...---...": "SOS", +// }; + + +// function morse (string) { + +// let newCode = morseCode.trim() +// console.log(newCode) + + + // let a = "phil" + // let b = "grace" + // let c = "favour" + + // console.log(`my name is ${a.charAt()} ${b}`) + + +var musicians = 0; + + +if (musicians <= 0) { + console.log("not a group"); +} else if (musicians === 1) { + console.log("solo"); +} else if (musicians === 2) { + console.log("duet"); +} else if (musicians === 3) { + console.log("trio"); +} else if (musicians === 4) { + console.log("quartet"); +} else if (musicians >= 5) { + console.log("this is a large group"); +} + +console.log(musicians === 3) + + +// for (let i = 0; i < string.length; i++) { +// let code = ""; +// if (morseCode[x] === Object.values(morseCode)) { +// return true; +// } else { +// return false +// } + + + + + + + + + + +// } +// console.log(morse[".."]) +// // let newMorse = Object.values(morseCode); +// console.log(newMorse) diff --git a/package-lock.json b/package-lock.json index 94d9068..71155be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2431,9 +2431,9 @@ } }, "node_modules/jest-snapshot/node_modules/semver": { - "version": "7.3.7", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz", - "integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" @@ -2577,9 +2577,9 @@ "dev": true }, "node_modules/json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true, "bin": { "json5": "lib/cli.js" @@ -3046,9 +3046,9 @@ "dev": true }, "node_modules/semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true, "bin": { "semver": "bin/semver.js" @@ -3342,9 +3342,9 @@ } }, "node_modules/ts-jest/node_modules/semver": { - "version": "7.3.7", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz", - "integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" @@ -5361,9 +5361,9 @@ }, "dependencies": { "semver": { - "version": "7.3.7", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz", - "integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", "dev": true, "requires": { "lru-cache": "^6.0.0" @@ -5474,9 +5474,9 @@ "dev": true }, "json5": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.1.tgz", - "integrity": "sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA==", + "version": "2.2.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz", + "integrity": "sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==", "dev": true }, "kleur": { @@ -5816,9 +5816,9 @@ "dev": true }, "semver": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", - "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==", + "version": "6.3.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", + "integrity": "sha512-BR7VvDCVHO+q2xBEWskxS6DJE1qRnb7DxzUrogb71CWoSficBxYsiAGd+Kl0mmq/MprG9yArRkyrQxTO6XjMzA==", "dev": true }, "shebang-command": { @@ -6023,9 +6023,9 @@ }, "dependencies": { "semver": { - "version": "7.3.7", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.7.tgz", - "integrity": "sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", + "integrity": "sha512-1bCSESV6Pv+i21Hvpxp3Dx+pSD8lIPt8uVjRrxAUt/nbswYc+tK6Y2btiULjd4+fnq15PX+nqQDC7Oft7WkwcA==", "dev": true, "requires": { "lru-cache": "^6.0.0" diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..4f08aaf 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,3 +1,42 @@ -function isolateDuplicates(text) {} +function isolateDuplicates(text) { + if (typeof text !== 'string') { + throw Error("Please enter a valid string"); + } + + let result = ''; + let duplicateCount = 0; + let bracketCount = 0 + let currentChar = ''; + + for (let i = 0; i < text.length; i++) { -module.exports = isolateDuplicates; + if (text[i].toLowerCase() === currentChar.toLowerCase()) { + if(duplicateCount == 1 ) { + result += '['; + bracketCount++ + } + duplicateCount++; + } else { + if(duplicateCount > 1){ + result += ']'; + } + duplicateCount = 0; + } + + currentChar = text[i]; + result += currentChar; + } + + for(let i = result.length - 1; i > 0; i--){ + if(result[i] == '['){ + result += ']'; + return [result, bracketCount]; + }else if(result[i] == ']'){ + return [result, bracketCount]; + } + } + + return [result, bracketCount]; +} + +module.exports = isolateDuplicates; \ No newline at end of file diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..07c7f7a 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -56,8 +56,50 @@ const MORSE_CODE = { "...---...": "SOS", }; -Object.freeze(MORSE_CODE); +// let txt = ""; +// for (let x in MORSE_CODE) { +// txt += MORSE_CODE[x] ; +// console.log(txt) +// }; -function morse(text) {} +// document.getElementById(txt); +// com -module.exports = morse; +// Object.freeze(MORSE_CODE); + +// function morse(text) {} + +// module.exports = morse; + + +// let text = "5"; +// text = text.padStart(3,"0"); + +// document.getElementById("demo").innerHTML = text; +// console.log() + +function morse(text) { + + if(typeof text === 'string'){ + + const arr = text.split(' '); + const result = []; + + for(let i of arr){ + let currInnerArr = i.split(' '); + let currChar = []; + for(let j of currInnerArr){ + currChar.push(MORSE_CODE[j]) + } + result.push(currChar.join('')); + } + + return result.join(' ').trim(); + + }else if(text === ""){ + return ""; + } + else { + throw Error("Please provide a morse string"); + } +} \ No newline at end of file diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index 75c6c9b..e3990e2 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,33 @@ -function removeDuplicates(obj) {} +function removeDuplicates(obj) { + + const newObj = {}; + const objKeys = Object.keys(obj).reverse(); + + const prev = []; + + for(let i= 0; i < objKeys.length; i++){ + let newArr = []; // Contain for all unique characters in each arrays in object. + let curr = obj[objKeys[i]]; // Current array in each object. + + for(let j = 0; j < curr.length; j++){ + if(i === 0){ + newArr.push(curr[j]); + prev.push(curr[j]); + } + if(i > 0 && !prev.includes(curr[j])){ + newArr.push(curr[j]); + prev.push(curr[j]); + } + } + + // Remove the duplicate and assign to object key + newObj[objKeys[i]] = newArr.reduce((a, b) => !a.includes(b) ? [...a, b] : a, []); + + } + + return newObj +} + + module.exports = removeDuplicates;