javascript - how to achieve following pattern with strings? - Stack Overflow

how to loop through string in decreasing pattern in javascript with any given string?* string example

how to loop through string in decreasing pattern in javascript with any given string?

/* string examples

tree
tre
tr
t
tr
tre
tree


// 2nd string example
car
ca
c
car

*/

I am able to do only following pattern with for loop

t
tr
tre
tree

how to loop through string in decreasing pattern in javascript with any given string?

/* string examples

tree
tre
tr
t
tr
tre
tree


// 2nd string example
car
ca
c
car

*/

I am able to do only following pattern with for loop

t
tr
tre
tree
Share Improve this question edited Mar 8 at 20:41 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Mar 8 at 18:07 Ravi kashyapRavi kashyap 114 bronze badges 1
  • 1 please provide what you've done, better in the SO code snippet – Alexander Nenashev Commented Mar 8 at 18:14
Add a comment  | 

3 Answers 3

Reset to default 2

The issue you're describing can be solved by using two loops:

The first loop will print the string in decreasing size.

The second loop will print the string in increasing size after the smallest substring.

function printDecreasingPattern(str) {
  for (let i = str.length; i > 0; i--) {
    console.log(str.slice(0, i));
  }

  // Loop to increase the string size
  for (let i = 2; i <= str.length; i++) {
    console.log(str.slice(0, i));
  }
}

Excellent answer from 'krupali shah', let's try to use just one for.

let word = "Hello";

function printDecreasingPattern( str ) {
   let modifier = - 1;
   for( let i = str.length; i <= str.length; i += modifier ) {
      console.log( str.slice( 0, i ));
      if( i == 1 ) modifier = 1;
   }
}

printDecreasingPattern( word );

As you can see, in printDecreasingPattern, we declare the variable modifier, through which we will modify the value of i.
In the first iteration, i starts with the value "5", given by:

let i = str.length;

so

console.log( str.slice( 0, i ));

will print "Hello".

In the second, i becomes "4", because we add the value of modifier which is currently "-1".
This is repeated until i equals "1", at which point, the code in the if block is executed, assigning the value "1" to modifier, from here on, the descending count becomes ascending, until i reaches the value of the length of the string.

Iterate from minus word length (-len) to word length + 1 (len + 1), when the counter (i) reaches 0, increment it by 2 to skip 0 (empty string), and 1 (identical to -1). Slice from 0 to Math.abs(i) because i starts as -4:

function printDecreasingPattern(str) {
  const len = str.length;
  
  for (let i = -len; i < len + 1; i++) {
    if(i === 0) i += 2; // skip 0 and 1
    
    console.log(str.slice(0, Math.abs(i)));
  }
}

printDecreasingPattern('tree');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889620a4599324.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信