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

165
Visualizações
Can someone please assits me as to why this function is not doing what i want it too

var firstDefrost = "4.0";

function zero(zeroArray) {
  if (zeroArray[1] == undefined) {
    zeroArray[1] = 0;
    zeroArray[2] = "";
    console.log("zero array 1 called")
  } else if (zeroArray[1] == ".") {
    zeroArray[1] = 0;
    zeroArray[2] = "";
    /*I want it to return 40 not 4.0 which is what is happening */
    console.log("zero array 2 called" + zeroArray)
  }

  return zeroArray;

}

firstDefrost = zero(firstDefrost);

In the second else if block of code I want the value to be returned of 40 but instead it returns 4.0 which I don't want.

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

0

From the cases in the OP, it looks like the objective is to take a number represented by a string param, and return a string representing that number times 10.

function zero(string) {
  return `${+string*10}`
}

console.log(zero("4.0"))
console.log(zero("3"))

about 4 years ago · Juan Pablo Isaza Relatório

0

This happens because when you use zeroArray[1] you can get the character in that position of the string but you cannot assign a new value like that, you need an array to do this operation. Let me edit your code with some little changes:

var firstDefrost = "4.0"; 

    function zero ( zeroArray ){
        zeroArray = zeroArray.split("");
        if(zeroArray[1] == undefined){
            zeroArray[1] = "0";
            zeroArray[2] = "";
            console.log("zero array 1 called")
            }
        else if(zeroArray[1] == "."){
            zeroArray[1] = "0";
            zeroArray[2] = "";
            /*I want it to return 40 not 4.0 which is what is happening */
            console.log("zero array 2 called" + zeroArray)
            }
        
        return zeroArray.join("");
        
    }
    
firstDefrost = zero(firstDefrost);
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