3

I've been practicing simple solutions using what I've been learning / known. The question I've faced is, how to move the capital letters in the string to the front?

I've solved it, but it's not to my expectation as my original idea was to → find the uppercase letter → put them in an array → concat the uppercase with the original string array with the uppercase letter removed in it.

Hence my question is, how can I remove the capital letter in the first conditional statement so I won't need to create another conditional statement to find the lower case and store the lower case letter in an array?

For example, the input string is 'heLLo' → output would be 'LLheo' (the capital letters are now in front).

Thank you!

function capToFront(s) {
    var sp = s.split("");
    var caps = []; 
    var lower = []
    for (var i = 0; i < sp.length; i++)
        {
            if (sp[i] == sp[i].toUpperCase()){              
                caps.push(sp[i]);
           **//How can i remove the capital letter in "sp" array as I've pushed them into the caps Array**

            }
            if (sp[i] == sp[i].toLowerCase()){
                lower.push(sp[i]);
            }
        }
    return caps.join("").concat(lower.join(""));
}
Rind
  • 175
  • 3
  • 12

5 Answers5

6

With RegExp, you can accomplish your goal in one line without any loops:

const result = [...'heLLo'].sort(l => /[A-Z]/.test(l) ? -1 : 0).join('');

console.log(result); // LLheo

If you want to ensure the original order among the capital letters is preserved, it will be slightly longer:

const result = [...'Hello World Foo Bar']
  .sort((a, b) => /[A-Z]/.test(a) ? /[A-Z]/.test(b) ? 0 : -1 : 0)
  .join('');

console.log(result); // HWFBello orld oo ar
GirkovArpa
  • 3,322
  • 3
  • 6
  • 29
  • 1
    Hi, thank you! But unfortunately I've been having a tough time understanding regular expression as i can't seem to grasp the foundation thus resorting to the fundamentals of loops. But yes thank you for your answer! I'll be selecting your answer for ideal solution. But would be keen in knowing how I can use splice as well. – Rind Jul 19 '20 at 07:33
  • `/[A-Z]/.test(a)` returns `true` if `a` is a letter anywhere from `A` to `Z`. Hope that helps! – GirkovArpa Jul 19 '20 at 07:34
2

You can reach your goal with a smaller loop by using Regex.

function capToFront(sp) {
    let upperRgx = /[A-Z]/g;
    let upperLetters = sp.match(upperRgx);
    for(let i=0; i < upperLetters.length;i++) {
        let indx = sp.indexOf(upperLetters[i]);
      sp = sp.substring(0,indx)+sp.substring(indx+1,sp.length);
    }
    sp = upperLetters.join("")+sp;
    
    return sp;
}

console.log(capToFront("heLLo")) // Output: LLheo
endrcn
  • 269
  • 2
  • 9
1

Use the Splice method to remove.

function capToFront(s) {
    var sp = s.split("");
    var caps = []; 
    var lower = []
    for (var i = 0; i < sp.length; i++)
        {
            if (sp[i] == sp[i].toUpperCase()){              
                caps.push(sp[i]);
                // Use the `splice` method to remove
                sp.splice(i, 1);
            }
            if (sp[i] == sp[i].toLowerCase()){
                lower.push(sp[i]);
            }
        }
    console.log('sp', sp);
    return caps.join("").concat(lower.join(""));
}

console.log(capToFront("stAck"))
Siva K V
  • 7,622
  • 2
  • 11
  • 24
1

You can also try this approach where you check the ASCII value of characters as the capital letters lie between 65 and 90 then use .sort and .join methods on the array accordingly

function capToFront(s) {
    var sp = s.split("");
    const res = sp.sort((a,b)=> isCaps(a) ? isCaps(b) ? 0 : -1 : 0)
    return res.join("")
}

function isCaps(c){
  return c.charCodeAt()>=65 && c.charCodeAt()<=90
}

console.log(capToFront('hIsAmplEStRing'))
Dinesh Nadimpalli
  • 1,177
  • 1
  • 9
  • 20
0

const result = [...'heLLo'].sort(l => /[A-Z]/.test(l) ? -1 : 0).join('');

console.log(result); // LLheo