JavaScript舍入不起作用
JavaScript Rounding not working
我正在尝试创建javascript代码,将数字四舍五入到最接近的整数、十分之一、百分之一和千分之一位,但它不起作用。你知道为什么吗?
<!DOCTYPE html>
<html>
<title>Rounding</title>
<head>
<script type="text/javascript">
var roundToInteger(x);
var roundToTenths(x);
var roundToHundredths(x);
var roundToThousandths(x);
x = window.prompt("Enter a integer");
function roundToInteger()
{
x = Math.round(x);
}
function roundToTenths()
{
x = Math.round(x * 10 + .5) / 10;
}
function roundToHundredths()
{
x = Math.round(x * 100 + .5) / 100;
}
function roundToThousandths()
{
x = Math.round(x * 1000 + .5) / 1000;
}
document.writeln ("<h1>""x" Rounded to a integer is + roundToInteger() + "<h1>");
document.writeln ("<h1>""x" Rounded to the tenths place is + roundToTenths() + "<h1>");
document.writeln ("<h1>""x" Rounded to the hundredths place is + roundToHundredths() + " <h1>");
document.writeln ("<h1>""x" Rounded to the thousandths place is + roundToThousandths() + "<h1>");
</script>
<body>
</body>
</html>
您需要阅读更多javascript。您的代码中充满了错误。正如您在页面(正文)上所写的那样,您的<script>
应该在<body>
上。此外,当你写<h1>
时,你必须在</h1>
的末尾关闭它。<title>
应位于<head>
内部。
无论如何,请阅读我修改您代码的方式:
<!DOCTYPE html>
<html>
<head>
<title>The page title is inside head</title>
<script>
// Here you can declare your functions
function roundToInteger(value)
{
var new_value = Math.round(value);
return new_value;
}
function roundToTenths(value)
{
var new_value = Math.round(value * 10 + .5) / 10;
return new_value;
}
function roundToHundredths(value)
{
var new_value = Math.round(value * 100 + .5) / 100;
return new_value;
}
function roundToThousandths(value)
{
var new_value = Math.round(value * 1000 + .5) / 1000;
return new_value;
}
</script>
</head>
<body>
<script>
// Make a pop up box
var x = window.prompt("Enter a number"); // Lets say you enter 1.623425
// Use console.log to print errors on the console. (F12>Console)
console.log('You entered', x);
document.writeln("<h1>'"x'" Rounded to a integer is " + roundToInteger(x) + "</h1>");
document.writeln("<h1>'"x'" Rounded to the tenths place is " + roundToTenths(x) + "</h1>");
document.writeln("<h1>'"x'" Rounded to the hundredths place is " + roundToHundredths(x) + "</h1>");
document.writeln("<h1>'"x'" Rounded to the thousandths place is " + roundToThousandths(x) + "</h1>");
</script>
</body>
</html>
我以前从未见过以这种方式编写函数。你确定这不是问题所在吗?
尝试以这种方式编写函数:
// Set up variables
var x, y;
// Set up function
var gimmeTwo = function(a) {
a = Math.round(a) + 2;
return a;
};
// Obtain and clean input
x = window.prompt("Enter a integer");
x = parseInt(x);
// Runthrough function
y = gimmeTwo(x);
// write out
document.write(x + " gimmeTwo is " + y);
相关文章:
- 添加/删除类淡入淡出不起作用
- 淡入淡出脚本不起作用
- 纯 JavaScript 淡入淡出 - 淡入不起作用
- 淡入淡出链接悬停不起作用
- 淡出加载然后淡入不起作用
- jQuery图像淡入淡出不起作用
- 循环浏览页面上所有选中的复选框并将id拉入数组不起作用
- 淡入淡出和AppendTo不起作用-jQuery
- jquery 和 safari 7 - slide切换和淡入淡出切换不起作用
- 淡入淡出 JavaScript 中的图像不起作用
- 在表单中签入的强制单选按钮不起作用
- 淡入和淡出功能不起作用
- 淡入/淡出功能不起作用
- 滑入下一张柔性滑块幻灯片后,css 动画不起作用
- 为什么这种淡入淡出效果不起作用
- 通过 id 获取元素不起作用,加载完整页面后在控制台中签入仍然没有线索
- 淡入淡出css动画不起作用
- QuerySelectorAll在放入外部js文件时不起作用
- JavaScript舍入不起作用
- 当我放入html5doctype声明时,jquery不起作用