Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active June 13, 2019 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harrisonmalone/462781c17280c656f5c056f4694cfc9d to your computer and use it in GitHub Desktop.
Save harrisonmalone/462781c17280c656f5c056f4694cfc9d to your computer and use it in GitHub Desktop.
// 1. Create an object that has three properties. One property is called ‘count’ and will be set to a number, the second will be set to an array, and the third to a function. This function should simply console.log each of the values of the second property on a new line.
const myObj = {
count: 2,
myArr: [1, 2, 3],
myFunc: function() {
this.myArr.forEach((num) => {
console.log(num)
})
}
}
myObj.myFunc()
// 2. What is NPM? In your brief explanation refer to package.json.
// Node package manager. It allows you to install and use code other developers have written in our own projects. The package.json file lists all the packages we have installed for our project.
// 3. What are Event Listeners? Gives some examples in your response.
// Event listeners are a set of methods that exist in browser javascript. These methods are waiting for browser events to occur (like a click) and when these events occur we can execute some javascript.
// An example
// button.addEventListener('click', () => {
// console.log('button clicked')
// })
// 4. Define a function called numMult that takes two number arguments and a callback. The function will multiply the two numbers and pass the result to the callback as an argument. Now call numMult and in the callback simply console.log out the argument that has been passed through.
function numMult(num1, num2, cb) {
const result = num1 * num2
cb(result)
}
numMult(2, 2, function(result) {
console.log(result)
})
// 5. Define a function called addNum with that takes two number arguments. In this function simply return the addition of these numbers. Write a second function called numsPlusFunct that takes three arguments, two numbers and a function. Inside numsPlusFunct call the function that is passed as an argument, and pass the two number arguments to this function. numsPlusFunct will return an object where the first property has the value returned from that function call, and the second property is a string. You have now made two functions. Call the numsPlusFunct and pass addNum as the appropriate argument.
function addNum(num1, num2) {
return num1 + num2
}
function numsPlusFunc(num1, num2, cb) {
const result = cb(num1, num2)
const obj = {
theReturnOfAddNum: result,
myString: 'hello'
}
return obj
}
const res = numsPlusFunc(2, 2, addNum)
console.log(res)
// 6. What is Express? What does it help us to do? Often we use ‘req’ and ‘res’ in our Express/Node code. Very briefly outline what these are.
// It's a node package that allows us to make a web server in javascript.
// req is the request object, it's the http request data that's being sent from the client
// res is the response object, this has functions like (.send, .json and .redirect) that determine what we send back to the client
// 7. What is the difference between synchronous and asynchronous code? Name some ways that JS deals with the issue of asynchronous code.
// synchronous code runs line by line, one process at a time
// asynchronous code can have multiple processes running at one time, this means that our code isn't line by line, whilst one asynchronous process takes place some other code in our program can be executed
// 8. What is fetch and how does it relate to AJAX? Give an example of how you would use it. What does fetch return? Give a very basic example of fetch.
// fetch allows us to make http requests in javascript
// ajax is a set of techniques that exist in javascript that allow us to do asynchronous programming
// fetch is an ajax technique, that's how they relate, it allows us to make http requests asynchronously
// const pokemon = fetch('https://pokeapi.co/api/v2/pokemon')
// fetch returns a promise
// 9. A JS object looks like this: const southernField = { location: “upper”, crop: “sorghum”, watered: false }. Use destructuring to store the value of watered in a variable.
const southernField = { location: "upper", crop: "sorghum", watered: false }
const { watered } = southernField
console.log(watered)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment