title | type | competencies | creator | ||||
---|---|---|---|---|---|---|---|
JS Functions |
homework |
JS basics |
|
Javascript functions are essential in developing functionality (who knew?) in our programs, but sometimes there's some funky behavior, and it doesn't do what we might expect it to do, especially when it comes to variable scope. In tonight's assignment, you will start by exploring local and global scope in Javascript and see how being able to access variables depends on the scope. You'll round off your knowledge by writing some functions and use Mocha tests to check your work.
The general guideline with variable scope is:
- Any variable declared outside a function belongs to the global scope.
- Global variables are accessible anywhere in your code.
- Each function has its own scope.
- Variables declared in those functions are local, meaning it's accessible only inside that function and any nested functions.
- The newest version of Javascript (ES6) offers block scoping which we won't be using here.
- Gain deeper insight to local and global scope.
- Practice writing functions that take 1 - 2 arguments.
- Use tests to test your work.
cd
into today's homework directory, and run npm install
in terminal -- this will download any assignment-specific JavaScript dependencies into to a directory called node_modules
.
There are automated tests built into this assignment so that you can check your work. You can run these tests by running npm test
in terminal in the homework
directory. Ideally, you should run the tests after each problem. Only the Questions 6 - 10 have tests provided for you.
- Write your answers in
script.js
. - For Questions 1 - 5:
- fill in the blank with either
local
orglobal
orout of scope
. - These are (super) short answer questions.
- Feel free to run the code in
script.js
or in a repl.it session, or in your browser console.
- fill in the blank with either
- For Questions 6 - 10:
- don't modify the variable names, since the tests use them.
- run
npm test
in a separate terminal tab after each problem and get them all to pass.
- The variable
greeting
is______
to thesayGreeting
function. - Can the function access
greeting
? - What happens when you invoke
sayGreeting
? - 🎯 Commit -m "Question 1 done"
var sayGreeting = function() {
var greeting = "Aloha!";
console.log(greeting);
}
- The variable
number
is_______
to theaddition
function. - Can the function access
number
? - What happens when you invoke
addition
? - 🎯 Commit -m "Question 2 done"
var number = 5;
var addition = function() {
console.log(number)
}
- The variable
vegetable
is_______
to thefirstFunction
function. - The variable
vegetable
is_______
to thesecond Function
function. - Can the
secondFunction
accessvegetable
? - What happens when you invoke
firstFunction
? - What happens when you invoke
secondFunction
? - 🎯 Commit -m "Question 3 done"
var firstFunction = function() {
var vegetable = "snap pea";
console.log(vegetable);
}
var secondFunction = function() {
console.log(vegetable);
}
- The variable
superhero
is_______
to thefightSuperman
function. - The variable
superhero
is_______
to thetheRealHero
function. - What happens when you invoke
fightSuperman
? - What happens when you invoke
theRealHero
? - Does
theRealHero
have access tosuperhero
insidefightSuperman
? - 🎯 Commit -m "Question 4 done"
var superhero = "Batman";
var fightSuperman = function() {
var superhero = "Superman";
console.log(superhero);
}
var theRealHero = function() {
console.log(superhero);
}
- The variable
beep
is in_______
scope. - What happens when you execute the code below?
- Is
makeNoise
a function declaration or function expression? - Change the syntax for
makeNoise
so that it's a function expression. What happens when you execute the code? - What happens if you move
makeNoise()
after the function expression? - What happens if you
console.log(beep)
below this revised code? - Go back to the original code. What happens if you throw a
console.log(beep)
after the function? How is this possible? (This is why we'll be mostly sticking to function expressions.) - 🎯 Commit -m "Question 5 done"
makeNoise();
function makeNoise(){
beep = "beep";
console.log(beep);
}
Build upon the sumOfNums
function so that:
- it takes an array of numbers
- it outputs the sum of numbers that were passed in
- Edge case: if the array is empty, it returns 0
- 🎯 Commit -m "Question 6 test passed"
Build upon the numsGreaterThanTen
function so that:
- it takes an array of numbers
- it outputs an array of the numbers from the first array that are strictly greater (i.e. greater than but not equal to) than 10
- 🎯 Commit -m "Question 7 test passed"
Build upon the allStartingWithA
function so that:
- it takes an array of words
- it outputs true if ALL words start with the letter 'a' (case-insensitive), and
false
otherwise. - Edge Case: If the array is empty, the function should return
true
. - 🎯 Commit -m "Question 8 test passed"
Build upon the hasAtLeastNVowels
function so that:
- it takes 2 parameters: a single word, and a number (
n
) - it outputs
true
if the word has at least some number (n
) of vowels,false
otherwise - Assume that vowels are 'a', 'e', 'i', 'o', and 'u' (NOT 'y')
- Edge Case: If
n
is less than zero, returnnull
. - 🎯 Commit -m "Question 9 test passed"
Build upon the buildObjectFromWords
function so that:
- it takes an array of words
- it outputs an object
{}
where each word in the array is a key, and the value tied to that key is the length of the word - e.g. given
['cat', 'horse', 'elephant']
, return{ cat: 3, horse: 5, elephant: 8}
- 🎯 Commit -m "Question 10 test passed"
We'll be writing a ton more functions during the course, and it never hurts to practice more and feel super cozy about functions. One really great resource we'll be using in class is Code Wars, a great training grounds for coding challenges for basically every programming language!
- Sign up on Code Wars
- For "Programming experience", select
Learning to Program
- For "Choose a language", select
JavaScript
Try your hand at one (or all) of these challenges:
When you're ready, create an issue on the class repo with a title in the format "First and Last Name -- Week XX Day XX". The issue body should have:
- A link to your forked repo (ie. to your
homework
folder) - A 'comfort' score on how you feel about your answers, from 1 (very uncomfortable) to 5 (very comfortable)
- A 'completeness' score, from 1 (didn't do it) to 5 (finished all of it)
- A 'win'
- A 'struggle'
- A 'comment'
- A screen shot of your tests.