1

I am a trying to write a program for my class that asks the users for a keyboard character and then for number of rows and columns, then displays the character in a rectangle shape based on the rows and columns.

var keyBoardInput = prompt("Pick a key on your keyboard to display");
var x = prompt("How many rows?");
var y = prompt("How many columns?");

rectangleX = new Array(x);
rectangleY = new Array(y);

for(var i =0; i < rectangleX.lenght; i++)
    {
    rectangleX.push(keyBoardInput);
    }

for(var j =0; j < rectangleY.lenght; j++)
   {
   rectangleY.push(keyBoardInput);
   }

I'm not sure how to get it to output so say if there was 3 rows and 2 columns and the user chose a as the keyboard input, I would need an output of something like:

aaa
aaa
2
  • how are you planning to output it? Commented Nov 6, 2015 at 16:07
  • document.write() has been the norm but im not sure how to get to display in the rectangle way like the AAAs in the post Commented Nov 6, 2015 at 16:10

4 Answers 4

1

That would be 2 rows and 3 columns, but if it´s just always the same letter, you can just make a double loop iterating your rows and columns, and set the corresponding text that you want to output:

var x = 2
var y = 3


html = '';

for(var i =0; i < x; i++){ // for each row
    for(var j =0; j < y; j++){ // we add a 'a' for each column in the row
            html+= 'a'
    } 
    // we already made a row, now we need a new line for the next row to show in the next line
	html+='<br/>'
}


var newParagraph = document.createElement('p'); // we create a paragraph element
    newParagraph.innerHTML = html; // set the html we made
document.body.appendChild(newParagraph); // and add it to the dom

And with ecmascript6 we could use a one liner solution taking advantage of the string repeat function:

html = ('a'.repeat(y)+'<br/>').repeat(x)
Sign up to request clarification or add additional context in comments.

3 Comments

The letter to be displayed as well as the rows(x) and columns(y) are all user defined.
@JoshuaWalters yeah, replace the 'a' with your keyBoardInput variable and prompt for x and y instead of fixed values, was just showing an example
ok thank you i for got a bracket and it wasnt running so i thought it was the variable change
0

You should do something like that:

var output = "";

for(var i = 0; i <3; i++){
  for(var j = 0; j <3; j++){
    output +="A";
  }
  output+="<br>";
}

document.write(output);

The result will be:

AAA
AAA
AAA

Comments

0

or other way with

alert(rectangleX + '\n' + rectangleY);

or

console.log(rectangleX + '\n' + rectangleY);

1 Comment

This won´t actually work with x = 10 and y = 10, for example
0

var x = 2
var y = 3


html = '';

for(var i =0; i < x; i++){ // for each row
    for(var j =0; j < y; j++){ // we add a 'a' for each column in the row
            html+= 'a'
    } 
    // we already made a row, now we need a new line for the next row to show in the next line
	html+='<br/>'
}


var newParagraph = document.createElement('p'); // we create a paragraph element
    newParagraph.innerHTML = html; // set the html we made
document.body.appendChild(newParagraph); // and add it to the dom

var BR = "<br />";  // Line break
var ES = "";        // Empty space
var PA = "<p />";   // full paragraph break

// Program heading

// Get inputs
var keyBoardInput = prompt("Pick a key on your keyboard to display");
var x = prompt("How many rows?");
var y = prompt("How many columns?");
var html = "";

rectangleX = new Array(x);
rectangleY = new Array(y);

for(var i =0; i < x; i++)
	{ // for each row
    for(var j =0; j < y; j++)
	{ // we add a 'a' for each column in the row
            html+= keyBoardInput
    } 
    // we already made a row, now we need a new line for the next row to show in the next line
	html+='<br/>'
}
	


// do the maths, and display the results
var newParagraph = document.createElement('p'); // we create a paragraph element
    newParagraph.innerHTML = html; // set the html we made
document.body.appendChild(newParagraph); // and add it to the dom
// thank user and end the program
document.write(PA + "Thank you for using this program. Good-bye!");

1 Comment

Here is the final working program thank you for all who helped with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.