From 2ebe6fb696f577fbace82e1f6c002ea3239f2bfc Mon Sep 17 00:00:00 2001 From: mac Date: Tue, 15 Aug 2023 22:09:41 +0100 Subject: [PATCH] Completed mini-challenge-2 --- src/isolate-duplicates/index.js | 44 ++++++++++++++++++++++++++++++++- src/morse/index.js | 14 ++++++++++- src/remove-dulplicates/index.js | 19 ++++++++++++-- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..cf23dc8 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,3 +1,45 @@ -function isolateDuplicates(text) {} +function isolateDuplicates(text) { + if (typeof text !== 'string') { + throw new Error("Please enter a valid string"); // Create an Error object with the message + } + + let duplicate = {}; + let newString = ""; + let str = ""; + let count = 0; + let stringCount = []; + let textNew = text.toLowerCase(); + + for (let i = 0; i < textNew.length; i++) { + if (newString !== textNew[i]) { + if (duplicate[newString] > 2) { + str += ']'; + } + duplicate[newString] = 0; + newString = textNew[i]; + } + + if (!duplicate[textNew[i]]) { + duplicate[textNew[i]] = 1; + } else if (duplicate[textNew[i]] === 1) { + duplicate[textNew[i]]++; + } else if (duplicate[textNew[i]] === 2) { + str += '['; + duplicate[textNew[i]]++; + count++; + } + + str += text[i]; + } + + if (duplicate[newString] > 2) { + str += ']'; + } + + stringCount.push(str); + stringCount.push(count); + + return stringCount; // Return the updated stringCount array +} module.exports = isolateDuplicates; diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..b76c99f 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -58,6 +58,18 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); -function morse(text) {} +function morse(text) { + if (typeof text !== 'string') throw "Please provide a morse string"; + const words = text.trim().split(" "); // Split input into words + const decodedWords = words.map(word => { + const letters = word.split(" "); // Split word into letters + const decodedLetters = letters.map(letter => { + return MORSE_CODE[letter]; // Translate each letter using MORSE_CODE + }); + return decodedLetters.join(""); // Join letters to form word + }); + + return decodedWords.join(" "); // Join words to form final decoded string + } module.exports = morse; diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index 75c6c9b..13a603a 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,18 @@ -function removeDuplicates(obj) {} - +function removeDuplicates(obj) { + let compare = []; + let newObj = {}; + let key = Object.keys(obj); + for (let i = key.length-1; i >= 0; i--){ + let value = obj[key[i]]; + let newArr = []; + for (let j = 0; j