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

100
Vistas
¿Cómo dejar que el aviso lea la entrada de texto e imprimir mensajes de texto personalizados?

Estoy tratando de hacer un juego de aventuras de texto usando JS básico. Básicamente, un usuario tendrá 2 opciones, y el cuadro de aviso debe imprimir los mensajes correspondientes según las elecciones que haga el usuario.

Supongo que lo que falta es un detector de eventos, pero no estoy seguro de cómo implementarlo.

 let message = prompt("Hi this is an adventure. Select your input as A or B. In front of you there is a sign. Pick A. Forest B. Lake") if (A) { prompt("you see the bushes ahead of you rustling.You-- A.proceed ahead B.turn back and run") }; else if (B) { prompt("you see the water bubbling. You--A. walk up B.--flee") }

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

0

Debe comparar if (message.toUpperCase() === "A") { para permitir que el usuario escriba a o A

También tendrá mucho menos código si usa un objeto y un formulario

 const advent = { "Start": { "text": "Hi this is an adventure. Click the buttons to make your choice. In front of you there is a sign. You go direction", "a": "Forest", "b": "Lake" }, "Forest": { "text": "you see the bushes ahead of you rustling. You --", "a": "proceed ahead", "b": "turn back and run" }, "Lake": { "text": "you see the water bubbling. You--", "a": "walk up", "b": "flee" }, "walk up": { "text": "You drown; The end" }, "flee": { "text": "You fall in a hole and die; The end" } }; let currentPath = advent["Start"] window.addEventListener("DOMContentLoaded", function() { const text = document.getElementById("text"); const A = document.getElementById("a"); const B = document.getElementById("b"); const show = () => { text.innerHTML = currentPath.text; console.log(currentPath.a) if(currentPath.a) A.value = currentPath.a if(currentPath.b) B.value = currentPath.b }; const form = document.getElementById("myForm").addEventListener("click", function(e) { const tgt = e.target; currentPath = advent[currentPath[tgt.id]] document.getElementById("choices").hidden = (!currentPath.a && !currentPath.b); // hide if no choices show() }); show(); // start });
 <form id="myForm"> <div id="text"></div> <div id="choices"><input type="button" id="a" value="A"> <input type="button" id="b" value="B"></div> </form>

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