1
#######
######
#####  
####  
###  
##  
#


The above question is the problem from the book eloquent javascript. But the question was different as:

Write a loop that makes seven calls to console.log to output the following triangle:

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

But I need a solution for the exact reverse of this by using the logic below:

for (let line = "#"; line.length < 8; line += "#")
  console.log(line);

The below is the code snippet which I tried as above logic but doesn't work as above:

for (let line = "#######"; line.length > 0; line -= "#")
  console.log(line);  

Anyone can give me the solution by using the same logic and where I went wrong?

  • `line = line.slice(0, -1)` or `line = line.slice(1)` There's no subtraction operator for strings, so you can use string manipulation methods instead. –  May 23 '20 at 03:21

3 Answers3

1

Are you looking for this:

for (let line = "#######"; line.length > 0; line = line.slice(1))
  console.log(line);
mc.
  • 539
  • 6
  • 15
0
let test_two = "#";
while (test_two.length <8){
   console.log(test_two);
   test_two +="#";
}
hopeson
  • 1
  • 1
0

for (let line = '#######'; line.length!=0; line = line.slice(0,line.length -1))
console.log(line);

try this