# A String of Sorts

Guys I’m unable to solve this algorithm with my code but able with someone else’s code. the thing is, both code spawns the same results. i spent at least 3-4 hours analysing but couldn’t tell the difference. Would someone kindly enlighten me as why my code doesn’t work? Challenge: https://www.codewars.com/kata/a-string-of-sorts/train/javascript

My code:

``````function sortString(string,ordering) {

let str = "";
let unmatched = "";
let arr = [];

for ( let u = 0; u < string.length; u++) {
if (!ordering.includes(string[u]) ) {

unmatched+=string[u]
}
}

for (let i = 0; i < ordering.length; i++ ) {
let j = 0;
let incr = 0;

while ( j < string.length) {

if (!ordering.includes(string[j])) {
arr.push(string[j])
}

if (string[j] !== ordering[i]) {

j++
continue;
}

j++
incr++

}

for ( let k = 0; k < incr; k++) {
str+= ordering[i];
}

}

console.log(str)
console.log(unmatched)
console.log(arr)
let orderedStr = str+unmatched;

return orderedStr
}

sortString("foos", "of")

// sortString("foos", "of")       => "oofs"

// sortString("string", "gnirts") => "gnirts"

// sortString("banana", "abn")    => "aaabnn"
``````

Someone else’s code:

``````function sortString(string,ordering) {
let str = "";
let arr = string.split("");
for ( let i = 0; i < ordering.length; i++) {
console.log(ordering[i])
while (arr.includes(ordering[i])) {
str+= arr.splice(arr.indexOf(ordering[i]),1);
}
}
console.log(str)
console.log(arr)

return str + arr.join("");

}

sortString("foos", "of")
``````

You’re not removing duplicates:

It is possible that in the second string characters repeat, so you should remove repeating characters, leaving only the first occurrence.

So `sortString("foos", "of")` returns “oofs” when it should return “ofs”

1 Like

in the challenge it said it should return “oofs”. now this is confusing, i removed occurences in the second parameter at the start and it didn’t work so should i remove occurences in the final string aka “orderedStr” instead?

i removed the duplicates buddy… it ain’t working. i even tried on the second parameter at the beginning before all the processing of the rest of the code
edit: sorting my final answer makes it different from the first few simpler test cases.

🤷 then the challenge tests don’t match the description. It clearly says remove duplicates, and if the tests fail you on that then the challenge is bugged. I can get yours to return “oofs” fine when I run through it in pythontutor btw

1 Like

The challenge says to ignore (remove) duplicates in teh second string. If there are multiples of a character in the first string, they should be grouped, but if there are multiples of a character in teh SECOND string, only the first should be recognized.

… Hey, this sounds like a great chance to play with Maps! Ooooo… lol

1 Like

wait b4 u do, let me revert my code back to the correct one

there u go sir ,copy my code and see if its bugged like dan says on https://www.codewars.com/kata/a-string-of-sorts/train/javascript . because im on the fence

dan on code wars it says 6 pass. 2 fail…
expected ‘buooliioohng’ to equal ‘buoolihng’
expected ‘bssxxsslhyhdrmukqlkdy’ to equal ‘bssxxlhyhdrmukqlkdy’
im spent hours analyzing but what does this mean?

So, in both those cases, see how there are duplicated letters (bu`oo`lii`oo`hng)? First, you want to turn the second string into a list of unique values. If the ordering string they provide you looks like “buolio”, you need to ignore that second “o”.

In that second sample you provided, they sidestep the problem by slicing the characters out of `string` as they get used, thus rendering them unavailable for any repeated letters in the `ordering` variable.

i honestly dk how to. i know how to remove all occurences except 1 like this concept of code :

``````let checker;
let order = ""

let splitOrdering = ordering.split("")

for ( let r = 0; r < splitOrdering.length; r++) {

if (splitOrdering[r] !== checker)  {
order += splitOrdering[r]
checker = splitOrdering[r]
}

}
``````

but can’t find out how to remove double oo at the last if there’s 2 oo at the front. i used loops with indexOf and manipulation of indexes but still to no avail.

and honestly i don’t get " they sidestep the problem by slicing the characters out of `string` as they get used, thus rendering them unavailable for any repeated letters in the `ordering` variable." can’t really visualise it right now lol

sidestep the problem? so basically it don’t work?. slicing characters of what string? the 2nd parameter? and what do u mean by “rendering them unavailable for any repeated letters in the ordering variable” lol.

So in the “someone else’s code”, they turn `string` into an array, using `let arr = string.split("");`. From that point on, their code has diverged from yours significantly.

At each pass through their `ordering` loop (the outer loop), they get every instance of each letter that matches, and using `str += arr.splice(arr.indexOf(ordering[i]), 1)` (which removes the first matching letter from `arr`, returns the removed character and appends it to `str`), they are removing that letter from `arr`. So, if we encounter that letter again in `ordering`, it won’t match anything, as we’ve already removed all the matches.

Using the String, rather than an Array, will make this a little more complicated. Not impossible, just a little more of a challenge. I strongly suggest reading up on array methods. Specifically, on either MDN or https://devdocs.io/, I would advise reading about Array.indexOf() and Array.splice()

ahh… nevermind. I give up