将整数添加到JavaScript变量 [英] Adding an integer to a JavaScript variable

查看:64
本文介绍了将整数添加到JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里声明了一个变量:

I have a variable declared here:

var quizScore = 0;

我想在每次 correctAnswer()时添加一个数字函数运行:

function correctAnswer(){
  quizScore+1;
    console.log ( 'correct answer selected: quizscore = ' + quizScore );
}

我添加了一个用于调试的控制台日志。它工作正常,因此正在调用该函数,但它没有添加分数。我需要将其解析为整数吗?我无法弄清楚+1需要正常工作。

I added a console log for debugging. It works, so the function is being called, but it is not adding the score. Do I need to parse this to an integer? I can't work out what the +1 needs to be to work correctly.

你可以去这里,你会看到点击问题1的Lightning Bolt在控制台中显示正确答案,但是没有添加到得分变量。

You can go here and you will see that clicking "Lightning Bolt" for question 1 shows "correct answer" in the console, but doesn't add to the score variable.

推荐答案

这样做:

function correctAnswer(){
    console.log ( 'correct answer selected: quizscore = ' + (++quizScore) );
}

您只需要将+1分配给quizScore变量。这可能是添加1并以一行显示的最快方式

You just need to assign the +1 to quizScore variable. This may be the fastest way to add 1 and display it in one line

这篇关于将整数添加到JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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