Skip to content

Paper problems + JS problems #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7c83e41
Completed problems for topics: 'HOF', '__proto__', 'exceptions' & 're…
anoukmontreuil Jan 12, 2018
53161ef
Problems solved for topics: 'HOF', '__proto__', 'exceptions' & 'retur…
anoukmontreuil Jan 13, 2018
fac1b4b
Update problem1.js
anoukmontreuil Jan 15, 2018
9d5f3de
Add files via upload
anoukmontreuil Jan 15, 2018
e308fcb
Add files via upload
anoukmontreuil Jan 15, 2018
07d0742
Add files via upload
anoukmontreuil Jan 15, 2018
9f18352
Add files via upload
anoukmontreuil Jan 16, 2018
93ccdbf
Add files via upload
anoukmontreuil Jan 16, 2018
ab0798b
Add files via upload
anoukmontreuil Jan 16, 2018
4b8f2bc
Completed paper problem: 'Arrow functions'
anoukmontreuil Jan 17, 2018
c8a27c3
Completed paper problems: 'Constructor functions'
anoukmontreuil Jan 17, 2018
ae92af8
Completed paper problems: 'Variable scoping'
anoukmontreuil Jan 17, 2018
1d986b7
Completed JS files: 'Arrow Functions'
anoukmontreuil Jan 17, 2018
6c42e8e
Completed JS file: 'Constructor Functions'
anoukmontreuil Jan 17, 2018
9f9f7c0
Completed JS files: 'Variable Scoping'
anoukmontreuil Jan 17, 2018
993a3d8
Add files via upload
anoukmontreuil Jan 17, 2018
d22a836
Add files via upload
anoukmontreuil Jan 17, 2018
74ef0b1
Add files via upload
anoukmontreuil Jan 17, 2018
e6c9498
Add files via upload
anoukmontreuil Jan 17, 2018
39d0025
Add files via upload
anoukmontreuil Jan 17, 2018
3fc0061
Added completed paper problems & JS files
anoukmontreuil Jan 17, 2018
4ab1be2
Overwriting constructor/problem5.txt
anoukmontreuil Jan 17, 2018
6e2d8e2
Merge branch 'master' into master
anoukmontreuil Jan 17, 2018
483c205
Completed JS exercise for: 'array functions'
anoukmontreuil Jan 18, 2018
dfe844d
Completed JS exercises for: 'conditional operators'
anoukmontreuil Jan 18, 2018
6531855
Completed paper problems for: 'array functions'
anoukmontreuil Jan 18, 2018
1be40e3
Completed paper problems for: 'conditional operators'
anoukmontreuil Jan 18, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions paperproblems/HOF/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ Someone has given you a function called filter
You don't know what it does, but there are clues at the bottom of this file
Use these clues to describe what filter is.
Specifically:
- How many parameters does it have
- What are the types of the parameters
- What does it do?

****************************************< ANSWER >**********************************************

- How many parameters does it have?
* 2

- What are the types of the parameters?
* 1st argument is an array (...technically an 'object', but whatever. :D)
* 2nd argument is a function;

- What does it do?
1) First, we see that in all cases: it returns an array.
2) It iterates through each element in the array passed as the first argument, and
3) checks, via the function passed as the second argument, whether the current element
evaluates to true or false in the conditional statements (if/else) executed within
the passed function...
a) If the expression evaluates to true: filter pushes the current element
in the resulting array (presented in step 1);
b) If the expression evaluates to false: the element is not pushed to the resulting array.
4) The result, therefore, is an array of elements that passed the conditional tests as true.

***************************************</ ANSWER >**********************************************

var evenNumbers = [2, 4, 6, 8, 10];
var oddNumbers = [3, 5, 7, 9];
Expand Down
22 changes: 20 additions & 2 deletions paperproblems/HOF/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@ Someone has given you a function called map
You don't know what it does, but there are clues at the bottom of this file
Use these clues to describe what map is.
Specifically:
- How many parameters does it have
- What are the types of the parameters

****************************************< ANSWER >**********************************************

- How many parameters does it have?
* 2

- What are the types of the parameters?
* 1st argument is an array (...technically an 'object', but whatever. :D)
* 2nd argument is a function;

- What does it do?
1) First, we see that in all cases: it returns an array.
2) It iterates through each element in the array passed as the first argument, and
3) performs, via the function passed as the second argument, an operation on the
current element, then pushes its result (the returned value) to the resulting
array (presented in step 1).
4) The result, therefore, is an array of new elements that have been generated
based on operations performed on the array's elements (within arg1) in the passed
function (arg2).

***************************************< /ANSWER >**********************************************

var someNumbers = [1, 2, 3, 4];
var someStrings = ["bob", "ERIC"];
Expand Down
17 changes: 16 additions & 1 deletion paperproblems/HOF/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,19 @@ function f(g) {
return g(3,5);
}

console.log(f(k));
console.log(f(k));

******************************************< ANSWER >********************************************

This will return an error, because the 2 arguments of k are never actually specified anywhere
(and certainly not by g...!)

Specifically:
k would *not* take the result of g as parameters to be passed down to itself;
rather, function f calls function k in an attempt to evaluate parameters it could pass into/as
its argument g. However, function k is being called without any arguments -- and its arguments
are necessary to evaluate the returned value.
This program won't be able to add or multiply numbers to undefined arguments.
I expect this will output either a type error or NaN (honestly, I'm not sure which).

*****************************************</ ANSWER >********************************************
16 changes: 15 additions & 1 deletion paperproblems/HOF/problem4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ function f(g, h) {
return g(h, 1, 4);
}

console.log(f(k, m));
console.log(f(k, m));

******************************************< ANSWER >********************************************

This will return an error, because the 3 arguments of k and the argument of m are never actually specified anywhere.

Specifically:
f would *not* take the result of k as parameters to be passed down to itself;
rather, function f calls function k in an attempt to evaluate parameters it could pass into/as its argument g. However, function k is being called without any arguments -- and its arguments are necessary to evaluate the returned value.
Moreover, nothing is passed as an argument for m (as x). Double whammy.

This program won't be able to add or multiply numbers to undefined arguments.
I expect this will output either a type error or NaN (honestly, I'm not sure which).

*****************************************</ ANSWER >********************************************
29 changes: 25 additions & 4 deletions paperproblems/__proto__/problem1.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
Draw the object diagram for this program
Draw the object diagram for this program

var x1 = {};
var x2 = {};
var x3 = {};
var x4 = {};
x2.__proto__ = x1;
x3.__proto__ = x2;
x4.__proto__ = x3;
x2.__proto__ = x1; // Impossible, would result in type error (cannot overwrite proto as a whole)
x3.__proto__ = x2; // Impossible, would result in type error (cannot overwrite proto as a whole)
x4.__proto__ = x3; // Impossible, would result in type error (cannot overwrite proto as a whole)

****************************************< ANSWER >**********************************************

╔════╤═══╗
║ │ ║ ┌────┬───┐
║ x1 │ ■───┤ {} │ ■────┐
║ │ ║ └────┴───┘ │
╟────┼───╢ │
║ │ ║ ┌────┬───┐ │
║ x2 │ ■───┤ {} │ ■────┤
║ │ ║ └────┴───┘ │ ┌───────────┬─────┐
╟────┼───╢ ├─►│ __proto__ │ ... │
║ │ ║ ┌────┬───┐ │ └───────────┴─────┘
║ x3 │ ■───┤ {} │ ■────┤
║ │ ║ └────┴───┘ │
╟────┼───╢ │
║ │ ║ ┌────┬───┐ │
║ x4 │ ■───┤ {} │ ■────┘
║ │ ║ └────┴───┘
╚════╧═══╝

***************************************</ ANSWER >**********************************************
22 changes: 18 additions & 4 deletions paperproblems/__proto__/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ var x1 = {};
var x2 = {};
var x3 = {};
var x4 = {};
x2.__proto__ = x1;
x3.__proto__ = x2;
x4.__proto__ = x3;
x2.__proto__ = x1; // Impossible, would result in type error (cannot overwrite proto as a whole)
x3.__proto__ = x2; // Impossible, would result in type error (cannot overwrite proto as a whole)
x4.__proto__ = x3; // Impossible, would result in type error (cannot overwrite proto as a whole)

// Therefore...

x1.age = 20;
x3.age = 18;

console.log(x1.age);
console.log(x2.age);
console.log(x3.age);
console.log(x4.age);
console.log(x4.age);

*************************************************< ANSWER >***************************************************

Technically, if you didn't remove the last three lines modifying __proto__ : you would get a type error.
...Once that was cleaned up; this is what we'd get:

1st console.log: 20
2nd console.log: 18 // None defined in object; last assigned key-value pair 'age' for __proto__ takes over.
3rd console.log: 18
4th console.log: 18 // None defined in object; last assigned key-value pair 'age' for __proto__ takes over.

*************************************************</ ANSWER >**************************************************
10 changes: 9 additions & 1 deletion paperproblems/__proto__/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ You get the following exception:


a) Explain what this exception means.
b) Why does this exception exist? What's wrong with a cyclic __proto__ value?
* A __proto__ object would be generated and destroyed (overriden by an empty object,
which itself points to a __proto__ object...) over and over again, indefintely.
It is an endless loop and therefore cyclic.
Ergo: a creepy, ouroboric, self-cannibalistic mess that must be avoided at all costs. :)

b) Why does this exception exist? What's wrong with a cyclic __proto__ value?
* This happens because we're trying to override the entirety of the __proto__ object,
which is a feature (implicit object) required to allow for Object-Oriented Programming in JavaScript.
An override would result in an infinite loop; because objects point to the __proto__ object when instantiated...
23 changes: 15 additions & 8 deletions paperproblems/anonymous-functions/problem1.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions

function greet(x) {
console.log("hello " + x);
}

greet(bob);
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions

****************<ANSWER>******************

const x = "Bob";

function greet() {
console.log("hello " + x);
}

greet();

****************</ANSWER>*****************

19 changes: 5 additions & 14 deletions paperproblems/anonymous-functions/problem2.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions
The final program should be on a single line


function greet(x) {
console.log("hello " + x);
}

function call(f) {
f("bob");
}

call(greet);
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions
The final program should be on a single line

const f = "bob", greet = () => console.log("hello " + f);
21 changes: 9 additions & 12 deletions paperproblems/anonymous-functions/problem3.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions

function greet(x, y) {
console.log("hello " + x + " " + y);
}

function call(f) {
f("Bob", "Dole");
}

call(greet);
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions

const x = "Bob";
const y = "Dole";

function greet() {console.log("hello " + x + " " + y)}

greet();
17 changes: 4 additions & 13 deletions paperproblems/anonymous-functions/problem4.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions

function greet(x) {
console.log("hello " + x);
}

function twice(f) {
f("bob");
f("mary");
}

twice(greet);
Rewrite this program so that it doesn't have a single function definition
In other words, replace all the function definitions with anonymous functions

const f = "bob", greet = () => console.log("hello " + f);
44 changes: 25 additions & 19 deletions paperproblems/anonymous-functions/problem5.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
What is the output of this program? Don't cheat by running it!

function askOnADate(n) {
if(n === "Bob") return "I do!";
else return "Absolutely not!";
}

function soften(f) {
return function(name) {
var r = f(name);
if(r !== "I do!") return "maybe";
else return r;
}
return ret;
}

var softAskOnADate = soften(askOnADate);
console.log(softAskOnADate("Eric"));
console.log(softAskOnADate("Bob"));
What is the output of this program? Don't cheat by running it!

**********************************<ANSWER>***********************************
"maybe" --- Softening applied for Eric (r !== "I do!")
"I do!" --- No softening is applied for Bob (r === "I do!")
ret is never actually used anywhere and could be removed altogether.
**********************************</ANSWER>**********************************

function askOnADate(n) {
if(n === "Bob") return "I do!";
else return "Absolutely not!";
}

function soften(f) {
return function(name) {
var r = f(name);
if(r !== "I do!") return "maybe";
else return r;
}
return ret;
}

var softAskOnADate = soften(askOnADate);
console.log(softAskOnADate("Eric"));
console.log(softAskOnADate("Bob"));
47 changes: 32 additions & 15 deletions paperproblems/array-functions/problem1.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
What is the output of this program?

var animals = [
{species: 'dog', name: 'Nacho'},
{species: 'cat', name: 'Ramses'},
{species: 'bunny',name: 'Flufftail'},
{species: 'dog', name: 'Popcorn'},
{species: 'giraffe', name: 'Neckbeard'},
{species: 'possum', name: ''}
];

var dogs = animals.filter(
function(animal)
{return animal.species === 'dog';});

What is the output of this program?

***************************<ANSWER>***************************

It will return 2.

The animal array's filter function will look for animals
whoses value of species corresponds to the value of 'dog';
since filter returns only items where the evaluation
expression returns true.

The filter function *does* return a new array; so it *can*
be assigned to variable 'dogs'.

dogs will contain an array containing 2 objects with 'dog' as
value of key 'species'.

***************************</ANSWER>**************************

var animals = [
{species: 'dog', name: 'Nacho'},
{species: 'cat', name: 'Ramses'},
{species: 'bunny',name: 'Flufftail'},
{species: 'dog', name: 'Popcorn'},
{species: 'giraffe', name: 'Neckbeard'},
{species: 'possum', name: ''}
];

var dogs = animals.filter(
function(animal)
{return animal.species === 'dog';});

console.log(dogs.length);
Loading