-
Notifications
You must be signed in to change notification settings - Fork 82
Testing Exercises Solutions #40
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 14 commits
923a9d1
843a52f
739b83c
0ae4f4b
8e1cb79
ef4d6ae
fb02ddd
e140957
6736fb7
e9b6eaf
a904de5
2a5ed0e
2b06163
43b5d50
5121ed4
44653fe
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,117 @@ | ||
| //1. Fix the following code: | ||
|
|
||
| var obj = { | ||
| fullName: "Harry Potter", | ||
| person: { | ||
| sayHi: function(){ | ||
| return "This person's name is " + this.fullName | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //Fix | ||
|
|
||
| var obj = { | ||
| fullName: "Harry Potter", | ||
| sayHi: function(){ | ||
| return "This person's name is " + this.fullName | ||
| } | ||
| } | ||
|
|
||
| obj.sayHi(); | ||
|
|
||
| //2. Two examples of array-like-objects are arguments and document.querySelectorAll() | ||
|
|
||
|
|
||
|
|
||
| //3. Write a function called sumEvenArguments which takes all of the arguments passed to a function and returns the sum of the even ones. | ||
|
|
||
| function sumEvenArguments(){ | ||
| return [].slice.call(arguments).reduce(function(acc,next){ | ||
|
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 like the use of reduce! |
||
| if(next % 2 === 0){ | ||
| acc += next; | ||
| } | ||
| return acc; | ||
| },0) | ||
| } | ||
|
|
||
| /*sumEvenArguments(1,2,3,4) // 6 | ||
| sumEvenArguments(1,2,6) // 8 | ||
| sumEvenArguments(1,2) // 2*/ | ||
|
|
||
|
|
||
| //4. 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(){ | ||
| return [].slice.call(arguments); | ||
| } | ||
|
|
||
| //5. 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 counter = 0; | ||
| return function(){ | ||
| counter++; | ||
| if(counter > max){ | ||
| return "Maxed Out!"; | ||
| } | ||
| return fn.apply(this,arguments); | ||
| } | ||
| } | ||
|
|
||
| //6. 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(guess){ | ||
| guesses++; | ||
| if(guesses > amount){ | ||
| return "You are all done playing!"; | ||
| } else{ | ||
| if(guess === answer){ | ||
| return "You got it!"; | ||
| } else if(guess > answer){ | ||
| return "You're too high!"; | ||
| } else{ | ||
| return "You're too low!"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,23 +11,42 @@ var person = { | |
| }.bind(this),1000) | ||
| } | ||
| } | ||
|
|
||
| var person = { | ||
|
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 can be simplified to: |
||
| fullName: "Harry Potter", | ||
| sayHi(){ | ||
| setTimeout(() => | ||
| console.log(`Your name is ${this.fullName}`) | ||
| ,1000) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```javascript | ||
| var name = "Josie" | ||
| console.log("When " + name + " comes home, so good") | ||
|
|
||
| var name = "Josie" | ||
| console.log(`When ${name} comes home, so good`) | ||
| ``` | ||
|
|
||
| ```javascript | ||
| var DO_NOT_CHANGE = 42; | ||
| DO_NOT_CHANGE = 50; // stop me from doing this! | ||
|
|
||
| const DO_NOT_CHANGE = 42; | ||
| DO_NOT_CHANGE = 50; // Uncaught TypeError: Assignment to constant variable. | ||
| ``` | ||
|
|
||
| ```javascript | ||
| var arr = [1,2] | ||
| var temp = arr[0] | ||
| arr[0] = arr[1] | ||
| arr[1] = temp | ||
|
|
||
| var arr = [1,2] | ||
| var [temp,temp2] = arr | ||
| [temp,temp2] = [temp2,temp] | ||
|
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. |
||
| ``` | ||
|
|
||
| ```javascript | ||
|
|
@@ -36,6 +55,8 @@ function double(arr){ | |
| return val*2 | ||
| }); | ||
| } | ||
|
|
||
| let double = arr => arr.map(val => val*2) | ||
| ``` | ||
|
|
||
| ```javascript | ||
|
|
@@ -48,6 +69,9 @@ var obj = { | |
|
|
||
| var a = obj.numbers.a; | ||
| var b = obj.numbers.b; | ||
|
|
||
|
|
||
| var {a,b} = obj.numbers; | ||
| ``` | ||
|
|
||
| ```javascript | ||
|
|
@@ -62,14 +86,16 @@ function add(a,b){ | |
| } | ||
| return a+b | ||
| } | ||
|
|
||
| let add = (a=10, b=10) => a+b; | ||
| ``` | ||
|
|
||
| Research the following functions - what do they do? | ||
|
|
||
| `Array.from` - | ||
| `Array.from` - creates a new Array instance from an array-like or iterable objects, such as arguments | ||
|
|
||
| `Object.assign` - | ||
| `Object.assign` - creates a shallow copy of an object by copying the vaules of all enumerable and own properties from one or more source objects to a target object and returning the target object | ||
|
|
||
| `Array.includes` - | ||
| `Array.includes` - determines whether an array includes a certain element, returning true if found and false if not found; if we need the index of the element, need to use indexOf | ||
|
|
||
| `String.startsWith` - | ||
| `String.startsWith` - determines whether a string begins with the characters of a specified string, returning true if the characters are found and false if not found | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>Hack or Snooze</title> | ||
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | ||
| <link rel="stylesheet" href="styles.css"> | ||
| </head> | ||
| <body> | ||
| <div class="container"> | ||
| <div class="row"> | ||
| <div class="col-xs-10 col-xs-offset-1 "> | ||
| <header> | ||
| <span><b>Hack or Snooze</b></span> | ||
| <span class="links"> | ||
| <span id="submit">submit</span> | <span id="favorites">favorites</span> | ||
| </span> | ||
| </header> | ||
| <section id="add-story" class="hide"> | ||
| <div class="row"> | ||
| <div class="col-xs-10"> | ||
| <form class="form"> | ||
| <div class="form-group"> | ||
| <label for="title">title</label> | ||
| <input type="text" class="form-control" id="title" placeholder="title"> | ||
| </div> | ||
| <div class="form-group"> | ||
| <label for="title">url</label> | ||
| <input type="url" class="form-control" id="url" placeholder="http://"> | ||
| </div> | ||
| <div class="form-group"> | ||
| <button type="submit" class="btn btn-default">Submit</button> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </section> | ||
| <section id="story-list"> | ||
| <ol> | ||
| <li><span class="glyphicon glyphicon-star-empty"></span><a href="http://visualgo.net" target="_blank">Visualizing Algorithms </a> | ||
| <span class = "small">(visualgo.net)</span> | ||
| </li> | ||
| <li><span class="glyphicon glyphicon-star-empty"></span><a href="http://www.rithmschool.com" target="_blank">Problem Solving with Rithm </a> | ||
| <span class = "small">(rithmschool.com)</span> | ||
| </li> | ||
| </ol> | ||
| </section> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <script | ||
| src="http://code.jquery.com/jquery-3.2.1.min.js" | ||
| integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" | ||
| crossorigin="anonymous"></script> | ||
| <script src="script.js"></script> | ||
| </body> | ||
| </html> |
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.
One way to fix it is: