Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions call_apply_bind_exercise/callApplyBind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function sumEvenArguments() {
var arr = [].slice.call(arguments);
var sum = 0;
for(var i = 0; i <= arr.length; i++) {
if (arr[i] % 2 === 0) {
sum += arr[i];
}
}
return sum;
}

function arrayFrom() {
var arr = [].slice.call(arguments);
return arr;
}

function invokeMax(fn, max) {
var counter = 0;
return function() {
var innerArgs = [].slice.call(arguments);
if (counter >= max) {
return "Maxed Out!";
} else {
counter++;
return fn.apply(this, arguments);
}
}
}
///yeah still need clarification on how this works ^
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me know if you want me to go over this with you. Looks like you go it right.


function guessingGame(amount) {
var answer = Math.floor(Math.random() * 10);
var guesses = 0;
return function(guess) {
guesses++;
if (guesses > amount) {
return "You are all done playing!";
} else if (answer < guess) {
return "You're too high!";
} else if (answer > guess) {
return "You're too low!";
} else if (answer === guess) {
return "You got it!";
}
}
}
32 changes: 20 additions & 12 deletions call_apply_bind_exercise/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ var obj = {
}
}
}

function sayHi() {
Copy link
Contributor

Choose a reason for hiding this comment

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

you could change this to:

 var obj = {
     fullName: "Harry Potter",
     person: {
         sayHi: function(){
             return "This person's name is " + this.fullName
          }
      }
  }
obj.person.sayHi = obj.person.sayHi.bind(obj);

That way, you preserve the structure.

return `This person's name is ${this.fullName}`;
}
var person = {
fullName : "Harry Potter"
}
sayHi.call(person);
```

- List two examples of "array-like-objects" that we have seen.
-
-
- arguments
- the result of getElementsByClassName

### Functions to write:

Make the tests pass for the following functions:
Make the tests pass for the following functions:

- Write a function called `sumEvenArguments` which takes all of the arguments passed to a function and returns the sum of the even ones.

Expand All @@ -29,7 +37,7 @@ sumEvenArguments(1,2,6) // 8
sumEvenArguments(1,2) // 2
```

- Write a function called `arrayFrom` which converts an array-like-object into an array.
- Write a function called `arrayFrom` which converts an array-like-object into an array.

```javascript
function sample(){
Expand All @@ -55,23 +63,23 @@ addOnlyThreeTimes(1,2) // 3
addOnlyThreeTimes(1,2) // "Maxed Out!"
```

Write a function called `guessingGame` which takes in one parameter `amount`. The function should return another function that takes in a parameter called `guess`. In the outer function, you should create a variable called `answer` which is the result of a random number between 0 and 10 as well as a variable called `guesses` which should be set to 0.
Write a function called `guessingGame` which takes in one parameter `amount`. The function should return another function that takes in a parameter called `guess`. In the outer function, you should create a variable called `answer` which is the result of a random number between 0 and 10 as well as a variable called `guesses` which should be set to 0.

In the inner function, if the guess passed in is the same as the random number (defined in the outer function) - you should return the string "You got it!". If the guess is too high return "You're too high!" and if it is too low, return "You're too low!". You should stop the user from guessing if the amount of guesses they have made is greater than the initial `amount` passed to the outer function.
In the inner function, if the guess passed in is the same as the random number (defined in the outer function) - you should return the string "You got it!". If the guess is too high return "You're too high!" and if it is too low, return "You're too low!". You should stop the user from guessing if the amount of guesses they have made is greater than the initial `amount` passed to the outer function.

You will have to make use of closure to solve this problem.

```javascript
var game = guessingGame(5)
game(1) // "You're too low!"
game(1) // "You're too low!"
game(8) // "You're too high!"
game(5) // "You're too low!"
game(7) // "You got it!"
game(1) // "You are all done playing!"
game(7) // "You got it!"
game(1) // "You are all done playing!"

var game2 = guessingGame(3)
game2(5) // "You're too low!"
game2(5) // "You're too low!"
game2(3) // "You're too low!"
game2(1) // "No more guesses the answer was 0"
game2(1) // "You are all done playing!"
game2(1) // "No more guesses the answer was 0"
game2(1) // "You are all done playing!"
```
110 changes: 106 additions & 4 deletions canvas_exercise/shapes_game/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,88 @@
window.addEventListener("load", function() {
//flow of what needs to happen:
Copy link
Contributor

Choose a reason for hiding this comment

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

Good start and your code looks clean so far. Try to add the seconds timer and stop the game after 30 seconds have passed.

//drawGameStartText - game starts text is drawn, text is cleared
//game starts when space bar is hit
//drawRandomShape - draws random shape
// shape attached to key
// check if key hit matches expectedkeymap
// also need to reassaign expectedkey from undefined to it's shape
var squareSize = 150;
var shapesArr = [
"squareWhite",
"triWhite",
"squareRed",
"triRed",
];
var currentShape = undefined;

function clear(ctx, width, heigt) {
function clear(ctx, width, height) {
//clears the screen
ctx.clearRect(0, 0, width, height);
}

function drawRandomShape(ctx, width, height) {
//draws square or triangle
//needs to be in a different location everytime called
clear(ctx, width, height);
var shapeNumber = Math.floor(Math.random() * 4);
var x = Math.floor(Math.random() * (width - squareSize));
var y = Math.floor(Math.random() * (height - squareSize));
draw(ctx, shapesArr[shapeNumber], x, y);
updateScore();
}

function updateScore(){
var span = document.getElementById("score-val");
span.innerText = points;
}

function draw(ctx, shape, x, y) {
currentShape = shape;
switch (shape) {
case "squareWhite":
ctx.fillStyle = "white";
ctx.fillRect(x, y, squareSize, squareSize);
break;
case "triWhite":
ctx.fillStyle = "white";
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(x + squareSize, y + squareSize);
ctx.lineTo(x, y + squareSize);
ctx.fill();
ctx.closePath();
break;
case "squareRed":
ctx.fillStyle = "red";
ctx.fillRect(x, y, squareSize, squareSize);
break;
case "triRed":
ctx.fillStyle = "red";
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(x + squareSize, y + squareSize);
ctx.lineTo(x, y + squareSize);
ctx.fill();
ctx.closePath();
break;
}
}

function drawGameStartText(ctx, width, height, score) {
gameOn = true;
ctx.font = '45px serif';
ctx.fillStyle = "white";
ctx.fillText('To start game, hit the space bar', 100, 300);
}

function restartGame(ctx, width, height) {
//gets called after finish first game
//timer reset
//score reset
//game cleared
//addEventListener for hitting the spacebar
//then call generate random shape

}

var canvas = document.getElementById("shapes-game"),
Expand All @@ -24,13 +97,42 @@ window.addEventListener("load", function() {
timerSpan = document.getElementById("time-remaining"),
scoreSpan = document.getElementById("score-val"),
seconds = 3,
points = 0,
intervalId;

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

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

document.addEventListener("keyup", function(event) {
if (event.keyCode === 32) { // spacebar
drawRandomShape(ctx, width, height);

} else if (event.keyCode === 37) { // left
if (currentShape === "triRed") {
points++;
} else {
points--;
}
} else if (event.keyCode === 38) { // up
if (currentShape === "triWhite") {
points++
} else {
points--;
}
} else if (event.keyCode === 39) { // right
if (currentShape === "squareWhite") {
points++;
} else {
points--;
}
} else if (event.keyCode === 40) { // down
if (currentShape === "squareRed") {
points++;
} else {
points--;
}
}
drawRandomShape(ctx, width, height);
});
drawGameStartText(ctx, width, height, 0);
});

Loading