charlie mcadams

Homework for 22nd March. Get the code working locally.

There will be a quiz next week to see who goes onto the A-Team ( research and development ).

What you need to do is download the code , get it working and most importantly - understand all the lines of code. If you do well on the quiz you have a better chance of going to the R+D team, if you want to. There are a couple of questions below which will be asked in class as well.

Anyway, best thing to do is go through each line in each of the 4 functions and understand what they all mean.

There are 4 buttons on the page and each button executes a different function.

This code is limited to converting a six digit binary string to decimal.

If you use a 5 digit binary like 10010 the program will crash - why is that?

The function called binary3 wont crash - but it gives the wrong answer - why is that?

There is only difference between the code in binary2 and binary3 - what is it?

Could it be why binary3 doesnt give NaN ? - what is NaN? for the lazy 

The function called binary4 is basically empty - you can play around in there and maybe get a working version.

Top marks if you can modify binary3 to give the correct answer.

Top top marks if you modify binary4 to work for any number of digits.

 

The binary code is located here

What you need to do is get this working locally.
That means copying all the code onto your laptop and running it from there
Your local copy should be the exact same as on this site.


Go to the page and press control key and the U key. This should show you the source code. 
Copy this source code to your local html file. 
Open this html file in your browser- the conversion should work. 
If it doesnt, you have done something wrong. 
Fix it. 

Heres a copy of the code if you cant get the source working - ignore the /***/ at the top and bottom

 

/******copy below******/

 

 

<!doctype html>

<script>

 

function binary1(){

 

alert("starting bin1");

 

var input = 0;

var result = 0;

var aBinary = [];

 

input = document.getElementById("input2").value;

 

aBinary = input.split('');

 

var number1 = aBinary[0] * 32;

var number2 = aBinary[1] * 16;

var number3 = aBinary[2] * 8;

var number4 = aBinary[3] * 4;

var number5 = aBinary[4] * 2;

var number6 = aBinary[5] * 1;

 

result = number1 + number2 + number3 + number4 + number5 + number6;

 

alert("result from bin2= " +result);

 

}

 

function binary2(){

 

alert("starting bin2");

 

var input = 0;

var result = 0;

var aBinary = [];

 

input = document.getElementById("input2").value;

 

aBinary = input.split('');

 

var factor = [32,16,8,4,2,1];

 

for ( var i =0 ;i < 6 ;i++){

result = result + (aBinary[i] * factor[i]);

}

 

alert("result from bin2= " +result);

 

}

 

function binary3(){

 

alert("starting bin3");

 

var input = 0;

var result = 0;

var aBinary = [];

 

input = document.getElementById("input2").value;

 

aBinary = input.split('');

 

var factor = [32,16,8,4,2,1];

 

for ( var i =0 ;i < aBinary.length ;i++){

result = result + (aBinary[i] * factor[i]);

}

 

alert("result from bin3= " +result);

 

}

function binary4(){

 

alert("starting bin4");

 

var input = 0;

var result = 0;

var aBinary = [];

 

input = document.getElementById("input2").value;

 

aBinary = input.split('');

 

alert("result from bin4= " +result);

 

}

 

 

</script>

<html>

<body>

 <textarea id="input2" ></textarea> 

 <br><br><button onclick="binary1()">bin1</button>

 <br><br><button onclick="binary2()">bin2</button>

 <br><br><button onclick="binary3()">bin3</button>

 <br><br><button onclick="binary4()">bin4</button>

</body>

</html>

 

 

 

 

/******copy above*******/

 

 

 

The new space game is located here

What you need to do is get this working locally.
That means copying all the code onto your laptop and running it from there
Your local game should be the exact same as on this site.
You need to have 4 files copied locally for this to work.
They are called


pvgf.html
pvgf.js
vector.js
ship.js

You should already have these files.
You just need to update the content of each file.
How to do this
We need to copy in the correct code


Go to here and copy the code into your local pvgf.js file

Go to here and copy the code into your local vector.js file

Go to here and copy the code into your local ship.js file
Just one file left - the html file.
Go to here and press control key and the U key. This should show you the source code.
Copy this source code to your local pvgf.html file.
Open this pvgf.html file in your browser- the game should work.
If it doesnt, you have done something wrong.
Fix it.

if you want the space background working then you have to download this image to your local folder.
Things to try when you get it working

You only ever need to change the ship.js file
You can always copy the original contents of the file if you mess it up.

1 Change color of earth
2 Change color of blackhole
3 Change color of ship
4 Add a message when you hit the green dot ( earth! )
5 Add a message when you hit the black hole
6 Add another blackhole
7 Add another earth