-1

I'm trying to make 10PRINT in javascript but I'm getting an out-of-memory error. it's supposed to output something like this: ///////\///\///\////////\\/\\///\\///
here is the code:

let st = []
let i = 0
let i2 = 0
while(i < 10)
{
    while(i2 < 10)
    {
        if(Math.random() > 0.5)
        {
            st.push("/")
        }else
        {
            st.push("  ⃥")
        }
    }
    i2 = 0
    console.log(st)
    st = {}
}

can somebody tell me why it's not working?

jamham0
  • 21
  • 2
  • When do you expect `i2 < 10` or `i < 10` to be false? What is the purpose of `i2 = 0` inside the loop or of `st = {}`? Read the documentation: [`while`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/while), [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array), [What’s the difference between “{}” and “\[\]” while declaring a JavaScript array?](/q/33514915/4642212). The entire code can be replaced by `console.log(Array.from({ length: 10 }, () => (Math.random() < 0.5 ? "/" : "\\")).join(""));`. Why do you have _nested_ loops? – Sebastian Simon Mar 22 '22 at 10:57

1 Answers1

0

As Sebastian Simon stated, i and i2 are never modified in the loops, so they will never reach 10, thus making the program run till the end of time (or end of memory, in your case).

I suggest replacing the whiles with for loops, assuming i and i2 need to be incremented sequentially.

GigiSan
  • 1,170
  • 2
  • 19
  • 30