Query Board
by LyndseyB
HTML
SetCol 32 20
SetRow 15 7
SetRow 16 31
QueryCol 32
SetCol 2 14
QueryRow 10
SetCol 14 0
QueryRow 15
SetRow 10 1
QueryCol 2
5118
34
1792
3571
JavaScript
var lines = [
"SetCol 32 20",
"SetRow 15 7",
"SetRow 16 31",
"QueryCol 32",
"SetCol 2 14",
"QueryRow 10",
"SetCol 14 0",
"QueryRow 15",
"SetRow 10 1",
"QueryCol 2"
];
var rows = [],
cols,
i = 0,
len = 256;
for(i; i<len; i++) {
rows[i] = new Array(len);
}
function set(num, val, x) {
var max = len;
while(max--) {
if(x === "row") {
rows[num][max] = val;
} else {
rows[max][num] = val;
}
}
return rows;
}
function queryItem(num, x) {
var total = 0,
i = 0,
pos;
for(i; i<len; i++) {
pos = (x === "row") ? rows[num][i] : rows[i][num];
total += (typeof pos === "number") ? Number(pos) : 0;
}
return total;
}
lines.forEach(function(line) {
line = line.split(" ");
var query = line[0];
switch(query) {
case "SetCol":
set(line[1], Number(line[2]), "col");
break;
case "SetRow":
set(line[1], Number(line[2]), "row");
break;
case "QueryCol":
console.log(queryItem(line[1], "col"));
break;
case "QueryRow":
console.log(queryItem(line[1], "row"));
break;
}
});