This one\'s a bit tricky, but I think you guys can help. Here\'s what I have to
ID: 3547443 • Letter: T
Question
This one's a bit tricky, but I think you guys can help. Here's what I have to do:
My HTML/Javascript page will allow the user to enter numbers once the button is clicked.
Once the button is clicked, the user will be prompted 16 times, once for each number, to be entered one at a time.
Once a number is chosen, it appears in the TABLE that I've created in the example you'll see in a moment.
The numbers create a two-dimensional array. In this array, every row, column, and the two diagonals will add up to the SAME NUMBER.
If this is TRUE, a message appears under the button, stating that it is TRUE. If it's FALSE, then the message reads that it's FALSE.
Here's what I have so far, including my Javascript Array attempt, as well as my table.
<script type="text/javascript">
function getNumbers ()
{var a=prompt "Enter number for row 1 column 1";
var b=prompt "Enter number for row 1 column 2";
var c=prompt "Enter number for row 1 column 3";
var d=prompt "Enter number for row 1 column 4";
var e=prompt "Enter number for row 2 column 1";
var f=prompt "Enter number for row 2 column 2";
var g=prompt "Enter number for row 2 column 3";
var h=prompt "Enter number for row 2 column 4";
var i=prompt "Enter number for row 3 column 1";
var j=prompt "Enter number for row 3 column 2";
var k=prompt "Enter number for row 3 column 3";
var l=prompt "Enter number for row 3 column 4";
var m=prompt "Enter number for row 3 column 1";
var n=prompt "Enter number for row 4 column 2";
var o=prompt "Enter number for row 4 column 3";
var p=prompt "Enter number for row 4 column 4";
function createMultiArray()
{MultiArray = newArray(8)
MultiArray [0] = newArray(4)
MultiArray [0][0] = a
MultiArray [0][1] = b
MultiArray [0][2] = c
MultiArray [0][3] = d
MultiArray [1] = newArray(4)
MultiArray [1][0] = e
MultiArray [1][1] = f
MultiArray [1][2] = g
MultiArray [1][3] = h
MultiArray [2] = newArray(4)
MultiArray [2][0] = i
MultiArray [2][1] = j
MultiArray [2][2] = k
MultiArray [2][3] = l
MultiArray [3] = newArray(4)
MultiArray [3][0] = m
MultiArray [3][1] = n
MultiArray [3][2] = o
MultiArray [3][3] = p
}
</script>
</head>
<body>
<form name="form1">
<table class="tg-table-plain">
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>e</td>
<td>f</td>
<td>g</td>
<td>h</td>
</tr>
<tr>
<td>i</td>
<td>j</td>
<td>k</td>
<td>l</td>
</tr>
<tr>
<td>m</td>
<td>n</td>
<td>o</td>
<td>p</td>
</tr>
</table>
<p><input type="Button" value="Check" /></p>
<p id="lbl1" > </p>
</form>
</body>
</html>
Explanation / Answer
<script type="text/javascript">
function getNumbers()
{
var arr1 = [];
var arr2 = [];
var r1,r2,r3,r4,c1,c2,c3,c4,d1,d2 = 0;
for(var i=1;i<=4;i++)
{
for(var j=1;j<=4;j++)
{
var k=prompt("Enter number for row "+i+" column "+j);
arr2[j-1] = k;
var tmp = i.toString() + j.toString();
document.getElementById( tmp ).textContent = k;
}
arr1.push(arr2);
}
//columns sum
c1=parseInt(arr1[0][0])+parseInt(arr1[1][0])+parseInt(arr1[2][0])+parseInt(arr1[3][0]);
c2=parseInt(arr1[0][1])+parseInt(arr1[1][1])+parseInt(arr1[2][1])+parseInt(arr1[3][1]);
c3=parseInt(arr1[0][2])+parseInt(arr1[1][2])+parseInt(arr1[2][2])+parseInt(arr1[3][2]);
c4=parseInt(arr1[0][3])+parseInt(arr1[1][3])+parseInt(arr1[2][3])+parseInt(arr1[3][3]);
//diagonals sum
d1=parseInt(arr1[0][0])+parseInt(arr1[1][1])+parseInt(arr1[2][2])+parseInt(arr1[3][3]);
d2=parseInt(arr1[0][3])+parseInt(arr1[1][2])+parseInt(arr1[2][1])+parseInt(arr1[3][0]);
if(get_sum(arr1[0])==get_sum(arr1[0]) && get_sum(arr1[1])==get_sum(arr1[2]) && get_sum(arr1[0])==get_sum(arr1[3])){
if(c1==c2 && c2==c3 && c1==c4){
if(d1==d2){
document.getElementById( 'lbl1' ).textContent = true;
}
else{
document.getElementById( 'lbl1' ).textContent = false;
}
}else{
document.getElementById( 'lbl1' ).textContent = false;
}
}
else
{
document.getElementById( 'lbl1' ).textContent = false;
}
function get_sum(a){
return parseInt(a[0])+parseInt(a[1])+parseInt(a[2])+parseInt(a[3]);
}
}
</script>
</head>
<body>
<form name="form1">
<table class="tg-table-plain">
<tr>
<td id="11">a</td>
<td id="12">b</td>
<td id="13">c</td>
<td id="14">d</td>
</tr>
<tr>
<td id="21">e</td>
<td id="22">f</td>
<td id="23">g</td>
<td id="24">h</td>
</tr>
<tr>
<td id="31">i</td>
<td id="32">j</td>
<td id="33">k</td>
<td id="34">l</td>
</tr>
<tr>
<td id="41">m</td>
<td id="42">n</td>
<td id="43">o</td>
<td id="44">p</td>
</tr>
</table>
<p><input type="Button" value="Check" /></p>
<p id="lbl1" > </p>
</form>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.