Javascript数组输入并显示用户定义的数据

时间:2015-11-06 16:04:07

标签: javascript arrays multidimensional-array

我正在尝试为我的类编写一个程序,询问用户键盘字符,然后是行数和列数,然后根据行和列显示矩形形状的字符。

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);
   }

我不知道如何输出它所以如果有3行和2列并且用户选择a作为键盘输入,我需要输出类似于:

aaa
aaa

4 个答案:

答案 0 :(得分:1)

这将是2行3列,但如果它只是相同的字母,你可以只做一个双循环迭代你的行和列,并设置你想要输出的相应文本:

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

使用ecmascript6,我们可以使用单线程解决方案,利用字符串重复功能:

html = ('a'.repeat(y)+'<br/>').repeat(x)

答案 1 :(得分:0)

你应该做那样的事情:

var output = "";

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

document.write(output);

结果将是:

AAA
AAA
AAA

答案 2 :(得分:0)

的其他方式
alert(rectangleX + '\n' + rectangleY);

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

答案 3 :(得分:0)

&#13;
&#13;
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
&#13;
&#13;
&#13;

&#13;
&#13;
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!");
&#13;
&#13;
&#13;