Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Mi bucle while no muestra ninguna salida después de realizar una búsqueda desde las matrices

Puedo ejecutarlo, completar ambas matrices, luego ingresar los criterios de búsqueda, pero eso es todo. no se devuelve ningún resultado.

 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); }

He intentado usar for y while loop pero ninguno funciona, en mi cabeza el while loop tiene sentido. Así que no estoy seguro de cuál es el problema.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Tienes que actualizar algunas partes:

  • Utilice otra variable y luego i en el segundo ciclo
  • Mover el aumento del contador al final del alcance del segundo ciclo
  • Use SIZE en el segundo bucle, ya que JS distingue entre mayúsculas y minúsculas. Consulte el ejemplo actualizado ( 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 Report

0

encontré el problema gracias a @Teemu, un error tonto que cometí

tenía TAMAÑO declarado pero más tarde estaba usando tamaño.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!