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

307
Vistas
Need to calculate the number of zeros, positive and negative nos in an array using Switch Case in Javascript

I tried the below code, but the switch case isn't working as I expected it to. Can you please tell me where I'm going wrong?

    function counter() {
 //   const arr = prompt("Enter numbers").split(",");
 let arr = [1,2,3,-1,0];
 console.log(arr.length);
    const neg=0, pos=0, zero=0;
    for (var i = 0; i < arr.length; i++) {
        let val = arr[i];
        console.log(val);
        switch (val) {
            case (val === 0):
                zero += 1;
                console.log("zero");
                break;  
            case (val < 0):
                neg += 1;
                console.log("neg");
                break;
            case (val > 0):
                pos += 1;
                console.log("pos");
                break;
            default:
                console.log("not working");
                break;
        }
    }
    document.write("Negatives: " + neg+"<br>");
    document.write("Positives: " + pos+"<br>");
    document.write("Zeroes: " + zero+"<br>");
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You cannot re-assign a const. Something like this will throw an error:

const num = 0;
num += 1; // Uncaught TypeError: Assignment to constant variable.

Another problem: In your case statements you are using expressions which evaluate to either true or false (the expressions will be evaluated before matching), so you'll have to use switch(true) instead of switch(val):

function counter() {
    // const arr = prompt("Enter numbers").split(",");
    let arr = [1,2,3,-1,0];
    let neg=0, pos=0, zero=0;
    for (var i = 0; i < arr.length; i++) {
        let val = arr[i];
        switch (true) {
            case (val === 0):
                zero += 1;
                console.log("zero");
                break;  
            case (val < 0):
                neg += 1;
                console.log("neg");
                break;
            case (val > 0):
                pos += 1;
                console.log("pos");
                break;
            default:
                console.log("not working");
                break;
        }
    }
    document.write("Negatives: " + neg+"<br>");
    document.write("Positives: " + pos+"<br>");
    document.write("Zeroes: " + zero+"<br>");
}

counter()

over 4 years ago · Santiago Trujillo 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