diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..3e8ec62 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,3 +1,30 @@ -function isolateDuplicates(text) {} - -module.exports = isolateDuplicates; +function isolateDuplicates(text) { + if (typeof text !== 'string'){ + throw new Error("Please enter a valid string"); + } + let tracker = 0; + let array = [] + let array2 = [] + let newText = text.toUpperCase() + + for(let i = 0; i < newText.length; i++){ + if(newText[i] === newText[i-1] && newText[i] === newText[i+1] && newText[i-2] !== newText[i]){ + + + array.push('['); + array.push(text[i]); + tracker += 1; + } else if (newText[i] === newText[i -1] && newText[i] === newText[i-2] && newText[i+1] !== newText[i]){ + array.push(text[i]); + array.push(']') + tracker += 1; + } else { + array.push(text[i]) + } + } + let stringOut = array.join('') + array2.push(stringOut, tracker) + return array2 + } + console.log(isolateDuplicates("aaaabbcccadehhh")) + module.exports = isolateDuplicates; \ No newline at end of file diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..fc19bb6 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -58,6 +58,41 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); -function morse(text) {} +decodeMorse = function(morseCode) { + + if (typeof text !== "string"){ + throw "Please provide a morse string" + } else if (text.length === 0){ + return "" + } + let text2 = text.trim() + let morArr = text2.split(" ") + // + //console.log(morArr) + + let strArr = [] + let count = 0; + for (let i = 0; i < morArr.length; i++){ + //console.log(MORSE_CODE[morArr[i]]) + //console.log(morArr[i].trim()) + + let char = morArr[i] + if(char == ""){ + count++ + }else{ + if(count>=2) strArr.push(" "); + //console.log(strArr) + + strArr.push(MORSE_CODE[char]); + count = 0; + + } + //console.log(strArr) + } + + return strArr.join('') + } + +console.log(morseCode("..- .-... ..")) module.exports = morse; diff --git a/src/remove-dulplicates/README.md b/src/remove-dulplicates/README.md index 2453d46..ec1a1c6 100644 --- a/src/remove-dulplicates/README.md +++ b/src/remove-dulplicates/README.md @@ -1,4 +1,4 @@ -# Many duplicates + # Many duplicates You are given an object, in which every property is an array of strings. E.g. @@ -12,7 +12,7 @@ You are given an object, in which every property is an array of strings. E.g. Create a function that returns an object with the same keys, but each string can only appear once. E.g. ```json -{ +{ "1": ["C"], "2": ["A", "B", "D"] } diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index 75c6c9b..1d1c5a0 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,21 @@ -function removeDuplicates(obj) {} +function removeDuplicates(obj) { + let c = []; + let diff = (a, b) => [...new Set([...a].filter((x) => !b.includes(x)))]; + return Object.entries(obj) + .reverse() + .map((entry) => { + const subCollector = c; + c = [...new Set([...c, ...entry[1]])]; + return [entry[0], [...new Set(diff(entry[1], subCollector))]]; + }) + .reduce((arrays, array) => ((arrays[array[0]] = array[1]), arrays), {}); + } -module.exports = removeDuplicates; + console.log(removeDuplicates({ + "432": ["A", "A", "B", "D"], + "53": ["L", "G", "B", "C"], + "236": ["L", "A", "X", "G", "H", "X"], + "11": ["P", "R", "S", "D"] + })) + + module.exports = removeDuplicates; \ No newline at end of file