How come a loop prints out the same variable value?

I like to know the reason of the below syntax:
Why the two console.log below gives different results ???

var n = ‘’;

for (i = 0; i < 4; i ++) {
n += ‘*’;
console.log(n);
// prints
*
**
***
****
}

console.log(n); // prints ****

Many thanks

Because n is persistent it still holds the last value it had inside the loop. It is declared before the loop.

Thanks
So If reverse it, it will print one star? As the last value inside the loop will be one star?

Cheers

Assuming that is what n will be, yes. Try it out.

Did I miss something ?? I tried it [as per syntax below], but gave same result !! i,e. [One star on top and 4 starts at the bottom]

Best regards

var n = ‘’;
for (i = 4; i > 0; i --) {
n += ‘*’;
console.log(n);
}
//prints:
*
**



If you want to start with 4 *'s, and end up with 1, you’ll need to do that in your code:

let n = '****';

To remove an * you can use the String.slice() method.

for(let i = 0; i < 4; i++) {
  console.log(n);
  n = n.slice(1);
}

Start with any number of stars, and have your loop ask for one less each iteration.

>>> n = 10
>>> while n:
    print ('*' * n)
    n -= 1

    
**********
*********
********
*******
******
*****
****
***
**
*
>>> n
0
>>> 

Its great to see you gave me a new type of Loop.
But would be grateful if you could kindly reverse it using the same for loop.
So that I know what wrong with my syntax. Because when I change the star into a number, it works perfectly

Cheers
L D

It would help if I was in the right language. My apologies. The slice suggestion kind of sent me off in that direction, language unbounded.

The nice thing about slicing is it does not affect the native string or array object. It is only ever a copy of that interval. We don’t ever have to shorten the original string, in the case of *****, just decide how long we want it to be, initially. From there we simply ask for a shorter slice each time around.

1 Like
 > a = new Array(20)
<- (20) [empty × 20]
 > a.fill('*')
<- (20) ["*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*"]
 > a.join('')
<- "********************"
1 Like
 > a = a.join('')
<- "********************"
 > n = a.length
<- 20
 > for (x = n; x >= 0; x--) {
       console.log(a.slice(x))
   }
   
   *
   **
   ***
   ****
   *****
   ******
   *******
   ********
   *********
   **********
   ***********
   ************
   *************
   **************
   ***************
   ****************
   *****************
   ******************
   *******************
   ********************
<- undefined
 > for (x = n; x >= 0; x--) {
       console.log(a.slice(-x))
   }
   *********************
   ********************
   *******************
   ******************
   *****************
   ****************
   ***************
   **************
   *************
   ************
   ***********
   **********
   *********
   ********
   *******
   ******
   *****
   ****
   ***
   **
   *
<- undefined

Might have to put your thinking cap on for this one. Don’t skip over it. There is a lesson or two to be learned here.