-1

Whenever I run this snippet:

let longString = 0;
let fact = 6;
const lettersLeft = ["abcde"]
    const select = lettersLeft;
    let j = 1
    while (j <= fact, j=j+1) {
      longString += select;
    }
    console.log(longString);

it crashes my app. I'm thinking it is trying to run indefinately, or it cannot read something. How do I fix this because this is very important to my code.

  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Oct 25 '22 at 19:44
  • 1
    `j <= fact, j=j+1` is just [the comma operator](https://stackoverflow.com/questions/3561043/what-does-the-comma-operator-do-in-javascript) and it returns the second thing. Which is always truthy. Do yourself a favour and don't try to hack in weird syntax. Just use a regular for loop which is clearly what you need, yet you tried to re-create it as a while for some reason. – VLAZ Oct 25 '22 at 19:45
  • 1
    increment `j` inside your loop – GrafiCode Oct 25 '22 at 19:47
  • 2
    Why is `longString` a number? Why are you adding an array to it? What is this code supposed to do? If you just want a repeated string, use [`"abcde".repeat(6)`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). – Sebastian Simon Oct 25 '22 at 19:50

1 Answers1

1

With a little change to your while expression you can make it work:

let longString = 0;
let fact = 6;
const lettersLeft = ["abcde"],
  select = lettersLeft;
let j = 1;
while (j++ <= fact)
  longString += select;

console.log(longString);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43