Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Convert string of character and number into integer?

I want to convert this string "F1" into an integer 1.

For example

"F1" ===> 1
"F2" ===> 2
"F3" ===> 3

So far, this is what I've done

parseLevel: function(level) {
    var levels = {
        "F1": 1,
        "F2": 2
    };
    return levels[level];
}

Here's the function

parseLevel: function (level) {
  return parseInt(level, 10);
}

I'm getting an error says it's NaN

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Since the string you pass has a leading F in it; parseInt can't figure out what integer F1 could be so just strip the F of the string.

parseLevel: function (level) {
  return parseInt(level.slice(1), 10);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example of how you could parse any string and only use its digits. to parse an integer.

let s = "ajs382bop";//can be any string it will strip all non digits
function stripAndReturnNumber(s){
    return parseInt(s.replace(/\D/g, ""));
    //This will replace all non digits with nothing and leave all digits remaining.
    //Then it will return the resulting integer.
}
console.log(stripAndReturnNumber(s));//382
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda