B
B
Bohdan Petrov2019-08-15 16:00:12
JavaScript
Bohdan Petrov, 2019-08-15 16:00:12

Will there be a stack overflow and how to check it?

function readNumber() {
  let answer = prompt('Введите число?', 0);
  if (isFinite(answer) == true && answer != '') {
    alert('Это число: ' + answer);
  } else {
    readNumber();
  }
}
readNumber();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RAX7, 2019-08-15
@mindyourlifeguide

In setTimeout wrap then there will be no stack overflow

function readNumber() {
  let answer = prompt('Введите число?', 0);
  if (isFinite(answer) == true && answer != '') {
    alert('Это число: ' + answer);
  } else {
    setTimeout(readNumber, 0);
  }
}

or rewrite without recursion

H
hzzzzl, 2019-08-15
@hzzzzl

well, if the user is very slow-witted

function readNumber(n) {
  console.log(n)

  if(n > 10000) {
    return 'DONE!'
  } else {
    return readNumber(n + 1)
  }
}

readNumber(0)

check for 10000 times like this
UPD:
ooh, on my decrepit working computer in the browser, an overflow occurs between 25000 and 26000 calls :D
5d555a6b0bc92704144614.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question