-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (108 loc) · 5.16 KB
/
script.js
File metadata and controls
114 lines (108 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// https://www.w3schools.com/js/js_arrays.asp
var all = [0, 0, 0, 0, 0, 0, 0, 0, 0];
var n1 = [], n2 = [], n4 = [], n8 = [],
n16 = [], n32 =[], n64 =[], n128 =[], nAll = [], AllAry = [];
for (var a = 1; a <= 42; a++) {
// http://www.javascriptkit.com/javatutors/randomnum.shtml
// https://stackoverflow.com/questions/9939760/how-do-i-convert-an-integer-to-binary-in-javascript
var Random = Math.floor(Math.random()*256) + 256; // Random number
if (Random == 256){Random = 257};
var RandomNumber = (Random).toString(2) // Converts to binary
// https://stackoverflow.com/questions/9951500/javascriptstoring-values-in-array
for (var i = 0; i < RandomNumber.length; i++) {
all[i] = RandomNumber.charAt(i)
if (i == 8 && all[8] == 1) {n1[n1.length] = Random};
if (i == 7 && all[7] == 1) {n2[n2.length] = Random};
if (i == 6 && all[6] == 1) {n4[n4.length] = Random};
if (i == 5 && all[5] == 1) {n8[n8.length] = Random};
if (i == 4 && all[4] == 1) {n16[n16.length] = Random};
if (i == 3 && all[3] == 1) {n32[n32.length] = Random};
if (i == 2 && all[2] == 1) {n64[n64.length] = Random};
if (i == 1 && all[1] == 1) {n128[n128.length] = Random};
}
nAll[nAll.length] = Random;
} // End Random # Generator loop
DisplayNumbers(nAll);
document.getElementById('Button1').innerHTML = "I have choosen my number";
var NextScreen = .5;
var MyGuess = 256;
var Counter = 0;
function Start(){
NextNumber();
document.getElementById("Display2").innerHTML = "";
document.getElementById("Display3").innerHTML = "Is your number <br>on the screen? <br> You are on screen " + Counter + " of 8";
document.getElementById('Button1').innerHTML = "Yes I see my number";
// https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_pushbutton_create
var x = document.createElement("BUTTON");
var t = document.createTextNode("NO it is NOT there");
x.appendChild(t);
x.id ='Button2';
document.body.appendChild(x);
//https://stackoverflow.com/questions/5303899/change-onclick-action-with-a-javascript-function
document.getElementById("Button1").onclick = Yes;
document.getElementById("Button2").onclick = NextNumber;
}
function Yes(){
MyGuess += NextScreen;
NextNumber();
}
function NextNumber(){
Counter += 1;
document.getElementById("Display3").innerHTML = "Is your number<br> on the screen? <br> You are on screen " + Counter + " of 8";
if (Counter == 9){End(); }
if (NextScreen == .5) {DisplayNumbers(n1); }
if (NextScreen == 1) {DisplayNumbers(n2); }
if (NextScreen == 2) {DisplayNumbers(n4); }
if (NextScreen == 4) {DisplayNumbers(n8); }
if (NextScreen == 8) {DisplayNumbers(n16); }
if (NextScreen == 16) {DisplayNumbers(n32); }
if (NextScreen == 32) {DisplayNumbers(n64); }
if (NextScreen == 64) {DisplayNumbers(n128); }
NextScreen *= 2;
}
function End(){
document.getElementById("Display1").innerHTML = "";
document.getElementById("Display2").innerHTML = "<h2>" + "Your number is: " + MyGuess + "</h2>" ;
document.getElementById("Display3").innerHTML = "";
document.getElementById('Button1').innerHTML = "New Game!";
document.getElementById('Button1').onclick = newgame;
var element = document.getElementById('Button2');
element.parentNode.removeChild(element);
document.getElementById("Table").innerHTML = "";
}
function newgame(){
document.location.reload(true);
}
//https://stackoverflow.com/questions/3481491/is-there-any-way-i-can-print-string-array-without-using-for-loop
function DisplayNumbers(x){
document.getElementById("Display1").innerHTML = "";
document.getElementById("Table").innerHTML = "";
var table = document.getElementById("Table");
// https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_table_insertrow
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var newRow = 0;
// https://stackoverflow.com/questions/15866451/how-to-insert-row-at-end-of-table-with-htmltableelement-insertrow
for (var i = 0; i < (x.length); i++ ) {
if (i % 6 == 0 && i != 0 ){
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
newRow += 1;
}
var cell1 = row.insertCell(i - (newRow * 6));
// https://www.quora.com/How-can-I-change-the-color-of-inner-HTML-var-value-text-with-JavaScript
// http://w3schools-fa.ir/colors/colors_wheels.html
if (x[i] % 2 == 0) {spanString = "<span id=\"span1\" style=\"color:"}
else {spanString = "<span id=\"span2\" style=\"color:"}
if ( x[i] <= 284) {cell1.innerHTML = spanString + "#FF0000\">" + x[i] + "</span>"} //red
if ( x[i] > 284) {cell1.innerHTML = spanString + "#FF8000\">" + x[i] + "</span>"} //orange
if ( x[i] > 313) {cell1.innerHTML = spanString + "#FFFF00\">" + x[i] + "</span>"} //yellow
if ( x[i] > 341) {cell1.innerHTML = spanString + "#00FF00\">" + x[i] + "</span>"} //green
if ( x[i] > 370) {cell1.innerHTML = spanString + "#00FFFF\">" + x[i] + "</span>"} // cyan
if ( x[i] > 398) {cell1.innerHTML = spanString + "#ADD8E6\">" + x[i] + "</span>"} //light blue
if ( x[i] > 427) {cell1.innerHTML = spanString + "#FFFFFF\">" + x[i] + "</span>"} //white
if ( x[i] > 455) {cell1.innerHTML = spanString + "#9932CC\">" + x[i] + "</span>"} //purple
if ( x[i] > 483) {cell1.innerHTML = spanString + "#FF00FF\">" + x[i] + "</span>"} //magenta
if ( x[i] > 512) {cell1.innerHTML = spanString + "#FF0080\">" + x[i] + "</span>"} //pink
}
}