Skip to content
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
47 changes: 32 additions & 15 deletions recursion.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
// to test these problems you can run 'node recursion.js' in your terminal
// Problem 1:

let n = 1;
while (n <= 10) {
console.log('While Loop', n);
n++;
}
// let n = 1;
// while (n <= 10) {
// console.log('While Loop', n);
// n++;
// }

// write a recursive - function called countToTen that mimics the while loop above.

// code here

// when you code is ready, un-comment the next line and run the file
// let n = 1;
// function countToTen() {
// if (n > 10) break; //base
// console.log(n);
// return countToTen(++n);
// };

// // when you code is ready, un-comment the next line and run the file
// console.log(countToTen());
/* ================ Next Problem ================= */



// Problem 2:

const factorial = n => {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
};
// const factorial = n => {
// let result = 1;
// for (let i = 2; i <= n; i++) {
// result *= i;
// }
// return result;
// };

console.log(factorial(5));
// console.log(factorial(5));

// write the above function in a recursive way.

let fact = 1;
let num = 2;
let n = 5;
function recursiveFactorial() {
if (num === n) return fact; // base
fact *= num;
return recursiveFactorial(num++)
}
// when your code is ready, un-comment the next line and run the file
// console.log(recursiveFactorial());
console.log(recursiveFactorial());
57 changes: 44 additions & 13 deletions this.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1.
* 2.
* 3.
* 4.
* 1. Window/Global Object Binding: When in the global scope, the value of “this” will be the window/console Object;
* 2. Implicit Binding: Whenever a function is called by a preceding dot, the object before that dot becomes 'this.
* 3. New Binding: When constructor function is used, object is created and returned by the constructor function.
* 4. Explicit binding: Whenever JavaScript’s call, apply or bind method is used, this is explicitly defined.
*
* write out a code example of each explanation above
*/

console.log('hello world!');

// Principle 1

// Principle 1 ==========
// code example for Window Binding
// function callWindow () {
// console.log(this);
// }
// callWindow();

// Principle 2

// Principle 2 ==========
// code example for Implicit Binding
// const computer = {
// gpu: 1,
// cpu: 2,
// motherboard: 1,
// cpuNumber: function () {
// console.log(this.cpu);
// }
// }
// computer.cpuNumber();

// Principle 3 ===========
// code example for New Binding

// Principle 3
// let Computer = function(gpu, cpu, motherboard) {
// this.gpu = gpu;
// this.cpu = cpu;
// this.motherboard = motherboard;
// }

// code example for New Binding
// let mike = new Computer('nvidia', 'i7', 'msi')
// let tom = new Computer('radeon', 'ryzen', 'gigabyte')

// Principle 4
// console.log(mike.gpu && mike.cpu);
// console.log(tom);

// Principle 4 ===============
// code example for Explicit Binding

let myComputer = {
gpu: 'nvidia',
cpu: 'intel',
motherboard: 'asus'
}

let saySpec = function() {
console.log('My computer has: ' + this.gpu + ' and ' + this.cpu + ' and ' + this.motherboard)
}

saySpec.call(myComputer);