admin管理员组文章数量:1327973
I don't understand why the below JavaScript program returns the answer Infinity. What am I doing wrong? I'm a novice, and would appreciate a detailed explanation!
Please note that this needs to be acplished using only math and math functions, no string functions or arrays!
<script type = "text/javascript">
var input;
var rev = 0;
input=window.prompt ("Please enter a 5-digit number to be reversed.");
input = input * 1;
while (input > 0)
{
rev *= 10;
rev += input % 10;
input /= 10;
}
document.write ("Reversed number: " + rev);
</script>
I don't understand why the below JavaScript program returns the answer Infinity. What am I doing wrong? I'm a novice, and would appreciate a detailed explanation!
Please note that this needs to be acplished using only math and math functions, no string functions or arrays!
<script type = "text/javascript">
var input;
var rev = 0;
input=window.prompt ("Please enter a 5-digit number to be reversed.");
input = input * 1;
while (input > 0)
{
rev *= 10;
rev += input % 10;
input /= 10;
}
document.write ("Reversed number: " + rev);
</script>
Share
Improve this question
asked Aug 8, 2011 at 23:50
sls3nycsls3nyc
5
- 2 You seem to have an answer already, so there's probably no point posting elsewhere, but for future reference: this site is for puzzles in the sense of games. For puzzles in the sense of bugs you don't understand, the main StackOverflow site is the appropriate one. – Peter Taylor Commented Aug 9, 2011 at 6:38
- It can (and should) still be migrated to its proper place. – Joey Commented Aug 9, 2011 at 8:41
- @Joey, except that the proper place isn't an option in the migration targets. The only option is meta.codegolf, for some reason. – Peter Taylor Commented Aug 9, 2011 at 8:44
- 1 Leave that to the mods. They can do things we can never ask for. – Joey Commented Aug 9, 2011 at 8:45
- I've started a thread on meta – Peter Taylor Commented Aug 9, 2011 at 8:46
2 Answers
Reset to default 4Your line: input /= 10;
doesn't result in an integer.
You end up with a sequence like this:
input rev
1234.5 5
123.45 54.5
12.345 548.45
This never hits 0, so your while condition just keeps going till it reaches 1e-323, then the number runs out of precision and goes to 0.
If you replace the input /= 10;
line with input = Math.floor(input/10);
then it works.
Because this is code golf you probably don't want to use Math.floor though. There is a smaller one, I'll see if I can find it again.
You can use input = ~~(input/10);
provided input
is always positive.
<html>
<head>
<script type="text/javascript">
function reversing(x){
y=x%10;
x=parseInt(x/10);
document.write(y);
if(x!=0){
reversing(x);
}
}
</script>
</head>
<body>
<input id="txt_field" type="text" name="field" />
<input type="button" name="submit" value="Submit" onclick="reversing(document.getElementById('txt_field').value);"/>
</body>
</html>
本文标签: algorithmJavascript program to print a number series in reverseStack Overflow
版权声明:本文标题:algorithm - Javascript program to print a number series in reverse - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742252369a2440999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论