-1

I am trying to solve a problem in hacker rank the given array is arr=["dd","dda","ddb"]

According to problem I had to make a string in alphabetic order the correct o/p for this according to hacker rank is "ddaddbdd"

For some test case the sorting and joining work but most of test case does not pass> Any recommendation to approach this problem?

I tried to sort the string and then join them like below but did not get the the output.

arr=arr.sort((a,b)=>a.localeCompare(b));

str=arr.join("")
Code Maniac
  • 37,143
  • 5
  • 39
  • 60

2 Answers2

1

You need to sort individual element inside array first and then join

let arr=["dd","dda","ddb"]

let final = arr.map(str=> str.split('').sort().join('')).join('')

console.log(final)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

one 'old is gold' approach for you:

  1. get every string item from array

  2. convert it in char array

  3. sort the char array

  4. put it back in string array

function getInSort(a){

 for(var i=0; i<a.length; i++){
    let temp = a[i].split('');
    temp.sort();
    a[i] = temp.join('');
 }
 return a.join('');
}
console.log(getInSort(["dd","dda","ddb"]));
Papai from BEKOAIL
  • 1,469
  • 1
  • 11
  • 17