Repeat a string help plz

Tell us what’s happening:
Hello ! someone can explain me clearly why we can put this =>
let newArr = “”;
while (num > 0) {
newArr += str;
num- -;
} return newArr;

and can’t put this ? =>
let newArr = “”;
while (num < 0) {
newArr += str;
num++;
}
return newArr;
I understand why the first solution works but I don’t know why the second one did not work.

   **Your code so far**

function repeatStringNumTimes(str, num) {
 
 /*let newArr = "";
 while (num < 0) {
   newArr += str;
   num++;
 }
 return newArr; */
 
 
 let arr = [];
while (arr.length < num) {
  arr.push(str);
}
return arr.join('');

}

repeatStringNumTimes("abc", 3);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Repeat a String Repeat a String

Link to the challenge:

what’s the starting value of num? a loop execute as long as the condition is true, so it depends what’s the starting value of num. Is num a negative number? than it could work. If num is a positive number, than num < 0 is false from the beginning and the loop never work

1 Like

Thank you @ILM , I get it. num value was 3. A positif num, so that’s why the second solution did not work. If num value was -3, so the second solution would work and the first one would not ? Thanks again ! I think I have to go back over the loop lesson to get it all.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.