Skip to content

Latest commit

 

History

History
192 lines (129 loc) · 6.76 KB

MAKEME.md

File metadata and controls

192 lines (129 loc) · 6.76 KB

Homework Week 3

Topics discussed this week:
• Closures
• Scope
• Callbacks

Here you find the readings you have to complete before the first JavaScript3 lecture.

Step 1: Read

Read:

Step 2: JavaScript

Deadline Wednesday

We learned a little bit about callbacks in JS. A callback is simply a function passed to another function that gets executed (run) after a potentially long running operation has completed. There is another function called setTimeout that will wait a specified period of time and then execute a function. For example:

function doIt() {
  console.log('I am done');
}
setTimeout(doIt, 5000);

If you run the above code it will wait 5 seconds and then print I am done. Please read something about setTimeout on MDN. The first argument to the setTimeout call is the callback (doIt).

2.1 We saw that we can pass functions as arguments to other functions. Your task is to write a function that takes another function as an argument and runs it.

function foo(func) {
  // What to do here?
}

function bar() {
  console.log('Hello, I am bar!');
}

foo(bar);

2.2 You must write a function that takes 4 arguments.

  • A start value
  • An end value
  • A callback to call if the number is divisible by 3
  • A callback to use if the number is divisible by 5

The function should first generate an array containing values from start value to end value (inclusive).

Then the function should take the newly created array and iterate over it, and calling the first callback if the array value is divisible by 3.

The function should call the second callback if the array value is divisible by 5.

Both functions should be called if the array value is divisible by both 3 and 5.

function threeFive(startIndex, stopIndex, threeCallback, fiveCallback) {
  const numbers = [];
  // make array
  // start at beginning of array and check if you should call threeCallback or fiveCallback or go on to next
}

threeFive(10, 15, sayThree, sayFive);

// Should create an array [10,11,12,13,14,15]
// and call sayFive, sayThree, sayThree, sayFive
// please make sure you see why these calls are made before you start coding

Note: The following assignments include some problems from freeCodeCamp. Note that some freeCodeCamp examples still mention var. However you can safely replace them with let and const as appropriate.

2.3 Please solve this problem:

Basic Algorithm Scripting: Repeat a String Repeat a String

2.3.1: with a for loop.
2.3.2: with a while loop.
2.3.3: with a do...while loop.

2.4 Some practice with objects:

Object Oriented Programming: Define a Constructor Function
> Object Oriented Programming: Use a Constructor to Create Objects

2.5 Nested loops

Basic JavaScript: Nesting For Loops

2.6 We did some work with arrays:

const arr = [1, 2, 3];

We can also nest arrays inside arrays like this:

const arr2d = [[1, 2], [3, 4], [5, 6]];

(for math people you can think of this as a matrix)

How would you flatten out all the items of an array with 2 dimensions into a one-dimensional array? Flattening out the arr2d array above would result in:

const flattenedArr = [1, 2, 3, 4, 5, 6];

How about 3 dimensions? How about with K dimensions? What if you didn't know how deep the array was nested? (You don't have to write code for this but think about it.)

2.7 Here are two functions that look like they do the something similar but they print different results. Please explain what's going on here.

const x = 9;
function f1(val) {
  val = val + 1;
  return val;
}
f1(x);
console.log(x);

const y = { x: 9 };
function f2(val) {
  val.x = val.x + 1;
  return val;
}
f2(y);
console.log(y);

If you are confused please run the code and then consult the Google for "javascript pass by value pass by reference"

Step 3: Scope and Closures

Deadline Saturday

Let's continue to learn a little more about scope and Closures.

Write a function that would allow you to do this:

const addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27

Bonus: Write a function takes this array ['a', 'b', 'c', 'd', 'a', 'e', 'f', 'c'] and returns an array which only has unique values in it (so it removes the duplicate ones). Make it a 'smart' algorithm that could do it for every array (only strings/number). Try to make it as fast as possible!

Step 4: Run the unit tests

To run the unit test for the week 3 homework, open a terminal window in the JavaScript2 folder and type

npm run test-week3

In case of errors, try and fix them. When done, run the tests again.

Repeat the previous step until all tests pass.

Step 5: Read before next lecture

Deadline Sunday morning

Go trough the reading material in the README.md to prepare for your next class

How to hand in your homework:

Go over your homework one last time:

  • Does your homework pass all the unit tests?
  • Does every file start with 'use strict';?
  • Have you used const and let and avoided var?
  • Do the variable, function and argument names you created follow the Naming Conventions?
  • Have you resolved all issues flagged by ESLint and the spell checker (no wavy red and green underlines in VSCode)?

If the answer is 'yes' to all preceding questions you are ready to follow these instructions: