From d69d9e99eab83c81bc09bf634941077c141e49ed Mon Sep 17 00:00:00 2001 From: Vhal Date: Wed, 16 Aug 2023 11:28:19 +0100 Subject: [PATCH] Second mini-challenges-2 --- src/isolate-duplicates/index.js | 44 +++++++++++++++++- src/morse/index.js | 44 +++++++++++++++++- src/remove-dulplicates/index.js | 81 ++++++++++++++++++++++++++++++++- 3 files changed, 166 insertions(+), 3 deletions(-) diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..1db1d64 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,3 +1,45 @@ -function isolateDuplicates(text) {} +function isolateDuplicates(text) { + + if (text === undefined){ + throw Error(); + } + if (text.length === 0) { + throw Error ("Please enter a valid string"); + } + let firstArr = []; + let secondArr = []; + let count = 0; + let str = text.toLowerCase(); + + for (let i = 0; i < str.length + 1; i++) { + if (str[i - 1] === str[i] && str[i - 2] === str[i]) { + secondArr.push(text[i]); + } + else { + if (secondArr.length !== 0) { + count += 1; + secondArr = secondArr.join(""); + //secondArr.push + firstArr.push(`[${secondArr}]`); + } + if (secondArr.length !== undefined){ + firstArr.push(text[i]); + } + secondArr = []; + } + } + if (secondArr.length !== 0){ + count += 1; + secondArr = secondArr.join(""); + firstArr.push(`[${secondArr}]`) + } + return [firstArr.join(""), count] + } + +let userInput = "hellllloooooodeeeecadevvsss" //prompt("Enter a string:"); + let result = isolateDuplicates(userInput); + + console.log(result); + module.exports = isolateDuplicates; diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..d0e2aaa 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -58,6 +58,48 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); -function morse(text) {} +function morse(text) { + if(text === ""){ + //console.log(text) + return ""; + } + if(text === undefined) { + //console.log(text) + throw Error () + } + if (text.length === 0){ + throw Error('Please provide a morse string') + } + if (typeof text == "string" ) { + console.log(text) + if (text.includes('-') || text.includes('.')) { + // Input is Morse code, decode to text + const words = text.trim().split(' '); + + const decodedMorse = words.map(word => { + const sequences = word.split(' '); + + const decodedCharacters = sequences.map(sequence => { + return MORSE_CODE[sequence]; + }); + //console.log(decodedCharacters) + return decodedCharacters.join(''); + }); +//console.log(decodedMorse) + return decodedMorse.join(' '); + // } else { + // throw new Error('Please provide a Morse code string.'); + } + + } +} + + const morseCode = "-.. . -.-. .- -.. . ...-"; + const decodedMessage = morse(morseCode); // Pass the morseCode as input to the function + console.log(decodedMessage); + console.log(morse('')) + + console.log(typeof []) + module.exports = morse; diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index 75c6c9b..34503de 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,82 @@ -function removeDuplicates(obj) {} +function removeDuplicates(obj) { + let outputObject = {}; + let sortedKeys = Object.keys(obj).map(key => Number(key)).sort((a, b) => b - a); +console.log(sortedKeys) + sortedKeys.forEach(key => { + let inputArray = obj[key]; + let uniqueArray = []; + + inputArray.forEach(item => { + // Check if the item is already in the uniqueArray or any other array + if (!uniqueArray.includes(item) && !isStringInOtherArray(outputObject, item, key)) { + uniqueArray.push(item); + } + }); + + if (uniqueArray.length >= 0) { + outputObject[key] = uniqueArray; + } + }); + + return outputObject; +} + +// Helper function to check if a string is in any other array +function isStringInOtherArray(object, item, currentKey) { + for (let key in object) { + if (object.hasOwnProperty(key) && key !== currentKey) { + if (object[key].includes(item)) { + return true; + } + } + } + return false; +} + + + // Helper function to check if a string is in any other array + function isStringInOtherArray(object, item, currentKey) { + for (let key in object) { + if (object.hasOwnProperty(key) && key !== currentKey) { + if (object[key].includes(item)) { + return true; + } + } + } + return false; + } + + let arryObj = { + "4": ["A"], + "10": ["B"], + "3": ["C"], + "1": ["D"], + "2": ["E"], + "26": ["F", "Z"], + "5": ["G"], + "8": ["H"], + "11": ["I", "B"], + "12": ["J", "I"], + "13": ["K", "J"], + "14": ["L", "K"], + "15": ["M", "L"], + "7": ["N"], + "6": ["O"], + "16": ["P", "M"], + "9": ["Q"], + "17": ["R", "P"], + "18": ["S", "R"], + "19": ["T", "S"], + "20": ["T", "U"], + "21": ["V", "T"], + "22": ["W", "V"], + "23": ["X", "W"], + "24": ["X", "Y"], + "25": ["Y", "Z"], + }; + + let result = removeDuplicates(arryObj); + console.log(result); + module.exports = removeDuplicates;