Tengo una página web, al cargar la página hace algunas preguntas si todas las preguntas son correctas solo luego muestra la parte del cuerpo; de lo contrario, la pregunta no permitirá la siguiente pregunta o no debería mostrar la parte del cuerpo, ayúdenme a solucionar este problema. ...
<!DOCTYPE html> <html> <head> <title>Special-Wishes </title> <script> let q1=prompt("what is your name...?"); //if the q1 answer is wrong it should not display the body content if(q1 == "John" || "JOHN" ){ let q2=prompt("what's your nick name...?"); if(q2=="blabla"){ alert("welcome to the page"); } } </script> </head> <body> <h1>My body section</h1> </body>La razón por la que todavía está haciendo la siguiente pregunta se debe a la lógica de su declaración if
if(q1 == "John" || "JOHN" ) debería ser if(q1 == "John" || q1 == "JOHN" )
Una forma aún más fácil de hacer esto sería if(q1.toUpperCase() == "JOHN")
Para no mostrar el cuerpo, desea eliminarlo o ocultarlo. Esto se puede hacer en un bloque else después de su declaración if
Eliminar: document.body.remove();
Ocultar: document.body.style.display = "none";
Use document.body.style.display = "none" cuando la condición no coincida
let q1 = prompt("what is your name...?"); //if the q1 answer is wrong it should not display the body content if (q1 == "John") { let q2 = prompt("what's your nick name...?"); if (q2 == "blabla") { alert("welcome to the page"); } else { document.body.style.display = "none" } } else { document.body.style.display = "none" } <body> <h1>My body section</h1> </body>