为什么 while 循环会忽略它的 != 规则
why does that while loop ignore it's != rule
所以基本上这是我在学校遇到的关于怪物和你的能量的问题,从第一个索引开始,之后的每个是与怪物战斗需要多少能量才能杀死它。如果您赢得了 3 场战斗,您的能量点将获得战斗计数。所以我注意到 End of Battle 字符串通过了 while 循环,即使我将它置于 != :/
的条件中
对不起,我有点菜鸟..
function dude(input) {
// inital energy as input(shift)
let energy = Number(input.shift());
// we create a count and ++ it for every itteration
let count = 0;
// while input[0] is NOT "End Of Battle" and energy > 0
while (input[0] !== 'End Of Battle' && energy > 0) {
// input.shift() is how much energy we will need for each monster
let monster = Number(input.shift());
// we substract that input[0].shift() energy from the starting energy
energy -= monster
count++;
if (count % 3 === 0) {
energy += count;
}
console.log(energy);
}
if(energy <= 0){
console.log(`Not enough energy! Game ends with ${count} won battles and ${energy} energy`);
}else{
console.log(`Won battles: ${count}. Energy left: ${Number(energy)}`);
}
}
dude((["200",
"54",
"14",
"28",
"13",
"End of battle"]));
您正在发送字符串 'End of battle' 并将其与 'End Of Battle' 进行比较。他们不一样。为了保护自己免受此类差异的影响,您可以在比较之前规范化字符串(trim 去掉任何多余的白色 space 并转换为小写)。我调整了这一行,现在可以使用了:
if (input[0].trim().toLowerCase() !== 'end of battle' && ...)
function dude(input) {
// inital energy as input(shift)
let energy = Number(input.shift());
// we create a count and ++ it for every itteration
let count = 0;
// while input[0] is NOT "End Of Battle" and energy > 0
while (input[0].trim().toLowerCase() !== 'end of battle' && energy > 0) {
// input.shift() is how much energy we will need for each monster
let monster = Number(input.shift());
// we substract that input[0].shift() energy from the starting energy
energy -= monster
count++;
if (count % 3 === 0) {
energy += count;
}
console.log('energy', energy);
}
if (energy <= 0) {
console.log(`Not enough energy! Game ends with ${count} won battles and ${energy} energy`);
} else {
console.log(`Won battles: ${count}. Energy left: ${Number(energy)}`);
}
}
dude(["200",
"54",
"14",
"28",
"13",
"End of battle"
]);
所以基本上这是我在学校遇到的关于怪物和你的能量的问题,从第一个索引开始,之后的每个是与怪物战斗需要多少能量才能杀死它。如果您赢得了 3 场战斗,您的能量点将获得战斗计数。所以我注意到 End of Battle 字符串通过了 while 循环,即使我将它置于 != :/
的条件中对不起,我有点菜鸟..
function dude(input) {
// inital energy as input(shift)
let energy = Number(input.shift());
// we create a count and ++ it for every itteration
let count = 0;
// while input[0] is NOT "End Of Battle" and energy > 0
while (input[0] !== 'End Of Battle' && energy > 0) {
// input.shift() is how much energy we will need for each monster
let monster = Number(input.shift());
// we substract that input[0].shift() energy from the starting energy
energy -= monster
count++;
if (count % 3 === 0) {
energy += count;
}
console.log(energy);
}
if(energy <= 0){
console.log(`Not enough energy! Game ends with ${count} won battles and ${energy} energy`);
}else{
console.log(`Won battles: ${count}. Energy left: ${Number(energy)}`);
}
}
dude((["200",
"54",
"14",
"28",
"13",
"End of battle"]));
您正在发送字符串 'End of battle' 并将其与 'End Of Battle' 进行比较。他们不一样。为了保护自己免受此类差异的影响,您可以在比较之前规范化字符串(trim 去掉任何多余的白色 space 并转换为小写)。我调整了这一行,现在可以使用了:
if (input[0].trim().toLowerCase() !== 'end of battle' && ...)
function dude(input) {
// inital energy as input(shift)
let energy = Number(input.shift());
// we create a count and ++ it for every itteration
let count = 0;
// while input[0] is NOT "End Of Battle" and energy > 0
while (input[0].trim().toLowerCase() !== 'end of battle' && energy > 0) {
// input.shift() is how much energy we will need for each monster
let monster = Number(input.shift());
// we substract that input[0].shift() energy from the starting energy
energy -= monster
count++;
if (count % 3 === 0) {
energy += count;
}
console.log('energy', energy);
}
if (energy <= 0) {
console.log(`Not enough energy! Game ends with ${count} won battles and ${energy} energy`);
} else {
console.log(`Won battles: ${count}. Energy left: ${Number(energy)}`);
}
}
dude(["200",
"54",
"14",
"28",
"13",
"End of battle"
]);