这是一个无限循环吗?为什么?

时间:2019-01-24 15:38:32

标签: javascript

我当时正在玩一些代码,做了这段似乎是无限循环的代码,但我不知道为什么。

let battery = 100;
let hackedTerminals = 0;
const welcomeMessage = 'fSociety distro is booting... Please enter your 
username: ';
const username = 'Samuel';

console.log(welcomeMessage + username);

const batteryLeft = () =>{
  battery ? console.log('I\'ve still got ' + battery + '% battery. Let\'s hack some more targets!'):
  console.log('I\'m out of battery. I was able to hack ' + hackedTerminals + 'termonals.')
}

const totalTries = 3;
let attempts = 0;
const failedAttemptMessage = 'Wrong password... Keep trying, Hackerman!';
let wipedData = false;

这里是无限循环

function tryHack(){
  attempts++
  if (attempts >= 3){
    wipedData = true;
  }else{
    wipedData = false;
  }
  wipedData ? console.log('All data has been deleted!'):
  console.log(failedAttemptMessage)
 tryHack()
}

tryHack()

1 个答案:

答案 0 :(得分:1)

您在tryHack内部无条件调用函数tryHack,这使其无限调用,因此您的代码将生成stack overflow(这是call stack发生的错误超出其范围)