Skip to content

javascript-fundamentals 2 #12

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 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions paperproblems/HOF/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Specifically:
- What does it do?


Filter is a function that takes two parameters. The first is an array, and the second is a function. The filter function goes through every element of the array and returns all elements in a new object that pass the second function's test.


var evenNumbers = [2, 4, 6, 8, 10];
var oddNumbers = [3, 5, 7, 9];
Expand Down
3 changes: 3 additions & 0 deletions paperproblems/HOF/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Specifically:
- What are the types of the parameters
- What does it do?


Map is a function that takes two parakeets. The first is an array and the second is function. Map will pass each element in array to the function and return the results in a new array.

var someNumbers = [1, 2, 3, 4];
var someStrings = ["bob", "ERIC"];

Expand Down
4 changes: 3 additions & 1 deletion paperproblems/HOF/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ function f(g) {
return g(3,5);
}

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

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

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


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

Nan
2 changes: 1 addition & 1 deletion paperproblems/__proto__/problem1.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Draw the object diagram for this program
Draw the object diagram for this program

var x1 = {};
var x2 = {};
Expand Down
9 changes: 7 additions & 2 deletions paperproblems/__proto__/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ x3.__proto__ = x2;
x4.__proto__ = x3;

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

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

20
20
18
18
4 changes: 3 additions & 1 deletion paperproblems/__proto__/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ 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?
b) Why does this exception exist? What's wrong with a cyclic __proto__ value?

This is a cyclic __proto__ value because the proto of the object which a points to, is being pointed at the proto of b. B is pointing back proto of a, creating a loop that will never end. If you are looking for something in proto of a and it has been pointed at the proto of b b, it then look in the proto of b, if there is nothing there, it would normally then go to the global proto and look there. If it couldn't find it there it would return an error. However if the proto of b is also pointing BACK at a, then it will continue looking forever, never reaching the global proto.
11 changes: 10 additions & 1 deletion paperproblems/anonymous-functions/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ function greet(x) {
console.log("hello " + x);
}

greet(bob);
greet("bob");


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

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




8 changes: 8 additions & 0 deletions paperproblems/anonymous-functions/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ function call(f) {
}

call(greet);


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

var call = function(f){f("bob")}


(function(f){f("bob");})(function(x){console.log("hello " + x);})
7 changes: 7 additions & 0 deletions paperproblems/anonymous-functions/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ function call(f) {
}

call(greet);


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

Var call = function (f){f("bob", "dole")};

(function(f){f("bob", "dole");})(function(x,y){console.log("hello" + x + " " +y);})
9 changes: 8 additions & 1 deletion paperproblems/anonymous-functions/problem4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ function twice(f) {
f("mary");
}

twice(greet);
twice(greet);

Var twice = function (f){f("bob");f("Mary");}

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

(function(f){f("bob");f("Mary");})(function(x) {console.log("hello " + x);})

4 changes: 3 additions & 1 deletion paperproblems/array-functions/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ var dogs = animals.filter(
function(animal)
{return animal.species === 'dog';});

console.log(dogs.length);
console.log(dogs.length);

2
5 changes: 4 additions & 1 deletion paperproblems/array-functions/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ var letterN = animals.filter(function(animal) {
return animal.name !== undefined && animal.name[0] === 'N'
});

console.log(letterN[0].name);
console.log(letterN[0].name);


Nacho
6 changes: 5 additions & 1 deletion paperproblems/array-functions/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ var animals = [
var animalNames = animals.map(
function(animal) {return animal.name});

console.log(animalNames.join());
console.log(animalNames.join());

"Nacho,Ramses,Flufftail,Popcorn,Neckbeard,"

// empty string takes a place in the array, but does not appear in the string exactly, but you know it's there because there is a comma after Neckbeard indicating that neckbeard is not the last element in the array. if the last name were undefined, it would look the same as it does here.
5 changes: 4 additions & 1 deletion paperproblems/array-functions/problem4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ function isDog(animal) {
return animal.species === 'dog';
}

animals.some(isDog)
animals.some(isDog)
animals.every(isDog)


true
false


14 changes: 14 additions & 0 deletions paperproblems/arrow-functions/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@ For each of the following expressions:

a) x => x + 1

(no. Input numbers, output numbers)

b) x, y => x * y

Syntax error : if more than one parameter, parentheses required on parameters

c) x => { x * 2 }

(no. inputs number, output number )

d) (x, z) => {console.log(z); return x * z}

(no. inputs number, output number)

e) x => console.log(z); return x * z

(Syntax error. if more than one line of code, curly braces are required)

f) (x) => x * 2

(No, input number output number)

e) () => console.log("hello")

no.input? Output = string

When you're done, check all your answers in the developer console.
4 changes: 3 additions & 1 deletion paperproblems/callbacks/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ function h() {
setTimeout(f, 1000);
}

setTimeout(h, 200);
setTimeout(h, 200);

After 1700ms console.log hello
4 changes: 3 additions & 1 deletion paperproblems/callbacks/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ function h() {
setInterval(g, 1000);
}

setInterval(h, 1000);
setInterval(h, 1000);


7 changes: 5 additions & 2 deletions paperproblems/classes/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ class Person {
}
toString() {
return "My name is " + this.name +
"and I'm " + this.age + " and I make " + this.salary;
" and I'm " + this.age + " and I make " + this.salary;
}
}

var bob = new Person(25, "Bob", 40000);

bob.increaseSalary(-500);
console.log(bob.toString());
console.log(bob.toString());

"Yay!"
"My name is Bob and I'm 25 and I make 39500"
4 changes: 3 additions & 1 deletion paperproblems/conditional-operator/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ function f(x) {
return x < 5 ? 3 : x > 8 ? 4 : x == 6 ? 12 : 9;
}

console.log(f(5));
console.log(f(5));

9
4 changes: 2 additions & 2 deletions paperproblems/constructor-functions/problem1.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Draw the object diagram for the following program.
Hints:
- There are two variables: makePerson and bob
- makePerson has a property called prototype
- There are two variables: Person and bob
- Person has a property called prototype
- bob has properties constructor and __proto__

function Person(name, age) {
Expand Down
4 changes: 3 additions & 1 deletion paperproblems/constructor-functions/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ var bob = new Person("Bob", 30);

Person.prototype.leave = function() {return this.name + " is leaving";}

bob.leave();
bob.leave();

Nothing - need a console.log to print "bob is leaving"
5 changes: 4 additions & 1 deletion paperproblems/constructor-functions/problem3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ Person.prototype.salary = Person.prototype.salary + 300;
Person.__proto__.salary = Person.__proto__.salary + 400;
bob.salary = bob.salary + 50;

console.log(bob.salary);
console.log(bob.salary);


40450
4 changes: 3 additions & 1 deletion paperproblems/constructor-functions/problem4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ var bob = Person("Bob", 30);

bob.name = bob.name + " Dole";

bob.greet();
bob.greet();

error - need New in order to work, otherwise greet is not linked to bob.
19 changes: 17 additions & 2 deletions paperproblems/constructor-functions/problem5.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Rewrite this program so that it does't use the new keyword.
The program must be equivalent in every other way.
(You would never do this in production. This is for learning purposes)

function Person(name, age) {
this.name = name;
Expand All @@ -11,4 +10,20 @@ function Person(name, age) {
}

var bob = new Person("Bob", 30);
var sue = new Person("Sue", 24);

//



function Person(name, age){
var ret= {
name: name,
age: age,
greet: function (){console.log ("hello my name is " + this.name)}
}ret.__proto__=Person.prototype;
Return ret;
}


var bob = Person("bob", 30);

2 changes: 2 additions & 0 deletions paperproblems/exceptions/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ function g() {
}

g();

500
3 changes: 3 additions & 0 deletions paperproblems/exceptions/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ function g() {
}

g();

500
"some error"
11 changes: 0 additions & 11 deletions paperproblems/expansion/problem1.txt

This file was deleted.

16 changes: 16 additions & 0 deletions paperproblems/inheritance/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ class Shape {
this.area() + " and perimeter " + this.perimeter();
}
}

class Rectangle extends Shape {
constructor(width, height){
super ('rectangle');
this.width = width;
this.height = height;
}
area(){
return this.height * this.width
}
perimeter(){
return (this.height*2)+(this.width*2)
}
}
var rec = new Rectangle (3, 4);
console.log(rec.toString());
3 changes: 3 additions & 0 deletions paperproblems/return/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ function f() {

console.log(f());


5

5 changes: 5 additions & 0 deletions paperproblems/this/problem1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ function foo() {
var obj = {bar: foo, baz: 8};
obj.bar();

8




2 changes: 2 additions & 0 deletions paperproblems/this/problem2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ var obj = {
var g = obj.bar;
g();

undefined

Loading