JavaScript How to create a specific X*X size table? (Battleship game) (HTML, CSS, JS) create specific size file linux,create specific size image,create

I have an assignment to create a battleship game, with 3x3 - 10x10 grid. My current idea would be to create a grid with a click of a button, or atlest change the current size with a click of a button. However Im a beginner and its really difficult to find any simple or understandable advice. So I thought I'd ask here explaining my personal situation.

(I pasted the following code pieces into pastebin since some indentation and formating seems to dissapear here. http://pastebin.com/ZL3cWt8U)

I'm trying to recreate this with a javascript function.

css:

.cell {border-right: 1px solid #000000; border-bottom: 1px solid #000000; float: left; width: 30px; height: 30px;}
.row {clear: both; overflow: hidden;}
.table {border-left: 1px solid #000000; border-top: 1px solid #000000;}

HTML:

<div class="table">
<div class="row">
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
</div>
<div class="row">
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
</div>
<div class="row">
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
</div>
<div class="row">
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
</div>
<div class="row">
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
    <div class="cell">&nbsp;</div>
</div>

Here is the function im trying to work with. Im using a dropdown menu wich has 3x3 - 10x10 values in it. Pressing a button executes the function.

function genTable(gridsize) {

var e = '<div class="table"><div class="row"><div class="cell">&nbsp;</div></div></div>'

var value = $("#option").val(); //gets value from button press (between 3-10)
gridsize = value; // this isn't really neccesary, oops
for (i = 0; i <= gridsize-1; i++) { //for loop to create the table
    $("#tabel_here").append(e);

}
}   

Now, this far it just creates a vertical X x 1 table, just a column. As far as I know, the solution would be to loop this:

<div class="cell">&nbsp;</div>

As much as needed(*gridsize) inbetween the row div. This is where I'm stuck because I cant seem make it work whitout breaking the code.

Also, if there are simpler ways to make this happen let me know, this might be a dumb way to solve the problem, but I just started learning html, css and js.

Now I would like to stress, that this will be used to make a battleship game, so it needs to have as much functionality as possible, this is why I considered using divs as they seem to be the most universal and flexible.

Answer:1

Try this:

 var createGrid=function(x,y){
    	var arrY = new Array(),
    		arrX,
            container = $(".table");
    	for(var iy=0; iy<y; iy++){
    		arrX = new Array();
    		for(var ix=0; ix<x; ix++){
    			arrX[ix]='<div class="cell">&nbsp;</div>';
    		}
    		arrY[iy]='<div class="row">'+arrX.join("\r\n")+'</div>';
    	}
    	container.append(arrY.join("\r\n"));
    };
// call function
    createGrid(10,10);
.cell {display:table-cell; 
    border-right: 1px solid #000000; 
    border-bottom: 1px solid #000000; 
    width: 30px; height: 30px;
}
.row {
    display:table-row; 
    clear: both; 
    overflow: hidden;
}
.table {
    display:table; 
    border-left: 1px solid #000000;
    border-top: 1px solid #000000;
    display:table;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="table"></div>
Answer:2

Here is a Demo

function drawGrid(cells)
{
    $(".grid").empty();
    for(var i=0 ; i<cells ; i++)
    {
        var row = $("<div class='rowa'></div>");
        for(var j=0 ; j<cells ; j++)
        {
            row.append("<div class='cella'>&nbsp;</div>");
        }
        $(".grid").append(row);
    }
}
Answer:3

You can create functions to repeat the column creation and row creation and update your HTML. something like below.

var TableUtil = {
    repeatString: function(str, times) { 
       return (new Array(times + 1)).join(str);
    },
	createTableString: function(rows, cols){
    var tableString='<div class="table">',
        rowString ='<div class="row">',
        colString ='<div class="cell">&nbsp</div>';
        
    //create single row
    rowString += this.repeatString(colString, cols);
    rowString +='<div>';
        
    
    //repeat n times the rows
    tableString += this.repeatString(rowString, rows);
    
    return tableString;
	}
};
var el = document.getElementById('TableBox');
el.innerHTML=TableUtil.createTableString(10,5);
.cell {border-right: 1px solid #000000; border-bottom: 1px solid #000000; float: left; width: 30px; height: 30px;}
.row {clear: both; overflow: hidden;}
.table {border-left: 1px solid #000000; border-top: 1px solid #000000;}
<div id="TableBox"></div>
Answer:4

I am creating a dynamic refresh feature for div elements on my site that need to be dynamically refreshed. $(document).ready(function() { function refreshDiv() { var refreshUrl =...

I am creating a dynamic refresh feature for div elements on my site that need to be dynamically refreshed. $(document).ready(function() { function refreshDiv() { var refreshUrl =...

  1. jquery bind elements

I'm looking to purge Cloudflare's cache through its API. More specially, the purge all files command. However, I keep running into the "Invalid Content-Type header, valid values are application/json,...

I'm looking to purge Cloudflare's cache through its API. More specially, the purge all files command. However, I keep running into the "Invalid Content-Type header, valid values are application/json,...

  1. purge cloudflare cache
  2. cloudflare purge cache api
  3. cloudflare purge cache not working
  4. cloudflare purge cache api php
  5. cloudflare purge cache wildcard
  6. cloudflare purge cache by url
  7. failed to purge cloudflare cache not authenticated
  8. how to purge cloudflare cache

We are building a form-based app that has a complex object with many levels of nested properties. So far, I have created a simple experiment with a single view model with one object. The experiment ...

We are building a form-based app that has a complex object with many levels of nested properties. So far, I have created a simple experiment with a single view model with one object. The experiment ...

  1. bind view model in mvc
  2. bind view model to view
  3. bind viewmodel extjs
  4. wpf bind to viewmodel
  5. bind view viewmodel xaml
  6. bind view to model backbone
  7. aurelia bind view-model
  8. bind partial view model
  9. bind property view model
  10. wpf bind viewmodel property
  11. bind view to viewmodel
  12. bind view to viewmodel android
  13. bind view to viewmodel wpf
  14. prism bind view viewmodel
  15. bind button to viewmodel
  16. android bind view to model
  17. bind button click to view model
  18. angular bind view to model
  19. razor bind view to model
  20. mvvm bind view to model

I have a menu have many items. I want to show only 5 items, other items will be hidden. When clicking next or previous button, it will show other items. Should be shown like this: Here is my demo ...

I have a menu have many items. I want to show only 5 items, other items will be hidden. When clicking next or previous button, it will show other items. Should be shown like this: Here is my demo ...