Very new to javascript and html-type stuff. I wanted to just make a quick example using input from a user and outputting it into a table. I'm having trouble getting the window prompt to actually come up though. I imagine there is somethign very obvious that I am doing wrong but I'm not currently seeing it...I am taking a class in school but this isn't a homework assignment, just exercises I am doing on my own.
Is this something with the while loop? Any suggestions as to how I should keep prompting the user until they state otherwise?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mileage Record</title>
<style type = "text/css">
table {
width:300px;
border-collapse:collapse;
background-color:lightblue;
}
table, td, th {
border: 1px solid black;
padding: 4px;
}
th {
text-align: left;
color: white;
background-color: darkblue
}
tr.oddrow {
background-color: white;
}
</style>
<script>
var milesDriven;
var gallonsUsed;
var mpg;
var anyMore;
document.writeln("<table>");
document.writeln("<thead><tr><th>Miles Driven</th>");
document.writeln("<th>Gallons Used</th>");
document.writeln("<th>MPG</th>");
document.writeln("</tr></thead><tbody>");
while (anyMore == true) {
milesDriven = window.prompt("How many miles did you drive?");
gallonsUsed = window.prompt("How many gallons did you use?");
mpg = milesDrive/gallonsUsed;
document.writln("<tr><td>" + milesDriven + "</td><td>" +
gallonsUsed + "</td><td>" + mpg + "</td></tr>");
anymore = confirm("Do you have any more data to input?");
}
document.writeln("</tbody></table>");
</script>
There are several problems in the approach. You should not use document.writeln()
to modify document content. Instead, create elements and add them to the document tree. A good JavaScript tutorial should tell you how to do that. It will also explain that identifiers are case-sensitive, so you can't just write anyMore
here and anymore
there. You also have other typos in identifiers. And since an undefined value does not equal true
, your loop is not executed ever.
<pre>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Mileage Record</title>
<style type = "text/css">
table {
width:300px;
border-collapse:collapse;
background-color:lightblue;
}
table, td, th {
border: 1px solid black;
padding: 4px;
}
th {
text-align: left;
color: white;
background-color: darkblue
}
tr.oddrow {
background-color: white;
}
</style>
<script>
// var milesDriven;
// var gallonsUsed;
// var mpg;
var anyMore=0;
var str="<table>";
str+="<thead><tr><th>Miles Driven</th>";
str+="<th>Gallons Used</th>";
str+="<th>MPG</th>";
str+="</tr></thead><tbody>";
function milesDriven(){
return window.prompt("How many miles did you drive?");
}
function gallonsUsed(){
return window.prompt("How many gallons did you use?");
}
function mpg(){
return milesDriven()/gallonsUsed();
}
function addComponentsToDOM(){
while (anyMore<3) { //not 0 is true or not undefined is true or not null is true
str+="<tr><td>" + milesDriven() + "</td><td>" +
gallonsUsed() + "</td><td>" + mpg() + "</td></tr>"
// anymore = confirm("Do you have any more data to input?");
anyMore++;
}
str+="</tbody></table>";
//document.writeln(); i suggest you not use this method add component to DOM tree
//because this method first empty DOM tree content
document.body.innerHTML=str;
}
//why use this event,because window after loaded document.body not null
window.onload=addComponentsToDOM;
</script>
</pre>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.