将整数添加到JavaScript变量

编程入门 行业动态 更新时间:2024-10-26 09:29:41
本文介绍了将整数添加到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变量

本文发布于:2023-10-10 22:48:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479974.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   变量   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!