Skip to content

Commit 5fa5f63

Browse files
author
Chris Bee
committed
Initial commit
0 parents  commit 5fa5f63

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01-random/codewars2.js

01-random/distance.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var distance = function (x1, y1, x2, y2) {
2+
var xlen = 0;
3+
var ylen = 0;
4+
5+
if (x1 > x2){
6+
xlen = x1 - x2;
7+
} else {
8+
xlen = x2 - x1;
9+
}
10+
11+
if (y1 > y2) {
12+
ylen = y1 - y2;
13+
} else {
14+
ylen = y2 - y1;
15+
}
16+
17+
ylen = (ylen * (111.320 * Math.cos(x1 - xlen)) / 0.62137);
18+
xlen = (xlen * 110.574) / 0.62137;
19+
console.log(ylen, xlen);
20+
21+
return Math.sqrt(Math.pow(xlen,2) + Math.pow(ylen,2));
22+
23+
}
24+
25+
26+
console.log(distance(37.7836966, -122.4089664, 37.7916655, -122.3952124));

01-random/primeNumbers.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
///Gets prime numbers up to N///
2+
3+
var Util = function () {
4+
this.hash = {};
5+
this.hash.count = 0;
6+
7+
}
8+
9+
Util.prototype.isPrime = function (num) {
10+
for (var i = num - 1; i > 1; i--) {
11+
if (num % i === 0) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
};
17+
18+
Util.prototype.createPrimes = function (n) {
19+
for (var i = 2; i < n; i++) {
20+
if(this.isPrime(i)){
21+
this.hash[i] = i;
22+
this.hash.count++;
23+
}
24+
}
25+
}
26+
27+
Util.prototype.getPrimes = function () {
28+
return this.hash;
29+
}
30+
31+
32+
33+
34+
var Primes = {
35+
first : function (n) {
36+
var results = [];
37+
var hash = {};
38+
hash[2] = 2;
39+
40+
41+
var isPrime = function (num) {
42+
////naive approach////
43+
// for (var i = num - 1; i > 1; i--) {
44+
// if (num % i === 0) {
45+
// return false;
46+
// }
47+
// }
48+
// return true;
49+
50+
////prime only approach///
51+
for (var key in hash) {
52+
if(num % key === 0) {
53+
return false;
54+
}
55+
}
56+
hash[num] = num;
57+
console.log(hash);
58+
return true;
59+
};
60+
61+
var check = function (currNum) {
62+
if(results.length === n) {
63+
console.log("hit me");
64+
return;
65+
} else {
66+
67+
68+
if(isPrime(currNum)) {
69+
results.push(currNum);
70+
}
71+
check(currNum + 1);
72+
}
73+
}
74+
75+
check(2);
76+
return results;
77+
}
78+
}
79+
80+
81+
82+
console.log(Primes.first(266));
83+
84+

01-random/tictactoe.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//Determines if tic tac toe board is solved and who the winner is
2+
3+
var isSolved = function (board) {
4+
5+
var m = new Moves();
6+
var count = 0;
7+
8+
for (var i = 0; i < board.length; i++) {
9+
for (var k = 0; k < board[i].length; k++) {
10+
if(board[i][k] !== 0) {
11+
var val = board[i][k];
12+
count++;
13+
//console.log(val, i, k);
14+
//loop through moves, starting at current val
15+
for (var mo = 0; mo < m.length; mo++) {
16+
var movedCoords = m[mo]([i, k]);
17+
//console.log([i, k], movedCoords);
18+
if(movedCoords && board[movedCoords[0]][movedCoords[1]] === val) {
19+
var movedCoords2 = m[mo](movedCoords);
20+
if(movedCoords2 && board[movedCoords2[0]][movedCoords2[1]] === val) {
21+
return val;
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
if(count <= 8) {
29+
return -1;
30+
}
31+
return 0;
32+
}
33+
34+
var Moves = function () {
35+
var moves = [];
36+
37+
var moveRight = function (coords) {
38+
var newCoords = [coords[0], coords[1]+1];
39+
if (newCoords[0] > 2 || newCoords[1] > 2) {
40+
newCoords = null;
41+
}
42+
return newCoords;
43+
}
44+
45+
var moveDown = function (coords) {
46+
var newCoords = [coords[0]+1, coords[1]];
47+
if (newCoords[0] > 2 || newCoords[1] > 2) {
48+
newCoords = null;
49+
}
50+
return newCoords;
51+
}
52+
53+
var moveDiag = function (coords) {
54+
var newCoords = [coords[0]+1, coords[1]+1];
55+
if (newCoords[0] > 2 || newCoords[1] > 2) {
56+
newCoords = null;
57+
}
58+
return newCoords;
59+
}
60+
61+
var moveDiagBack = function (coords) {
62+
var newCoords = [coords[0]+1, coords[1]-1];
63+
if (newCoords[0] > 2 || newCoords[1] > 2 || newCoords[0] < 0 || newCoords[1] < 0) {
64+
newCoords = null;
65+
}
66+
return newCoords;
67+
}
68+
69+
moves.push(moveRight);
70+
moves.push(moveDown);
71+
moves.push(moveDiag);
72+
moves.push(moveDiagBack);
73+
74+
return moves;
75+
76+
}
77+
78+
79+
var testBoard = [[0,2,1],
80+
[0,1,2],
81+
[1,2,1]];
82+
83+
console.log(isSolved(testBoard));
84+

blackjack/blackjack.js

Whitespace-only changes.

0 commit comments

Comments
 (0)