I am trying to practice JavaScript and I wanted to output the variable in a created element. But the value stored in the variable is not output. Only the word answer. I console logged the variable and it has a number value in it. But it does not display only the word I do not know why?
var name, computer, age, answer;
name = window.prompt("What is your name?");
alert("you chose " + name);
computer = window.prompt("How old where you when you used a computer");
alert(computer + " 0.0");
age = window.prompt("How old are you now?");
alert("you are " + age);
if (age - computer < 0) {
age = 0;
computer = 0;
}
answer = age - computer;
var con = document.getElementById('container');
var h1 = document.createElement('h1');
var h1Text = document.createTextNode('answer');
h1.appendChild('h1Text');
con.appendChild('h1');
// document.write(age + " - " + computer + " = ");
// document.write(answer + "<br>");
body {
background-color: orange;
}
div {
background-color: green;
max-width: 960px;
margin: 3em auto;
}
<div id="container"></div>
innerTextproperty of the h1 tag?var h1Text = document.createTextNode('answer'); h1.appendChild('h1Text'); con.appendChild('h1');->var h1Text = document.createTextNode(answer); h1.appendChild(h1Text); con.appendChild(h1);