Mobile app version of vmapp.org
Login or Join
Kimberly868

: Javascript. Tree of hash char I'm newbie in javascript. I'm trying to do a tree of hash ("#") in javascript. The output I would to have is something like: # ## ### #### ##### ###### ####### I

@Kimberly868

Posted in: #Javascript

I'm newbie in javascript. I'm trying to do a tree of hash ("#") in javascript. The output I would to have is something like:

#
##
###
####
#####
######
#######


I know how increment the line with "for" cycle;
This is my code:

var s = "#######";
for (i = 0; i <s.length; i++)
console.log(s);


But I don't understand how can decrement the char "#" by string.

Can someone please explain this to me.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Kimberly868

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

Modifying your existing code, try the following:

var s = "#######";
for (i = 0; i <s.length; i++) {
console.log(s.substring(0,i));
}


The substring() method of the string object (ie. your var s) returns a portion of the string starting at 0 (the first char) and ending at i, which increases by 1 for each iteration of your for() loop.


But I don't understand how can decrement the char


Should be increment.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme