Javascript谷歌Chrome,代码赢了吗?

时间:2016-02-13 03:22:33

标签: javascript html google-chrome

<!DOCTYPE html>
<html lang="en">
<body>
<script Language="JavaScript">

window.alert("Bennet's Used Auto Sales: 2010 Sales");  //window to display the text


var Aqs = prompt("Enter 1st Quarter Sales for 2010 :", "");  // prompts to have user input data 
var Bqs = prompt("Enter 2nd Quarter Sales for 2010 :", ""); // prompts to have user input data 
var Cqs = prompt("Enter 3rd Quarter Sales for 2010 :", ""); // prompts to have user input data 
var Dqs = prompt("Enter 4th Quarter Sales for 2010 :", ""); // prompts to have user input data 

window.alert("The Sales Figure for Quarter One is :" + Aqs);  //prompts that repeat the inputted number
window.alert("The Sales Figure for Quarter two is :" + Bqs);
window.alert("The Sales Figure for Quarter three is :" + Cqs);
window.alert("The Sales Figure for Quarter four is :" + Dqs);

var totSales = (Aqs+Bqs+Cqs+Dqs);   //adds together all inputted data 
var avgQSales = (totSales/4);  //averages all inputted data

window.alert("The total sales for the year of 2010 for Bennet's Used Auto Sales are :" + totSales);    // window to show the variable from above
window.alert("The average Quarterly Sales of 2010 for Bennet's Used Autp Sales are :" + avgQSales);

</script>

</body>
</html>

新代码在上面,窗口显示正常,但算术的结果不正确,我无法说明原因。我的目标是让用户输入4个数字并将这些数字加在一起,然后平均,我需要显示它。

1 个答案:

答案 0 :(得分:1)

将第三行更改为

<script>

在chrome dev工具(F12)中,单击Console。刷新页面。你会看到你在哪里放错了分号(第23,24行)。修复变量赋值。 Javascript提示返回一个值,除非传递给变量或方法,否则它们就会消失。

请注意,在完成提示之前,您的页面无法完成加载。您的代码将按预期工作。但是,通过允许页面加载,触发提示,然后通过文档对象模型在某些特定位置(例如http://www.w3schools.com/js/js_htmldom_html.asp中)将结果写入页面,可以实现更好的实现。

编辑:您可能想要使用document.writeln。这会在每行的末尾放置一个换行符/ n,这样你的DOM写入就不会在一行中显示。