Reputation: 29
I just want a way to return my tile map (array of an array)in the textarea, I mean at position [0][1] for example it paints a # for 1 values and empty fill for 0 ones.
<script language="javascript">
var wall = "#";
var empty = "";
var mapa = [
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]
];
function map() {
for (i = 0; i < mapa.length; i++) {
for (j = 0; j < mapa[i].length; j++) {
mapa[i][j];
}
}
}
function display(form) {
var myform = form;
myform.caja.value = map();
}
</script>
<body>
<form name=form>
<textarea name=caja cols=5 rows=5></textarea><br>
<input type=button value="click!" onclick="display(this.form)">
</form>
Upvotes: 0
Views: 49
Reputation: 664971
I'd go with
function print2dim(arr, charmap) {
return arr.map(function(row) {
return row.map(function(el) { return charmap[el]; }).join('');
}).join("\n");
}
function display(form) {
form.caja.value = print2dim(mapa, [" ", "#"]); // or {"0":" ","1":"#"}
}
Upvotes: 0
Reputation: 40517
try this:
function map(){
var ret='';
for(i=0;i<mapa.length;i++){
for(j=0;j<mapa[i].length;j++){
if(mapa[i][j]===0){
ret +='#';
}else{
ret +=' ';
}
}
ret +='\n';
}
return ret;
}
Upvotes: 1