Javascript计数器变量不仅增加附加数字 [英] Javascript counter variable not incrementing only appending digits

查看:105
本文介绍了Javascript计数器变量不仅增加附加数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var counter = 0;

  var userAdd = prompt("How many would you like to add?");

  counter += userAdd;

  console.log(counter);

javascript的新功能。

New to javascript.

我试图让计数器增加userAdd变量指定的数量。

I am trying to get the counter to increment up by the amount that the userAdd variable specifies.



运行此代码时,我从console.log()获得的所有内容例如:


When I run this code all I get from the console.log() is, for example:

如果userAdd为每次为'1':

If userAdd is '1' each time:

01

011

0111

...等

01
011
0111
...etc.

而不是:

1

2

3

1
2
3

这里出什么问题了?

推荐答案

您需要输入结果为int而不是字符串。

You need to type your result as an int rather than a string.

var userAdd = parseInt(prompt("How many would you like to add?"));

这将强制数字加法而不是字符串附加。

This forces the numeric adding instead of the string appending.

这篇关于Javascript计数器变量不仅增加附加数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆