From 064961525e1caf42a2b06cbc8f714738072611ee Mon Sep 17 00:00:00 2001 From: mac Date: Sun, 13 Aug 2023 22:13:13 +0100 Subject: [PATCH 1/3] Solution to mini-challenge-2 task --- src/isolate-duplicates/index.js | 41 ++++++++++++++++++++++++++++++++- src/morse/index.js | 28 +++++++++++++++++++++- src/remove-dulplicates/index.js | 30 +++++++++++++++++++++++- 3 files changed, 96 insertions(+), 3 deletions(-) diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..da612be 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++) { + + 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; diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..81852cb 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -58,6 +58,32 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); -function morse(text) {} +// Tip: A single space is used to separate the character codes and 3 spaces are used to separate words. + +function morse(text) { + + if(typeof text === 'string'){ + + const arr = text.split(' '); + const result = []; + + for(let i of arr){ + let currWord = i.split(' '); + let curArr = [] + for(let j of currWord){ + curArr.push(MORSE_CODE[j]) + } + result.push(curArr.join('')) + } + + return result.join(' ').trim(); + + }else if(text === ""){ + return ""; + } + else { + throw Error("Please provide a morse string"); + } +} module.exports = morse; diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index 75c6c9b..e49837b 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,31 @@ -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 = []; + let curr = obj[objKeys[i]]; + + 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; From 1fa6d548f5cf64539ac50bc86bdde90cfffa4a4c Mon Sep 17 00:00:00 2001 From: mac Date: Sun, 13 Aug 2023 22:31:36 +0100 Subject: [PATCH 2/3] Solution to mini-challenge-2 task --- src/morse/index.js | 10 +++++----- src/remove-dulplicates/index.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/morse/index.js b/src/morse/index.js index 81852cb..356ec9f 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -68,12 +68,12 @@ function morse(text) { const result = []; for(let i of arr){ - let currWord = i.split(' '); - let curArr = [] - for(let j of currWord){ - curArr.push(MORSE_CODE[j]) + let currInnerArr = i.split(' '); + let currChar = [] + for(let j of currInnerArr){ + currChar.push(MORSE_CODE[j]) } - result.push(curArr.join('')) + result.push(currChar.join('')) } return result.join(' ').trim(); diff --git a/src/remove-dulplicates/index.js b/src/remove-dulplicates/index.js index e49837b..23f8d52 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -6,8 +6,8 @@ function removeDuplicates(obj) { const prev = []; for(let i= 0; i < objKeys.length; i++){ - let newArr = []; - let curr = obj[objKeys[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){ From dc437af22a3fa8b2bad7e6726d01ba1cceb5a7f3 Mon Sep 17 00:00:00 2001 From: mac Date: Sat, 19 Aug 2023 16:33:08 +0100 Subject: [PATCH 3/3] finished line --- src/morse/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/morse/index.js b/src/morse/index.js index 356ec9f..1f5521d 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -69,11 +69,11 @@ function morse(text) { for(let i of arr){ let currInnerArr = i.split(' '); - let currChar = [] + let currChar = []; for(let j of currInnerArr){ currChar.push(MORSE_CODE[j]) } - result.push(currChar.join('')) + result.push(currChar.join('')); } return result.join(' ').trim();