JavaScript
var myMatrix = [
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-'],
['-','-','-','-','-','-','-','-','-','-']
];
function stepOne(){
console.log('Step One');
for( var i = 0; i < myMatrix.length; i++ ){
console.log(myMatrix[i].join(' '));
}
}
//stepOne();
function stepTwo(coordinates) {
console.log('Step Two');
for(var j =0; j < coordinates.length; j ++) {
myMatrix[coordinates[j].y][coordinates[j].x] = 'x';
}
for(var i = 0; i < myMatrix.length; i++) {
console.log(myMatrix[i].join(' '));
}
}
var coordinatesArray = [
{x: 4, y: 0},
{x: 5, y: 1},
{x: 8, y: 2},
{x: 8, y: 3},
{x: 3, y: 4},
{x: 7, y: 6},
{x: 4, y: 8},
{x: 7, y: 9},
];
//stepTwo(coordinatesArray);
function stepThree(points) {
points.forEach(function(entry) {
var row = myMatrix[entry.y];
if(!row) { // if no row continue to next iteration
return;
}
var fillNum = entry.x > row.length ? row.length : entry.x; // if the number to fill is higher than length use length
myMatrix[entry.y] = new Array(fillNum + 1) // create an array which is bigger by 1 from places to fill
.join('x') // join it using x's getting the right number
.split('') // split it to create new array
.concat(row.slice(fillNum)); // add the leftovers
});
}
var pointsArray = [
{x: 5, y: 0},
{x: 6, y: 1},
{x: 8, y: 2},
{x: 8, y: 3},
{x: 3, y: 4},
{x: 6, y: 6},
{x: 4, y: 8},
{x: 7, y: 9},
];
stepThree(pointsArray);
stepOne();