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.
Tienes que actualizar algunas partes:
i en el segundo ciclo 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++; }encontré el problema gracias a @Teemu, un error tonto que cometí
tenía TAMAÑO declarado pero más tarde estaba usando tamaño.