Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/isolate-duplicates/index.js
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 31 additions & 1 deletion src/morse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 21 additions & 1 deletion src/remove-dulplicates/index.js
Original file line number Diff line number Diff line change
@@ -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;