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

222
Vistas
My while loop is not displaying any outputs after making a search from the arrays

I'm able to run it, fill out both Arrays, then input the search criteria but that's it. no results are thrown back.

        var SIZE = 4;
        var actors = new Array(SIZE); //array holding actors
        var roles = new Array(SIZE); //array holding roles
        
        
        for (var i = 0; i < SIZE; i++)  {
        
            actors[i] = prompt ("Please enter your "  + (i + 1) + " favorite Actor");
            roles[i] = prompt ("What role did your " + (i + 1)+ " Actor played?");
        }
                    
        var favorite; // hold search actor name
        var found = false;
        var BR = "<br>";    
        
        favorite = prompt ("Please enter the name of your most favorite actor of all");
        
        while (i < size && !(found))    {
            i++;
        
            if (actors[i] == favorite)  {
                found = true;
                document.write("Your favorite actor " + actors[i] + " played the role of " + roles[i] + BR);
                
        /*for(var i = 0; i < size; i++) {

            if ( favorite == actors[i]) {
                found = true;
                document.write("Your favorite actor " + actors[i] + " played the role of " + roles[i] + BR);
        
            }
        
        }
        */
        if (!found) { //if we find nothing 
            document.write("Sorry, we couldn't find an actor with that name" + BR);
        }

I have tried using for and also while loop but none work, in my head the while loop makes sense. So im not sure whats the issue.

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

0

You have to update some parts:

  • Please use another variable then i in the second loop
  • Move increase of counter to the end of scope of the second loop
  • Use SIZE in the second loop, as JS is case-sensitive. Please check the updated example (https://codepen.io/alekskorovin/pen/xxXxYGB):

var SIZE = 4;
var actors = new Array(SIZE); //array holding actors
var roles = new Array(SIZE); //array holding roles

for (var i = 0; i < SIZE; i++) {
  actors[i] = prompt("Please enter your " + (i + 1) + " favorite Actor");
  roles[i] = prompt("What role did your " + (i + 1) + " Actor played?");
}

var favorite; // hold search actor name
var found = false;
var BR = "<br>";

favorite = prompt("Please enter the name of your most favorite actor of all");
let k = 0
while (k < SIZE && !found) {
  if (actors[k] == favorite) {
    found = true;
    console.log(
      "Your favorite actor " +
        actors[k] +
        " played the role of " +
        roles[k] +
        BR
    );

    if (!found) {
      //if we find nothing
      console.log("Sorry, we couldn't find an actor with that name" + BR);
    }
  }
  k++;
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

found the problem thanks to @Teemu, a silly mistake I made

had SIZE declared but later down I was using size.

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