javascript - 如何在 Javascript 中将数字添加到变量中?
问题描述
我需要通过这个程序猜测 4 位代码,但我不知道为什么它不起作用。
我尝试使用两个 for 循环来猜测从 0 到 9 的所有数字的 4 位数字。
但是,该程序不打印任何内容。
我想知道如何将当前值添加x
到变量result
中。
function start() {
var secretPasscode = generateRandomPasscode();
// Write your code here
var result = "";
//Loop through for the secretPasscode's length
for(var i = 0; i < secretPasscode.length; i++){
//Put numbers from 0 to 9
for(var x = 0; x < 10; x++){
result = "";
var currNum = x;
//If the current number is right, store it to the result
if(currNum == secretPasscode.charAt(i)){
result += x;
}else{
result = result;
}
}
}
return result;
println(result);
}
function isCorrect(guessCode, correctCode) {
return guessCode == correctCode;
}
// Generates a random 4 digit passcode and returns it as a String
function generateRandomPasscode() {
var randomPasscode = "";
for(var i = 0; i < 4; i++) {
var randomDigit = Randomizer.nextInt(0, 9);
randomPasscode += randomDigit;
}
return randomPasscode;
}
解决方案
推荐阅读
- javascript - redux mapDispatchToProps 不更新状态
- mysql - Mysql:获取特定用户的最新值
- vhdl - Verilog中的浮点平方根
- javascript - 如何在屏幕上显示这个数组?
- javascript - LowDB 获得价值
- r - R:获取一个值是否在两个日期之间并计数
- javascript - JS for循环获取每个数组中的日期差异
- python - NoReverseMatch at /checkout/ - Django 网站
- python - 如何循环 pyspark 数据框列值以进行条件计算
- r - 如何使用 plotly 在散点图中显示第三个变量的第二个图例?