# Thread: Difficulties understanding the directions in Chapter 7 of Eloquent Javascript

1. Registered User
Join Date
Apr 2016
Posts
6

## Difficulties understanding the directions in Chapter 7 of Eloquent Javascript

I found the directions in Chapter 7 of Eloquent Javascript(Project: Electronic Life) a little confusing.

It is defined as follow:

Code:
```var directions = {
"n":  new Vector( 0, -1),
"ne": new Vector( 1, -1),
"e":  new Vector( 1,  0),
"se": new Vector( 1,  1),
"s":  new Vector( 0,  1),
"sw": new Vector(-1,  1),
"w":  new Vector(-1,  0),
"nw": new Vector(-1, -1)
};```
I would suppose that "n" should be Vector(0,1) rather than (0,-1), while "s" should be Vector(0,-1) and not (0,1).

Could someone who has read this classic book give some clue please? Thanks a lot.

2. Registered User
Join Date
Dec 2012
Posts
1,061
I assume that the first value refers to the x-coordinate and the second to y.
y positive -> down -> south direction
y negative -> up -> north direction
Thus I think that the value for "n" is correct.

3. Registered User
Join Date
Apr 2016
Posts
6
Hi Sempervivum,

Thank you for replying, but isn't y positive --> UP?

So I would think that they got these two reverse? Don't you think?

4. Spasmodic User
Join Date
Jul 2011
Posts
211
-1 0 1
0
1

do you think these numbers are in
the correct order ?

5. Registered User
Join Date
Apr 2016
Posts
6
I see. I thought it was quadrant I. But when programming we are using quadrant IV.
Thanks for this short but very powerful hint!

6. Registered User
Join Date
Apr 2016
Posts
6
By the way, is there a way to find someone to pair up with and study the book together? Sometimes I find having a different perspective helps immensely. Anywhere I can find such a study mate? Thanks.

7. Originally Posted by niujinghui
I found the directions in Chapter 7 of Eloquent Javascript(Project: Electronic Life) a little confusing.

It is defined as follow:

Code:
```var directions = {
"n":  new Vector( 0, -1),
"ne": new Vector( 1, -1),
"e":  new Vector( 1,  0),
"se": new Vector( 1,  1),
"s":  new Vector( 0,  1),
"sw": new Vector(-1,  1),
"w":  new Vector(-1,  0),
"nw": new Vector(-1, -1)
};```
I would suppose that "n" should be Vector(0,1) rather than (0,-1), while "s" should be Vector(0,-1) and not (0,1).

Could someone who has read this classic book give some clue please? Thanks a lot.
It helps others if you give a link if you are quoting elements of a book like so http://eloquentjavascript.net/07_elife.html

8. Registered User
Join Date
Apr 2016
Posts
6
Originally Posted by \\.\
It helps others if you give a link if you are quoting elements of a book like so http://eloquentjavascript.net/07_elife.html
Thanks for noting this, I got it!

9. Originally Posted by niujinghui
Thanks for noting this, I got it!
No, it `helps others` who are helping you, so thanks for nothing comment is not very productive.

10. Registered User
Join Date
Sep 2007
Posts
390
If I understand
Code:
```
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style type="text/css">
input {width:100px; height:50px; }
</style>
<body>
<div id="mekan"></div>
<table><tr>
<td><input type="button" value="kb (-1,-1)" title="kuzeybatı"  onclick="hareketEt(oYer, oYer.arti(yon['kb']) )"></td>
<td><input type="button" value="k (0,-1)" title="kuzey" onclick="hareketEt(oYer, oYer.arti(yon['k']) )"></td>
<td><input type="button" value="kd (1,-1)" title="kuzeydoğu"  onclick="hareketEt(oYer, oYer.arti(yon['kd']) )"></td>
</tr><tr>
<td><input type="button" value="b (-1,0)" title="batı"  onclick="hareketEt(oYer, oYer.arti(yon['b']) )"></td>
<td></td>
<td><input type="button" value="d (1,0)" title="doğu"  onclick="hareketEt(oYer, oYer.arti(yon['d']) )"></td>
</tr><tr>
<td><input type="button" value="gb (-1,1)" title="güneybatı"  onclick="hareketEt(oYer, oYer.arti(yon['gb']) )"></td>
<td><input type="button" value="g (0,1)" title="güney"  onclick="hareketEt(oYer, oYer.arti(yon['g']) )"></td>
<td><input type="button" value="gd (1,1)" title="güneydoğu"  onclick="hareketEt(oYer, oYer.arti(yon['gd']) )"></td>
</tr></table>

<script type="text/javascript">

var plan = ["###########################",
"#                         #",
"#                         #",
"#                         #",
"#                         #",
"#            O            #",
"#                         #",
"#                         #",
"#                         #",
"#                         #",
"###########################"];

function Vektor(x, y) {
this.x = x;
this.y = y;
}

var oYer;
// plan isimli dizinin elemanlarını tabloya yerleştirelim.

function tabloYap(){
var i,n,renk, t="<table id='alan'>";
for(var i=0; i<plan.length; i++){
t += "<tr>";
for(n=0; n<plan[i].length; n++){
if(plan[i][n]=="#"){renk ="blue";}
if(plan[i][n]=="O"){renk ="yellow";   oYer= new Vektor(n,i);     } // O nun olduğu yerin x ve y değerlerini oYer nesnesine gönderelim

t += "<td style='background-color:"+renk+"'>"+plan[i][n]+"</td>";
}
t += "</tr>";
}
t += "</table>";

var el= document.getElementById('mekan');
el.innerHTML = t;
}

tabloYap();
// O nun x ve y değerleri
alert("Başlangıçaki değerler: \noYer.x = "+ oYer.x + "\n oYer.y= "+ oYer.y);

// yon nesnesindeki mesela new Vektor( 0, -1) deki x ve y değerlerini ekleyerek yeni yeri belirlemek.

Vektor.prototype.arti = function(diger) {
return new Vektor(this.x + diger.x, this.y + diger.y);
};

// kuzey, kuzeydoğu doğu güneydoğu güney güneybatı batı kuzeybatı
var ad = "k kd d gd g gb b kb".split(" ");
// yon nesnesinde yönler ve o yöne gitmesi için eklenecek sayılar belirtiliyor.
//   "k":  new Vektor( 0, -1),  mesela  x=x+0    y = y + ( -1 )  olacak
var yon = {
"k":  new Vektor( 0, -1),
"kd": new Vektor( 1, -1),
"d":  new Vektor( 1,  0),
"gd": new Vektor( 1,  1),
"g":  new Vektor( 0,  1),
"gb": new Vektor(-1,  1),
"b":  new Vektor(-1,  0),
"kb": new Vektor(-1, -1)
};
// rastgele bir sayı
var r = Math.floor(Math.random() * ad.length);

// hedeflenen yer
var oHedef = oYer.arti(yon[ad[r] ]);

alert("hedef: \n oHedef.x ="+ oHedef.x + " \n oHedef.y =    "+oHedef.y);

// önceki yerden sonraki yere taşıyalım
function hareketEt(eskiYer, yeniYer){
var el= document.getElementById('alan');
var tr= el.getElementsByTagName('tr');
var eskiTr= tr[eskiYer.y];
var eskiTd = eskiTr.getElementsByTagName('td')[eskiYer.x];
eskiTd.innerHTML = "";
eskiTd.style.backgroundColor="lime";
var yeniTr= tr[yeniYer.y];
var yeniTd = yeniTr.getElementsByTagName('td')[yeniYer.x];
yeniTd.innerHTML = "O";
yeniTd.style.backgroundColor="yellow";
oYer = new Vektor(yeniYer.x, yeniYer.y);
}

hareketEt(oYer, oHedef);

</script>
</body>
</html>```
Last edited by Ayşe; 04-19-2016 at 04:20 PM.

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•