From 2c606a8032a2ce953db12555c593e3ecb5bb75fa Mon Sep 17 00:00:00 2001 From: Harney Hart Date: Tue, 15 Aug 2023 01:51:43 +0100 Subject: [PATCH] decrypt morse code,isolate duplicates and remove duplicates --- src/isolate-duplicates/index.js | 43 ++++++++++++++++++++++++++++++++- src/morse/index.js | 32 +++++++++++++++++++++++- src/remove-dulplicates/index.js | 22 ++++++++++++++++- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/isolate-duplicates/index.js b/src/isolate-duplicates/index.js index 79ae426..542f47c 100644 --- a/src/isolate-duplicates/index.js +++ b/src/isolate-duplicates/index.js @@ -1,3 +1,44 @@ -function isolateDuplicates(text) {} +function isolateDuplicates(text) { + let count = 0; + let newArr = []; + let duplicatesArr = []; + + // check if argument is a string and length more than zero + if (text.length === 0) { + throw Error("Please enter a valid string"); + } + if (typeof text !== 'string'){ + throw Error('Please enter a valid string') + } + + let capArr = text.toLowerCase(); // check case sensitivity + + let textArr = capArr.split(''); // convert string to array + + for(let i = 0; i < textArr.length + 1; i++){ + + if(textArr[i -1] === textArr[i] && textArr[i -2] === textArr[i]){ + duplicatesArr.push(text[i]); //check if occurrence of character is more than two in a sequence then push >2 into duplicate array + }else{ + if(duplicatesArr.length !== 0){ + count +=1; // for every duplicate array increase count + duplicatesArr = duplicatesArr.join(''); // convert entire array to string + duplicatesArr = `[${duplicatesArr}]`; //group strings as an array + newArr.push(duplicatesArr); // push duplicate array into next index of new array + } + newArr.push(text[i]); // if item is unique or first character push directly into new array + duplicatesArr = []; // empty duplicates array, until another occurence of more than two characters in a sequence + } + } + + let strLength = newArr.length + if(newArr[strLength - 1] === undefined){ + newArr.splice(strLength, 1); // if last character in new array is equal to undefined, remove from array. + } + + newArr = newArr.join(''); // convert final array to string + return [newArr, count]; + } + module.exports = isolateDuplicates; diff --git a/src/morse/index.js b/src/morse/index.js index cf65063..46ddc1f 100644 --- a/src/morse/index.js +++ b/src/morse/index.js @@ -58,6 +58,36 @@ const MORSE_CODE = { Object.freeze(MORSE_CODE); -function morse(text) {} +function morse(text) { + let result = ''; // initialize final string + let resultArr = []; // initialize holding array + + + if(typeof text == 'string' && typeof text != 'object'){ + const words = text.split(' '); // split array into words and push into words array, using 3 spaces + + for(const word of words){ // iterate through words array, containing each word + const chars = word.split(' '); // split each word into individual characters and push into chars array using 1 space + + for(const char of chars){ // iterate through each character in chars array + const trimmedChar = char.trim(); + if(MORSE_CODE[trimmedChar]){ // check if character matches object value in MORSE_CODE Object + resultArr.push(MORSE_CODE[char]); // if it does push into holding array + } + + } + + resultArr.push(' '); // add a space charcter after a characters of a word has been pushed into holding array + } + + result = resultArr.join('').trim(''); // convert holding array to string, join all characters to make words and spaces. the trim leading and trailing white spaces + return result; //return final sentence as a string + }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..0ea3750 100644 --- a/src/remove-dulplicates/index.js +++ b/src/remove-dulplicates/index.js @@ -1,3 +1,23 @@ -function removeDuplicates(obj) {} +function removeDuplicates(obj) { + let result = {}; // object will store the filtered arrays for each key. + let accumulator = [] // array will keep track of all unique values encountered across the arrays in the object keys + let objKey = Object.keys(obj); //Gets an array of all the keys sorted in ascending order + + + for(let i = objKey.length -1; i >= 0; i--){ //iterate over the keys of the object in reverse order to ensure numerically higher keys hold most occuring values + let keyValues = obj[objKey[i]]; // gets values associated with the current key + let filteredVals= []; // array to temporarily hold filtered values of current key + + for(let j = 0; j < keyValues.length;j++){ // loop iterates over each value in the keys array + if(!accumulator.includes(keyValues[j])){ //Checks if the current value is not already present in the accumulator array. + accumulator.push(keyValues[j]); // If the value is unique, it is pushed into the accumulator array + filteredVals.push(keyValues[j]); // The unique value is also pushed into the filteredVals array for the current key. + } + } + + result[objKey[i]] = filteredVals; // Assigns the filteredVals array to the current key in the result object. then empties filterdVals + } + return result; +} module.exports = removeDuplicates;