Coder Social home page Coder Social logo

functions-and-scope-worksheet's Introduction

Variables, Functions, and Scope in JS

Writing functions in JavaScript

  1. Write a function named hiWorld that prints 'hello world' to the console
  1. Write the same function from above in 2 other ways using different syntax
  1. Write a function that accepts a 'name' parameter and prints "Hi, my name is <name>" to the console
  1. Write a funtion that accepts 2 numbers as parameters and returns their sum
  1. Write a function that accepts a number as a parameter returns the double of it
  1. Write a function called logger that accepts a string as a parameter and then passes that string to console.log. Use logger as a callback to console.log each element in the array below.
let stringArray = ["JavaScript", "is pretty", "cool", "I guess."]

// put answer here
  1. Write a function that accepts a number as a parameter and prints "โŒ You're too young to enter the clurb! โŒ" if the parameter is less than 21 and "๐Ÿคก Welcome to the clurb! ๐Ÿš€" if the parameter is 21 or over
  1. Modify the above function to allow an underage patron in if they have a fake id

Scope in JavaScript

  1. What is lexical scope?
  1. What would be printed to the console in the example below? Why?
let carType = "Honda Civic"

function myCar(carType){
  console.log(`I drive a fancy ass ${carType}!`)
}

carType('Tesla X')
  1. What would be printed to the console in the example below? Why?
let carType = "Honda Civic"

function myCar(carType){
  console.log(`I drive a fancy ass ${carType}!`)
}

carType('Tesla X')
  1. Write out what would be printed to the console, in order, if the script below were run in a browser.
console.log(person) 

let person = "Lady Gaga"

function someFunction(){
  console.log(person)

  function otherFunction(){
    let otherPerson = "Madonna"

    console.log(person)
    console.log(otherPerson)
  }

  otherFunction()

  console.log(person)
  console.log(otherPerson)
}

someFunction()

console.log(otherPerson)
  1. What would be printed to the console in the example below? Why?
if(true){
  let dogName = "Neikko"
  console.log(`My dog's name is ${dogName}`)
}

console.log(`My dog's name is ${dogName}`)

Hoisting

  1. In your own words, describe hoisting?

  2. If I had a JavaScript file with the following code, what would happen in each of the function calls below? Why?

bark()
meow()

function bark(){
  console.log("woof woof")
}

let meow = function(){
  console.log("meeeeooooowwr")
}
  1. What will the console.log print in each of the examples below? Why?
console.log(dogName)

var dogName = "Perky"
console.log(catName)

const catName = "Houdini"
  1. What will the console.log print in the example below? Why?
horse = "Benny"
console.log(horse)
var horse
  1. With regard to hoisting, what's the difference between let, var, and const

Variable declaration

  1. What are the differences between declaring variables using let, var, and const?

First Class Functions

  1. Write a function that accepts a number (e.g. x) as a parameter and returns an inner function that accepts a different number (e.g. y) as a parameter and returns the product of it and the number from the outer function.
  1. Using the function from above, create a function that accepts a number as a parameter and returns its double.
  1. If you successfully got the question above working, explain how you utilized closures to do so?
  1. What would get printed to the console in the example below?
let steven = {
  name: "Steven",
  goForRun: function(distance){
    console.log(`Today I ran ${distance} km.`)
  }
}

steven.goForRun // what would happen here?

steven.goForRun()

functions-and-scope-worksheet's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.