-
Notifications
You must be signed in to change notification settings - Fork 82
intermediate javascript exercise sols #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 18 commits
e020f7b
abfac49
5698a00
c1b0bc7
f6b21f4
b4d5310
a253fe5
6bd6d7b
26867d4
317c8c8
2cd69a8
38f8f15
d7d53b6
58f3863
ab7f4a1
a17aad4
3fefe46
c098e3d
3e0dffe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| var obj = { | ||
| fullName: "Harry Potter", | ||
| person: { | ||
| sayHi: function(){ | ||
| return "This person's name is " + this.fullName | ||
| }.bind(obj) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function sumEvenArguments() { | ||
| var args = [].slice.call(arguments); | ||
|
|
||
| return args.reduce(function(acc, nextValue) { | ||
| if( nextValue % 2 === 0 ) acc += nextValue; | ||
| return acc; | ||
| }, 0); | ||
|
|
||
| // return args.filter(function(a) { return (a % 2) === 0; }).reduce(function(acc, nextValue) { | ||
| // return acc + nextValue | ||
| // }, 0); | ||
| } | ||
|
|
||
| function arrayFrom() { | ||
| return [].slice.call(arguments); | ||
| } | ||
|
|
||
| function invokeMax(fn, num) { | ||
| var count = num; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you count down, you don't even need to copy the variable. Numbers are not reference types. |
||
| return function innerFunction(){ | ||
| if (count <= 0) { | ||
| return "Maxed Out!"; | ||
| } | ||
| else { | ||
| count--; | ||
| return fn.apply(this, arguments); | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| function guessingGame(amount) { | ||
| var answer = Math.floor(Math.random() * 11); | ||
| var guesses = 0; | ||
| return function innerFunction(guess) { | ||
| if(guesses <= amount) { | ||
| if(guesses === amount && guess !== answer) { | ||
| guesses++; | ||
| return "No more guesses the answer was " + answer; | ||
| } | ||
| if(guess === answer) { | ||
| guesses = amount + 1; | ||
| return "You got it!"; | ||
| } | ||
| if (guess < answer) { | ||
| guesses++; | ||
| return "You're too low!"; | ||
| } | ||
| if (guess > answer) { | ||
| guesses++; | ||
| return "You're too high!"; | ||
| } | ||
| } | ||
| else { | ||
| return "You are all done playing!"; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,135 @@ | ||
| window.addEventListener("load", function() { | ||
|
|
||
| function clear(ctx, width, heigt) { | ||
| ctx.clearRect(0, 0, width, height); | ||
| } | ||
|
|
||
| function drawRandomShape(ctx, width, height) { | ||
| function drawTraingle(ctx, width, height, color) { | ||
| ctx.fillStyle = color; | ||
| ctx.beginPath(); | ||
| ctx.moveTo(width,height); | ||
| ctx.lineTo(width+100, height+100); | ||
| ctx.lineTo(width, height+100); | ||
| ctx.fill(); | ||
| ctx.closePath(); | ||
| } | ||
|
|
||
| function drawGameStartText(ctx, width, height, score) { | ||
| function drawRandomShape(shapes) { | ||
| var shape = shapes[Math.floor(Math.random()*shapes.length)]; | ||
| var randWidth = Math.floor(Math.random() * (700 - 100)) + 100; | ||
| var randHeight = Math.floor(Math.random() * (650 - 100)) + 100; | ||
| var eKey; | ||
| switch(shape) { | ||
| case "redT": | ||
| clear(ctx, (canvas.width), (canvas.height)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are doing the clear in all cases, you don't need to put that code in the switch. Just do it before the switch statement. Also, you don't need the ( ) around cavas.width and canvas.height. |
||
| drawTraingle(ctx, randWidth, randHeight, "red"); | ||
| eKey = "redT"; | ||
| break; | ||
| case "redS": | ||
| clear(ctx, (canvas.width), (canvas.height)); | ||
| drawSquare(ctx, randWidth, randHeight, 100, 100, "red"); | ||
| eKey = "redS"; | ||
| break; | ||
| case "whiteT": | ||
| clear(ctx, (canvas.width), (canvas.height)); | ||
| drawTraingle(ctx, randWidth, randHeight, "white"); | ||
| eKey = "whiteT"; | ||
| break; | ||
| case "whiteS": | ||
| clear(ctx, (canvas.width), (canvas.height)); | ||
| drawSquare(ctx, randWidth, randHeight, 100, 100, "white"); | ||
| eKey = "whiteS"; | ||
| break; | ||
| default: | ||
| clear(ctx, (canvas.width), (canvas.height)); | ||
| drawTraingle(ctx, randWidth, randHeight, "red"); | ||
| eKey = "redT"; | ||
| } | ||
| return eKey; | ||
| } | ||
|
|
||
| function restartGame(ctx, width, height) { | ||
| function drawSquare(ctx, startx, starty, width, height, color) { | ||
| var square = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to make the whole object because you don't really save it and use it later.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the only code you really need here is: |
||
| corner: [startx,starty], | ||
| width: width, | ||
| height: height, | ||
| color: color, | ||
| draw: function() { | ||
| ctx.fillStyle = this.color; | ||
| ctx.fillRect(this.corner[0], this.corner[1], this.width, this.height); | ||
| } | ||
| } | ||
| square.draw(); | ||
| } | ||
|
|
||
| function drawGameStartText(ctx, width, height, score) { | ||
| ctx.fillStyle = "#FFFFFF"; | ||
| ctx.font = '30px san-serif'; | ||
| ctx.textBaseline = 'middle'; | ||
| ctx.textAlign = 'center'; | ||
| ctx.fillText('Press the space bar to start a new game.' , width, height); | ||
| ctx.fillText("Score: " + score, width, height + 30); | ||
| } | ||
|
|
||
| var canvas = document.getElementById("shapes-game"), | ||
| height = canvas.scrollHeight, | ||
| width = canvas.scrollWidth, | ||
| gameOn = false, | ||
| expectedKey = undefined, | ||
| ctx = canvas.getContext('2d'), | ||
| // white triangle = up, red square = down, | ||
| // red triangle = left, white square = right | ||
| expectedKeysMap = {white0: 38, red1: 40, red0: 37, white1: 39}, | ||
| timerSpan = document.getElementById("time-remaining"), | ||
| scoreSpan = document.getElementById("score-val"), | ||
| seconds = 3, | ||
| intervalId; | ||
|
|
||
| //. ########################## // | ||
| // Loading prep work | ||
| //. ########################## // | ||
| var canvas = document.getElementById("shapes-game"), | ||
| height = canvas.scrollHeight, | ||
| width = canvas.scrollWidth, | ||
| gameOn = false, | ||
| expectedKey = undefined, | ||
| shapes = ["whiteS", "whiteT", "redS", "redT"], | ||
| ctx = canvas.getContext('2d'), | ||
| // white triangle = up, red square = down, | ||
| // red triangle = left, white square = right | ||
| expectedKeysMap = {whiteT: 38, redS: 40, redT: 37, whiteS: 39}, | ||
| timerSpan = document.getElementById("time-remaining"), | ||
| scoreSpan = document.getElementById("score-val"), | ||
| seconds = 3, | ||
| cScore = 0, | ||
| intervalId; | ||
| canvas.width = width; | ||
| canvas.height = height; | ||
| drawGameStartText(ctx, (canvas.width/2), (canvas.height/2), 0); | ||
|
|
||
| document.addEventListener("keyup", function() { | ||
|
|
||
| }); | ||
| }); | ||
| //. ########################## // | ||
| // Key up activities | ||
| //. ########################## // | ||
| document.addEventListener("keyup", function(e) { | ||
| if (e.keyCode === 32) { | ||
| gameOn = true; | ||
| expectedKey = drawRandomShape(shapes); | ||
|
|
||
| // start timer | ||
| var time = parseInt(timerSpan.innerText); | ||
| intervalId = setInterval(function() { | ||
| timerSpan.innerText = --time; | ||
| if (time === 0) { | ||
| clearInterval(intervalId); | ||
| // reset gameOn, score and time to default values | ||
| gameOn = false; | ||
| scoreSpan.innerText = "0"; | ||
| timerSpan.innerText = "30"; | ||
| // display text | ||
| clear(ctx, (canvas.width), (canvas.height)); | ||
| drawGameStartText(ctx, (canvas.width/2), (canvas.height/2), cScore); | ||
| } | ||
| }, 1000); | ||
| } | ||
| else { // score computations | ||
| if(gameOn) { | ||
| cScore = parseInt(scoreSpan.innerText); | ||
| if (e.keyCode === expectedKeysMap[expectedKey]) | ||
| scoreSpan.innerText = ++cScore; | ||
| else { | ||
| if(cScore > 0) | ||
| scoreSpan.innerText = --cScore; | ||
| } | ||
|
|
||
| expectedKey = drawRandomShape(shapes); | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work because obj won't be defined yet. Here is a work around: