while 루프를 작성할 때 부울 변수를 true에서 false로 변경하여 루프를 종료하면 콘솔에 "false"가 기록됩니다. 가장 최근에 Codecademy에서이 코드를 보았습니다.
var slaying = true;
// A bit of new math magic to calculate the odds
// of hitting the dragon. We'll cover this soon!
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;
while (slaying) {
if (youHit) {
console.log("You hit the dragon and did " + damageThisRound + " damage!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You did it! You slew the dragon!");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon burninates you! You're toast.");
slaying = false;
}
}
프로그램은 잘 실행되지만 결국 "거짓"으로 기록됩니다. 어떤 일이 일어나는지 아는 사람이 있습니까?
나는 그것이 끝날 때 마지막 while 루프의 조건을 인쇄하는 CodeAcademy의 자바 스크립트 엔진의 버그 / 기능이라고 생각합니다.
다음 코드 조각은 true
기록하지 않아도 콘솔에 인쇄 됩니다.
var slaying = true;
while (slaying) {
console.log("slaying");
slaying = false;
}
while(!slaying){
console.log("not slaying..");
slaying = true;
}
그래서 제 생각에는 그것은 단순히 버그입니다.
(나는 또한 m in을 대문자로 써야한다는 말을 들었습니다 Math.floor()
. 주석에서 math라는 단어를 실제 코드로 파싱했습니다).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다