Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 83 additions & 6 deletions canvas_exercise/shapes_game/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
window.addEventListener("load", function() {

function clear(ctx, width, heigt) {
function clear(ctx, width, height) {
ctx.clearRect(0, 0, width, height);
}

function drawRandomShape(ctx, width, height) {
function drawRandomShape(ctx) {
//0 = white triangle 1 = red triangle 2 = white square 3 = red square
var shapes = [0, 1, 2, 3];
var x = Math.floor(Math.random() * (650 - 100) + 100);
var y = Math.floor(Math.random() * (650 - 100) + 100);

//{white0: 38, red1: 40, red0: 37, white1: 39}
if (shapes.indexOf(Math.floor(Math.random() * (4 - 0))) === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than produce a random number in each if statement, save the result as a variable outside of the if.

var num = Math.floor(Math.random() * 4);

ctx.fillStyle = 'white';
ctx.beginPath();
ctx.moveTo(x, x);
ctx.lineTo(x, x + 70);
ctx.lineTo(x + 70, x + 70);
ctx.fill();
ctx.closePath();
return 'white0';
}
else if (shapes.indexOf(Math.floor(Math.random() * (4 - 0))) === 1) {
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.moveTo(x, x);
ctx.lineTo(x, x + 70);
ctx.lineTo(x + 70, x + 70);
ctx.fill();
ctx.closePath();
return 'red0';
}
else if (shapes.indexOf(Math.floor(Math.random() * (4 - 0))) === 2) {
ctx.fillStyle = 'white';
ctx.fillRect(x, y, 70, 70);
return 'white1';
}
else {
ctx.fillStyle = 'red';
ctx.fillRect(x, y, 70, 70);
return 'red1';
}
}

function drawGameStartText(ctx, width, height, score) {
ctx.fillStyle = 'white';
ctx.font = '36px serif';
ctx.fillText('Press the space bar to start a new game', width, height);
if (score !== undefined) {
ctx.fillText('Score: ' + score, width + 225, height + 50);
}
}

function restartGame(ctx, width, height) {
countdown = 30;
scoreSpan.innerHTML = 0;
}

var canvas = document.getElementById("shapes-game"),
Expand All @@ -23,14 +68,46 @@ window.addEventListener("load", function() {
expectedKeysMap = {white0: 38, red1: 40, red0: 37, white1: 39},
timerSpan = document.getElementById("time-remaining"),
scoreSpan = document.getElementById("score-val"),
seconds = 3,
countdown = 30,
intervalId;

canvas.width = width;
canvas.height = height;

document.addEventListener("keyup", function() {

document.addEventListener("keyup", function(event) {
if (!gameOn && event.keyCode === 32) {
gameOn = true;
clear(ctx, width, height);
expectedKey = expectedKeysMap[drawRandomShape(ctx)];
intervalId = setInterval(function() {
countdown--;
if (countdown === 0) {
clearInterval(intervalId);
gameOn = false;
var endScore = +scoreSpan.innerHTML;
restartGame();
clear(ctx, width, height);
drawGameStartText(ctx, 100, 400, endScore);
}
timerSpan.innerHTML = countdown;
}, 1000);
//0 = white triangle (up) 1 = red triangle(left) 2 = white square(right) 3 = red square(down)
}
else if (gameOn && Object.values(expectedKeysMap).includes(event.keyCode)) {
if (expectedKey === event.keyCode) {
scoreSpan.innerHTML = Number(scoreSpan.innerHTML) + 1;
}
else {
scoreSpan.innerHTML = Number(scoreSpan.innerHTML) - 1;
}
clear(ctx, width, height);
expectedKey = expectedKeysMap[drawRandomShape(ctx)];
}
});
});
drawGameStartText(ctx, 100, 400);

//once key gets pressed it runs functions attached
//waits for next key to be pressed
//
//match expectedKey to expectedKeysMap
});
172 changes: 129 additions & 43 deletions lodash_exercise/lodash.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,143 @@
function drop(){

function drop(arr, num){
if (num === 0) return arr;
if (!num) num = 1;
return arr.slice(num);
}

function fromPairs(){

function fromPairs(arr){
var newObj = {};
for (var i = 0; i < arr.length; i++) {
newObj[arr[i][0]] = arr[i][1];
}
return newObj;
}

function head(){

function head(arr){
return arr.shift();
}

function take(){

function take(arr, num){
if (num === 0) return [];
if (!num) return [arr[0]];
return arr.slice(0, num);
}

function takeRight(){

function takeRight(arr, num){
if (num === 0) return [];
if (!num) return [arr.length];
if (num > arr.length -1) return arr;
return arr.slice(num - 1, arr.length);
}

function union(){

}

function zipObject(){

}

function includes(){

}

function sample(){

}

function cloneDeep(){

}

function sumBy(){

}

function inRange(){

}

function has(){

}

function omit(){

var array = [];
for (var i = 0; i < arguments.length; i++) {
array = array.concat(arguments[i]);
}
return array.filter(function(val, index, arr) {
return arr.indexOf(val) === index;
});
}

function zipObject(arr1, arr2){
var myObj = {};
for (var i = 0; i < arr1.length; i++) {
myObj[arr1[i]] = arr2[i];
}
return myObj;
}

function includes(collection, src){
if (arguments.length > 2) return false;
if (typeof collection === 'string') {
if (collection.match(src)) return true;
return false;
}
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
if (collection[i] === src) return true;
else return false;
}
}
if (typeof collection === 'object') {
for (var key in collection) {
if (collection[key] === src) return true;
return false;
}
}
}

function sample(arr){
return arr[Math.floor(Math.random() * arr.length)];
}

function cloneDeep(val){
if (Array.isArray(val)) {
var newVal = [];
for (var i = 0; i < val.length; i++){
newVal = newVal.concat(cloneDeep(val[i]));
}
return newVal;
}
else {
var newVal = {};
for (var key in val) {
newVal[key] = val[key];
}
return newVal;
}
}

function sumBy(arr, key){
return arr.reduce(function(all, item, index){
if (typeof key === 'function') {
return all + key(item);
}
return all + item[key];
}, 0);
}

function inRange(num, start, end){
if (start > end) {
if (num > end & num < start) return true;
}
if (typeof end === 'undefined') {
end = start;
start = 0;
}
if (num > start && num < end) return true;
else return false;
}

function has(obj, path){
var isTrue = true;
if (Array.isArray(path)) {
for (var i = 0; i < path.length; i++) {
for (var key in obj) {
if (obj[key] === path[i]) isTrue && true;
else isTrue && false;
}
}
}
else {
for (var key in obj) {
if (obj[key] === path) isTrue && true;
else isTrue && false;
}
}
return isTrue;
}

function omit(obj, path){
var newObj = obj;
for (var i = 0; i < path.length; i++) {
for (var key in newObj) {
if (newObj[key] === path) {
delete newObj[key];
}
}
}
return newObj;
}

function pick(){
Expand Down
34 changes: 34 additions & 0 deletions recursion_exercise/recursion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function productOfArray(arr) {
if (arr.length === 0) return 1;
return arr[0] * productOfArray(arr.splice(1));
}

function collectStrings(obj) {
var newArray = [];
function helper(myObj) {
for (var key in myObj) {
if (typeof myObj[key] === 'object') {
helper(myObj[key]);
}
else {
newArray.push(myObj[key]);
}
}
}
helper(obj);
return newArray;
}

function contains(obj, src) {
var isFound = false;
for (var key in obj) {
if (typeof obj[key] === 'object') {
isFound = contains(obj[key], src);
}
else if (obj[key] === src) {
return true;
}
if (isFound) return true;
}
return false;
}
41 changes: 41 additions & 0 deletions testing_exercise/testing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function replaceWith(str, char, replaced) {
return str.split(char).join(replaced);
}

function expand(array, num) {
var newArray = [];
for (var i = 0; i < num; i++) {
for(var j = 0; j < array.length; j++) {
newArray.push(array[j]);
}
}
return newArray;
}

function acceptNumbersOnly() {
var isTrue = true;
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] === parseInt(arguments[i])) {
isTrue = isTrue && true;
}
else isTrue = false;

}
return isTrue;
}

function mergeArrays(array1, array2) {
var newArray = array1.concat(array2);
return newArray.sort(function(a, b) {
return a - b;
});
}

function mergeObjects(obj1, obj2) {
for (var key in obj2) {
if (obj2.hasOwnProperty(key)){
obj1[key] = obj2[key];
}
}
return obj1;
}
Loading