简体   繁体   中英

Is this a infinite loop? why?

I was playing arround with some code and did this code that appears to be a infinit loop but I don't know why.

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;

Here the infinite loop

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

tryHack()

您在tryHack无条件地调用函数tryHack使其无限调用自身,因此您的代码会产生堆栈溢出(这是调用堆栈超出其界限时发生的错误)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM