sequence in Javascript

clmlbx

Technomancer
guys I want to print this in Javascript

*______*
**____**
***__***
********


Below is the code for it.. to simplify above sequence has total 8 characters in any line.. ( 8 - stars) = underscores in any line

Code:
var row = "";
var co;
for(i=1;i<=4;i++){
    
    for(j=1;j<=i;j++){
    row += "*"; 
    co++;
      }
      
      for(k =(8-(co+co));k>0;k--){
          


              row += "_";
            
          }
             for(l=1;l<=i;l++){
                row += "*";
                  }
                
                console.log(row);
}
 

nbaztec

Master KOD3R
You are heading in the right direction, albeit a few things:

  • Your indentation is messy.
  • You don't have to use separate loop counter.
  • You are just few arithmetic short of fixing it.

But the first step in generating /any/ sequence is to generalize it, so it can work for any `n` number of rows, for instance yours would look like:

Code:
function print_pattern(rows){
	for(var i=0; i<rows; i++){
		var line = '';
		for(var j=0; j<=i; j++)
			line += '*';
		for(var j=0; j<(rows-i-1)*2; j++)
			line += '_';
		for(var j=0; j<=i; j++)
			line += '*';
		console.log(line);
	}
}

Now you can simply do a:
Code:
print_pattern(3);
*____*
**__**
******

print_pattern(2);
*__*
****

print_pattern(1);
**

print_pattern(5);
*________*
**______**
***____***
****__****
**********
 

rero

Broken In
Loop approach:

( Live view link: JS Bin - Collaborative JavaScript Debugging - js sequence - Loop approach )

Code:
var i, imax = 4,
    j, k, out = new Array();

for (i = 1; i <= imax; i++) {
    var o = "",
        n = "";

    for (j = 1; j <= i; j++)
    o += "*";

    for (k = 1; k <= (imax - i); k++)
    n += "_";

    out[i - 1] = o + n + n + o;
}

console.log(out.join("\r\n"));

String padding approach:

( Live view link: JS Bin - Collaborative JavaScript Debugging - js sequence - String padding approach )

Code:
var i, imax = 4,
    j = new Array(),
    k = new Array(),
    out = new Array();

for (i = 1; i <= imax; i++) {
    var o = "",
        n = "";

    j.length = i + 1;
    o = j.join("*");

    k.length = (imax - i) + 1;
    n = k.join("_");

    out[i - 1] = o + n + n + o;
}

console.log(out.join("\r\n"));

happy coding :)
 
Last edited:

vickybat

I am the night...I am...
Here is the java implementation for Op's question:

Code:
public class pyramid {
	
	public static void DesignPattern (int row){
		int i,j;
		
		for (i = 0; i <= row ;i++){
		for (j = 0 ;j<=i; j++){
			
			System.out.print("*");
			}
		for(j=0; j <=(row-i-1)*2 ;j++){
			System.out.print("_");
		}
		for (j=0; j<=i;j++){
		System.out.print("*");
	}
	
		System.out.println();
		
		}
		
	}
	public static void main (String[] args){
		
		DesignPattern(5);
	}
}

Its generalized and can be tweaked further to add keyboard input.

But i have a question to ask to op as well as other members ( especially nbaztec).

Is javascript the correct platform for solving these problems? I can be completely wrong but imo, JavaScript should be used to add behavior in webpages and used with markup languages like html etc. Is it wise to learn javascript in a problem solving perspective or simply to add behaviour to web pages whilst building webapps.

Since i'm learning javascript as well, my approach has been completely different. Please throw some light into this guys.
 

nbaztec

Master KOD3R
Here is the java implementation for Op's question:
Is javascript the correct platform for solving these problems? I can be completely wrong but imo, JavaScript should be used to add behavior in webpages and used with markup languages like html etc. Is it wise to learn javascript in a problem solving perspective or simply to add behaviour to web pages whilst building webapps.

Since i'm learning javascript as well, my approach has been completely different. Please throw some light into this guys.

Well yes and no. Every language serves a specific purpose, in case of JavaScript, yes, it's primarily used for client side scripting to enrich HTML pages. But who's to know if someone might need some algorithm to say parse input/yield a layout that resembles that pattern. The programmer ought to know the basics well, especially of the algorithms - be it any language. There are no constraints.

Would you be using it anytime soon? I doubt so.
Would you be /never/ using it? I doubt so.

Algorithms cut across languages, and practicing problem solving in JavaScript is perfectly fine (albeit unconventional). It's right there with a stack implementation in Python and a string parser in Perl.
 

vickybat

I am the night...I am...
^^ Okay i get your point. Even i was thinking in the same lines. Its not at all bad using JavaScript for problem solving.
But to be honest mate, it felt a bit weird to me when i first saw op's question because i had never imagined JavaScript in this demeanor.

Even though its unconventional like you said, it shows the potential and power of JavaScript.
My doubts are clear. :)


@ OP & others

Guys plz post all JavaScript queries in the following thread from next time. It will be easier for members to find everything in one place. :)

*www.thinkdigit.com/forum/programming/161760-post-html5-javascript-css3-queries-here.html
 
Last edited:
Top Bottom