JSFiddle - React, Tailwind, and code Playground
by Bruno AM
HTML
<div id="corpo">
<div id="output"></div>
<div id="container">
<input type="text" id="input" value="">
</div>
<!-- jquery for enter key press -->
<script src="https://code.jquery.com/jquery-3.0.0.js" integrity="sha256-jrPLZ+8vDxt2FnE1zvZXCkCcebI/C8Dt5xyaQBjxQIo=" crossorigin="anonymous"></script>
</div>
CSS
* {
margin: 0;
padding: 0;
}
#corpo {
background-color: #f0db4f;
height: 300px;
width: 400px;
}
h1, p {
font-family: sans-serif;
text-align: center;
color: #323330;
font-size: 50px;
}
p {
font-size: 30px;
}
#output, #container {
display: flex;
justify-content: center;
margin-top: 100px;
}
input {
background-color: #eee;
border: none;
font-family: sans-serif;
color: #000;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
}
JavaScript
var questionNum = 0; // keep count of question, used for IF condition.
var question = '<h1>Qual seu nome?</h1>'; // first question
var output = document.getElementById('output'); // store id="output" in output variable
output.innerHTML = question; // ouput first question
function bot() {
var input = document.getElementById("input").value;
console.log(input);
if (questionNum == 0) {
output.innerHTML = '<h1>Olá ' + input + '</h1>';// output response
document.getElementById("input").value = ""; // clear text box
question = '<h1>Quantos você precisa?</h1>'; // load next question
setTimeout(timedQuestion, 2000); // output next question after 2sec delay
}
else if (questionNum == 1) {
output.innerHTML = '<h1>Parece que temos em estoque ' + (50 * input) + '</h1>';
document.getElementById("input").value = "";
question = '<h1>Qual a forma de pagamento?</h1>';
setTimeout(timedQuestion, 2000);
}
}
function timedQuestion() {
output.innerHTML = question;
}
//push enter key (using jquery), to run bot function.
$(document).keypress(function(e) {
if (e.which == 13) {
bot(); // run bot function when enter key pressed
questionNum++; // increase questionNum count by 1
}
});