Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

188
Vistas
Move string capital letters to front while maintaining the order (JavaScript)

This is my first post so I hope im doing this correctly.

I am taking a coding class and we were asked to make a piece of code that will ask for the input of a phrase, and will return in the console that phrase with the capital letters moved to the front, but still in the same order. Then print to the console this reordered phrase. (We aren't allowed to use arrays) For example: Inputting "HeLLoTherE" would return "HLLTEeoher"

However the problem is im having issues understanding how to write this code. How can I make the code select these capital letters and move them to the front? using .toUpperCase()? How can i make that select the letter and move it in front of the rest? If someone could show me an example of how this is done and explain it a little i would greatly appreciate it :)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You might just start with a the most straight forward algorithm to get something working.

let value = "HeLLoTherE";
let result = "";

for (let char of value) {
  if (char >= "A" && char <= "Z") {
    result += char;
  }
}

for (let char of value) {
  if (char >= "a" && char <= "z") {
    result += char;
  }
}

console.log(result);

You could then consolidate the 2 loops by combining the conditions.

let value = "HeLLoTherE";
let upper = "";
let lower = "";

for (let char of value) {
  if (char >= "A" && char <= "Z") {
    upper += char;
  } else if (char >= "a" && char <= "z") {
    lower += char;
  }  
}

console.log(upper + lower);

Another way of solving this would be to use regex.

var value = "HeLLoTherE";
var upper = value.replace(/[^A-Z]*/g, "");
var lower = value.replace(/[^a-z]*/g, "");

console.log(upper + lower);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Well, you are not able to use arrays, which makes it a little bit difficult, however you can still do sommething.

Although I'm using a for loop, I'm not actually using arrays. Since strings allows the [] operator, you can use an index to select each character of the string and check if it's lowercase or uppercase.

In addition, you said you need to mantain the order of uppercase letters, so you couldn't just do newStr = upper + newStr, because it would revert the original order. So, I used the string.prototype.substring() to insert the uppercase character where it should be.

const str = "HeLLoTherE";

const moveUpperToFront = (target) => {
  // Strings are immutable in js, so you cannot move one character
  // to the front without using a new string.
  let newStr = "";
  // Number of uppercase letters that appeared.
  // It's necessary because you need to mantain the original order
  let upperNumber = 0;
  // Iterate each character from beginning
  for (let i = 0; i < str.length; ++i) {
    // Is there an uppercase letter?
    if (str[i].charCodeAt() >= 65 && str[i].charCodeAt() <= 90) {
      newStr =
        newStr.substring(0, upperNumber) +
        str[i] +
        newStr.substring(upperNumber, newStr.length);

      ++upperNumber;
    }
    // No uppercase letter?
    else
      newStr += str[i];
  }
  return newStr;
};

console.log(moveUpperToFront(str));

about 4 years ago · Juan Pablo Isaza Denunciar

0

This is really not a very hard problem:

function rearrange(str) {
  let result = "";
  for (let c of str)
    if (c >= 'A' && c <= 'Z')
      result += c;
  for (let c of str)
    if (c < 'A' || c > 'Z')
      result += c;
  return result;
}

console.log(rearrange("Hello World, It Is A Beautiful Morning!"));

Find the upper-case characters, and add them to a result string. Then go back and find the other characters, and add them at the end. By looping through without any sorting, just simple iteration from start to finish, the order is preserved (other than the upper-case stuff).

The truly hard part of this would be coming up with a way to detect "upper-case" letters across all of Unicode. Some languages (well, orthographies) don't have the concept at all. JavaScript has ways that are more and less convenient to deal with that, but I suspect for the classroom material the OP has available so far, given the nature of the original question, such regex trickery would probably be inappropriate for an answer.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda