-
Notifications
You must be signed in to change notification settings - Fork 83
finished testing exercises in testing.js and testingSpec.js #33
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 all commits
e5a1a48
40942a9
4fdc44f
a9fc3ce
77f4ad1
72705c5
ba25185
6e328a2
9843fb0
8ba543e
27074b7
65cfc53
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,136 @@ | ||
| // Fix the following code: | ||
|
|
||
| var obj = { | ||
| fullName: "Harry Potter", | ||
| person: { | ||
| sayHi: function(){ | ||
| return "This person's name is " + this.fullName | ||
| } | ||
| } | ||
| } | ||
| obj.person.sayHi.call(obj) | ||
|
|
||
|
|
||
|
|
||
| var obj = { | ||
| fullName: "Harry Potter", | ||
| sayHi: function(){ | ||
| return "This person's name is " + this.fullName | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // Write a function called sumEvenArguments which takes all of the arguments passed to a function and returns the sum of the even ones. | ||
| // sumEvenArguments(1,2,3,4) // 6 | ||
| // sumEvenArguments(1,2,6) // 8 | ||
| // sumEvenArguments(1,2) // 2 | ||
|
|
||
| //I feel like there should be an easier way to do this with less code .... | ||
| function sumEvenArguments() { | ||
| var arr = [].slice.call(arguments) | ||
| var evens = [] | ||
| arr.forEach(function(element) { | ||
| if (element % 2===0) { | ||
| evens.push(element) | ||
|
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. instead of pushing all of the evens into a new array, you could just do the sum here. |
||
| } | ||
| }) | ||
|
|
||
| var sum = 0 | ||
| evens.forEach(function(element) { | ||
| sum = sum + element | ||
| }) | ||
| return sum | ||
| } | ||
|
|
||
|
|
||
| // Write a function called arrayFrom which converts an array-like-object into an array. | ||
| // function sample(){ | ||
| // var arr = arrayFrom(arguments) | ||
| // if(!arr.reduce) throw "This is not an array!" | ||
| // return arr.reduce(function(acc,next){ | ||
| // return acc+next; | ||
| // },0) | ||
| // } | ||
|
|
||
| function arrayFrom() { | ||
| var arr = [].slice.call(arguments) | ||
| return arr | ||
| } | ||
|
|
||
|
|
||
| // Write a function called invokeMax which accepts a function and a maximum amount. invokeMax should return a function that when called increments a counter. If the counter is greater than the maximum amount, the inner function should return "Maxed Out" | ||
|
|
||
| // function add(a,b){ | ||
| // return a+b | ||
| // } | ||
|
|
||
| // var addOnlyThreeTimes = invokeMax(add,3); | ||
| // addOnlyThreeTimes(1,2) // 3 | ||
| // addOnlyThreeTimes(2,2) // 4 | ||
| // addOnlyThreeTimes(1,2) // 3 | ||
| // addOnlyThreeTimes(1,2) // "Maxed Out!" | ||
|
|
||
| function invokeMax(fn, max) { | ||
| // var outerArgs = [].slice.call(arguments) | ||
| var counter = 0 | ||
|
|
||
| return function() { | ||
| if (counter === max) { | ||
| return "Maxed Out!" | ||
| } else { | ||
| counter++ | ||
| return fn.apply(this,arguments) //ES 5 | ||
| // return fn(...arguments) //ES2015 | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // 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. | ||
|
|
||
| // You will have to make use of closure to solve this problem. | ||
|
|
||
| // var game = guessingGame(5) | ||
| // 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!" | ||
|
|
||
| // var game2 = guessingGame(3) | ||
| // 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!" | ||
|
|
||
|
|
||
| function guessingGame(amount) { | ||
| var answer = (Math.floor(Math.random() *11)) | ||
| var guesses = 0 | ||
| return function() { | ||
| var guess = [].slice.call(arguments); | ||
| // var guess = args[0] | ||
| guesses++ | ||
| if (guesses > amount) { | ||
| return "No more guesses, the answer was " + answer | ||
| } else if (guess > answer) { | ||
| return "You're too high!" | ||
| } else if (guess < answer) { | ||
| return "You're too low!" | ||
| } else { | ||
| return "You got it!" | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,67 @@ | ||
| window.addEventListener("load", function() { | ||
|
|
||
| function clear(ctx, width, heigt) { | ||
|
|
||
| function clear(ctx, width, height) { | ||
| ctx.clearRect(0,0,canvas.width,canvas.height) | ||
| } | ||
|
|
||
| function createTriangle(x,y) { | ||
| ctx.beginPath(); | ||
| ctx.moveTo(x,y); | ||
| ctx.lineTo(x+100,y+100); | ||
| ctx.lineTo(x,y+100); | ||
| ctx.fill(); | ||
| ctx.closePath(); | ||
| } | ||
|
|
||
|
|
||
| function drawRandomShape(ctx, width, height) { | ||
| var shape = (Math.floor(Math.random() *4))+1 //number from 1-4 | ||
| var x = (Math.floor(Math.random() * 700))+1 //wont go out of bounds on x axis | ||
| var y = (Math.floor(Math.random() * 650))+1 //wont go out of bounds on y axis | ||
|
|
||
|
|
||
| if (shape === 1) { | ||
| ctx.fillStyle = "white" | ||
| createTriangle(x,y) | ||
| expectedKey = 38 | ||
|
|
||
| } else if (shape === 2) { | ||
| ctx.fillStyle = "red" | ||
| ctx.fillRect(x,y,100,100) | ||
| expectedKey = 40 | ||
|
|
||
| } else if (shape === 3) { | ||
| ctx.fillStyle = "red" | ||
| createTriangle(x,y) | ||
| expectedKey = 37 | ||
|
|
||
| } else if (shape === 4) { | ||
| ctx.fillStyle = "white" | ||
| ctx.fillRect(x,y,100,100) | ||
| expectedKey = 39 | ||
|
|
||
| } | ||
| } | ||
|
|
||
| function drawGameStartText(ctx, width, height, score) { | ||
| function drawGameStartText(ctx, width, height) { | ||
|
|
||
| ctx.fillStyle = "white" | ||
| ctx.font = "38px serif" | ||
| ctx.fillText("Press the space bar to start a new game",100, 350) | ||
| } | ||
|
|
||
| function restartGame(ctx, width, height, score) { | ||
|
|
||
| drawGameStartText(ctx,width,height) | ||
| ctx.font = "25px serif" | ||
| ctx.fillText("Score: " + score, 350,400) | ||
|
|
||
| } | ||
|
|
||
| function restartGame(ctx, width, height) { | ||
| function clearAndShow(ctx,width,height) { | ||
| clear(ctx,width,height) | ||
| drawRandomShape(ctx,width,height) | ||
| } | ||
|
|
||
| var canvas = document.getElementById("shapes-game"), | ||
|
|
@@ -23,14 +75,42 @@ 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, | ||
| seconds = 0, | ||
| intervalId; | ||
|
|
||
| canvas.width = width; | ||
| canvas.height = height; | ||
|
|
||
| document.addEventListener("keyup", function() { | ||
|
|
||
| drawGameStartText(ctx, width, height) | ||
|
|
||
| document.addEventListener("keyup", function(e) { | ||
| //I feel like this nested if statements can be condensed. | ||
| if (timerSpan.innerHTML > 29) { | ||
|
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. This should be a better way of tracking this. If I hit the space bar mutliple times before the time changes to 29, weird things might start happening. I would just keep a variable to tell if the game has started or not. I think I called it gameOn in my solution. |
||
| if (e.keyCode === 32) { | ||
| clearAndShow(ctx,width,height) // when space is first pressed, clear the words and show the first shape. | ||
| var decreaseTimerId = setInterval(function(){ | ||
| timerSpan.innerHTML --; // decrease timer span | ||
| seconds ++ // variable to check when 30 seconds has elapsed ... somehow I could not use timerSpan.innerHTML to check?? | ||
| if (seconds === 30) { | ||
| clearInterval(decreaseTimerId); | ||
| clear(ctx, width, height) | ||
| //show score on the screen and reset all the variables. | ||
| restartGame(ctx,width,height,scoreSpan.innerHTML) | ||
| seconds = 0; | ||
| timerSpan.innerHTML = 30 | ||
| scoreSpan.innerHTML = 0 | ||
| } | ||
| },1000) | ||
| } | ||
| //^ everything above checks if the space key is pressed and do some action based on a condition. | ||
| } else if (e.keyCode === expectedKey) { | ||
| scoreSpan.innerHTML ++ | ||
| } else { | ||
| scoreSpan.innerHTML -- | ||
| } | ||
|
|
||
| clearAndShow(ctx,width,height) | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
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.
You can also do: