How can you help?
You can support by buying a coffee ☕️
Follow me on Github
Follow me on Twitter
Instructions
Mothers arranged a dance party for the children in school. At that party, there are only mothers and their children. All are having great fun on the dance floor when suddenly all the lights went out. It's a dark night and no one can see each other. But you were flying nearby and you can see in the dark and have ability to teleport people anywhere you want.
Legend:
-Uppercase letters stands for mothers, lowercase stand for their children, i.e. "A" mother's children are "aaaa".
-Function input: String contains only letters, uppercase letters are unique.
Task:
Place all people in alphabetical order where Mothers are followed by their children, i.e. "aAbaBb" => "AaaBbb".
My solution:
function findChildren(str) {
str = str.toLowerCase()
// beeeebb
let arr = str.split('')
// [ 'b', 'e', 'e', 'e', 'e', 'b', 'b' ]
var r = '';
let letters = [...new Set(arr)].sort()
// ['b','e']
for(let i = 0; i<letters.length; i++){
// Will iterate ['b','e']
for(let j = 0; j<str.length; j++){
// Will iterate // [ 'b', 'e', 'e', 'e', 'e', 'b', 'b' ]
if(letters[i] == arr[j]) r += arr[j]
}
}
return r.split('').map((x,i)=> x !== r[i-1] ? x = x.toUpperCase() : x ).join('')
// in the map, if the actual letter isn't the same as the last one it'll make it upperCase becuase it is the first one of those group
}
Explanation
First I converted the string to lower case, and then I splitted it into an array, and I made the variable "r" with an empty string, for saving the result there
str = str.toLowerCase()
// beeeebb
let arr = str.split('')
// [ 'b', 'e', 'e', 'e', 'e', 'b', 'b' ]
var r = '';
Then I made the variable "letters" I used a new Set() into the array with the letters so I could get the letters that the array contains, and then I sorted it so I could get it in alphabetical order.
let letters = [...new Set(arr)].sort()
// ['b','e']
Then I used 2 for loops, the first one to iterate the Ungrouped letters that the string contains and the second one to iterate all the letters that the string contains mixed.
Inside of the second loop I used a conditional, if the letter that the group contains is equal to the letter being iterated on the mixed string, it'll added to "r"
for(let i = 0; i<letters.length; i++){
// Will iterate ['b','e']
for(let j = 0; j<str.length; j++){
// Will iterate // [ 'b', 'e', 'e', 'e', 'e', 'b', 'b' ]
if(letters[i] == arr[j]) r += arr[j]
}
}
At the end "r" is equal to a string with the letters in order but without the first letter being a capital letter ('bbbeeee'), and I want it to be ('BbbEeee').
So I splitted "r" into an array and then I mapped it, if the current letter isn't equal to the letter behind it it means it needs to be Capital because it's the first one of the group, so I make it upperCase, if not I just leave as it is.
And I return this result
return r.split('').map((x,i)=> x !== r[i-1] ? x = x.toUpperCase() : x ).join('')
// in the map, if the actual letter isn't the same as the last one it'll make it upperCase becuase it is the first one of those group
What do you think about this solution? 👇🤔
Solve this Kata 👨🏽💻
Top comments (0)