Code Wars: Where is my parent!?(cry) / Also lesson on iterating thru loop and removing items at same time
Mothers arranged dance party for children in school.On that party there are only mothers and their children.All are having great fun on dancing floor when suddenly all lights went out.Its dark night and no one can see eachother.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" mothers children are "aaaa".-Function input:String contain only letters,Uppercase letters are unique.
Task:
Place all people in alphabetical order where Mothers are followed by their children.I.E "aAbaBb" => "AaaBbb".https://stackoverflow.com/questions/9882284/looping-through-array-and-removing-items-without-breaking-for-loop
lexicographical order meaning: https://www.youtube.com/watch?v=LtlZtFXe8Io
My solution:
function findChildren(dancingBrigade){
let arr = dancingBrigade.split('').sort();
let capitals = [];
for (i = arr.length - 1; i >= 0; i--) { //loop backwards to remove and iterate at same time
if (arr[i] === arr[i].toUpperCase()) {
capitals.push(arr.splice(i, 1).toString());
}
}
for (i = 0; i < capitals.length; i++) {
let capLetter = capitals[i].toLowerCase();
if (arr.indexOf(capLetter) !== -1) {
arr.splice(arr.indexOf(capLetter), 0, capitals[i]);
}
}
return arr.join('');
};
Best practice to use localeCompare()
ReplyDeletehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare