PDA

View Full Version : JavaScript Help



salpha
Jan 19th, 2010, 09:39 PM
Hi All, I recently went to interview for Web Developer and got the following questions from my interviewer. I have no idea about how to solve this.

Consider a scenario like below:
<div id = test></div>
<table></table>
Given a 5 * 5 table consisting integer data. I need to write a function in JavaScript that reads table in the spiral way (see arrow in the table below to know the spiral )

[0][0]-----------------> [0][5]
[1][0]<----------------- [1][5]
[2][0]-----------------> [2][5]
[3][0]<----------------- [3][5]
[4][0]------------------>[4][5]

Compute the following in the function
1. Read table in spiral way
2. Sum up the values of row 1 and then calculate the average and put that value in row[2] column 1.
3. For the last row average value put in the div tag defined above in the question

Note: Not Allowed to use document.getElementById. Need to use core JavaScript functions.

Dormilich
Jan 19th, 2010, 11:49 PM
hint on the note: most browsers treat whitespace through indentation as a separate Node.

and of course the div should be written:

<div id="test"></div>
in XHTML it were even possible to write

<div id="test"/>
// or
<div xml:id="test"/>

_Aerospace_Eng_
Jan 19th, 2010, 11:54 PM
So what are you asking? You want us to help you solve a problem that you should know how to do if you are going to be doing the job you were interviewed for?

gusblake
Jan 20th, 2010, 03:22 AM
I take "don't use document.getElementById" to mean don't calculate everything by brute force:



var a1=document.getElementById("a1").innerHTML;
var a2=document.getElementById("a2").innerHTML;
...
var averageA=(a1+a2 etc)/5;


So you will need to use loops to handle the repetitive bits:



<html>
<head>
<title></title>
<script type="text/javascript">
function spiral() {
var table=document.getElementById("table");
var div=document.getElementById("test");

var row=table.getElementsByTagName("tr");
var cell;

var count;
var total;
var average;

for(var i=0; i<row.length; i++) {
cell=row[i].getElementsByTagName("td");

if(typeof average!="undefined") {
cell[0].childNodes[0].nodeValue=average;
}

count=cell.length;
total=0;

for(var j=0; j<cell.length; j++) {
total+=parseInt(cell[j].childNodes[0].nodeValue);
}

average=Math.round(total/count);

div.childNodes[0].nodeValue=average;
}
}
</script>
</head>
<body>
<div id="test">div</div>
<table id="table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
<input type="button" onclick="spiral()" value="averages">
</body>
</html>


I couldn't see the point of making it "spiral" (as far as I can tell it makes no difference to the ouput), so this code just starts from column 0 every time.