Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active November 18, 2018 12:51
Show Gist options
  • Save harrisonmalone/c045915155ee013592166ed64b55b25e to your computer and use it in GitHub Desktop.
Save harrisonmalone/c045915155ee013592166ed64b55b25e to your computer and use it in GitHub Desktop.
js drills that matt posted in first fasttrack batch
// 1. Define an object that has two keys. One will have the value of a string, and the other a function (and this function can simply log its own name to screen).
const obj = {
key1: "string",
key2: printName = () => { return "this is the function printName" }
}
// 2. Log the string to screen.
console.log(obj.key1)
// 3. Log the entire function to screen.
console.log(obj.key2)
// 4. Invoke the function within the object.
console.log(obj.key2())
// ------------
// 5. Define an object that has three keys, and each key has the value of an anonymous function. In each function log something to screen (different in each function).
const obj2 = {
key1: function() {
console.log("hello")
},
key2: function() {
console.log("ni hao")
},
key3: function() {
console.log("ola")
}
}
// 6. Call each function (through the object).
obj2.key1()
obj2.key2()
obj2.key3()
// ------------
// 7. Make a function that returns an object that has two keys, one key with a string value, and the other that has an anonymous function as its value.
function makeObj() {
const obj3 = {
key1: "string 2",
key2: function() {
return "this is an anonymous function"
}
}
return obj3
}
// 8. Log to screen the result of invoking this function.
console.log(makeObj())
// 9. Work out how to log to screen the value of the returned object’s string.
const objfunc = makeObj()
console.log(objfunc.key1)
// 10. Invoke the returned object’s function (via the first function - you will need to return the object, and then call the function within the returned object).
console.log(makeObj().key2())
// ------------
// 11. Write a function that takes a number and a function as an argument. Inside this function perform three simple calculations with the given number (the first argument), and then pass the final result to the function that is passed as the second argument.
function simpleCalc(num, func) {
num = num + 2
num = num * 2
const result = num / 2
return func(result)
}
// 12. Call this function three times with different numbers, and a different anonymous callback function each time (perhaps each time the callback doing a different simple calculation).
const res1 = simpleCalc(2, function(result) {
return result * 2
})
const res2 = simpleCalc(3, function(result) {
return result + 3
})
const res3 = simpleCalc(4, function(result) {
return result - 4
})
// 13. Now write two functions that take a number as an argument, and do some very simple calculation.
function func1(num) {
return num + 1
}
function func2(num) {
return num - 1
}
// 14. Invoke the function from 11 twice, each time with a different number argument, and using each of the two functions that you just defined in 13 (one in each invocation).
const res4 = simpleCalc(2, func1)
const res5 = simpleCalc(1, func2)
// ------------
// 15. Define three very basic functions that take no arguments and that simply console out the name of the function.
const function1 = function() {
console.log("this is function 1")
}
const function2 = function() {
console.log("this is function 2")
}
const function3 = function() {
console.log("this is function 3")
}
// 16. Place each of these functions into an array (do not call the functions).
const arr = [function1, function2, function3]
// 17. Loop through this array and invoke each of the functions in turn.
arr.forEach(function(element) {
console.log(element())
})
// ------------
// 18. Define a function (funcReturnsFunc) that returns an anonymous function. If you like you can have funcReturnsFunc log out something before it returns the function. The returned function can just log out a simple message to inform you it ran.
function funcReturnsFunc() {
console.log("about to enter anonymous function")
return function() {
console.log("anonymous function has run")
}
}
// 19. Invoke funcReturnsFunc, and store the result in a const.
const funcWithinFunc = funcReturnsFunc()
// debugger;
// 20. Using that const, run the returned function.
funcWithinFunc()
// 21. Now do the same thing in one line: run funcReturnsFunc, and then run the anonymous function result immediately.
funcReturnsFunc()()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment