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

166
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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