Skip to content
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

update #8

Open
wants to merge 1 commit into
base: main
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
40 changes: 34 additions & 6 deletions exercises/part1-fizzbuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,40 @@ Hint 2. Consider how modulo (calculating the remainder of a division problem) mi
Believe it or not, this is a common programming challenge in job interviews!

===================== */

/* =====================

Start code

===================== */
function IfThree(n){
if (n % 3 ==0){
return "Fizz"
} else {
return n
}
}

function IfFive(n){
if (n % 5 ==0){
return "Buzz"
} else {
return n
}
}


let s = '';

for (i = 1; i<101; i++) {
if (i % 3 == 0) {
if (i % 5 == 0){
s += "FizzBuzz" +'\n';
} else{
s += "Fizz" +'\n';
}

}
else if(i % 5 ==0 ) {
s += "Buzz" +'\n';
}
else {s += i.toString}
}
console.log(s);



Expand Down
35 changes: 26 additions & 9 deletions exercises/part2-basic-functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ in years. (Let's just assume this person was born January 1 at 12:01 AM)
Example: "age(2000) should return 22"
===================== */

let age = (birthYear) => {};
let age = (birthYear) => { return 2022-birthYear };

console.log('age success:', age(1971) === 51);

Expand All @@ -22,7 +22,7 @@ Instructions: Write a function that adds one to the number provided
Example: "plusOne(2) should return 3"
===================== */

let plusOne = () => {};
let plusOne = (i) => { return i+1 };

console.log('plusOne success:', plusOne(99) === 100);

Expand All @@ -31,7 +31,7 @@ Instructions: Write a function that multiplies the number provided by three
Example: "timesThree(2) should return 6"
===================== */

let timesThree = () => {};
let timesThree = (i) => { return i*3 };

console.log('timesThree success:', timesThree(33) === 99);

Expand All @@ -40,7 +40,7 @@ Instructions: Write a function that adds two given numbers
Example: "add(2, 3) should return 5"
===================== */

let add = () => {};
let add = (x,y) => { return x+y };

console.log('add success:', add(4, 6) === 10);

Expand All @@ -49,7 +49,7 @@ Instructions: Write a function that multiplies two given numbers
Example: "multiply(2, 3) should return 6"
===================== */

let multiply = () => {};
let multiply = (x,y) => { return x*y };

console.log('multiply success:', multiply(4, 6) === 24);

Expand All @@ -58,7 +58,7 @@ Instructions: "Write a function that returns the value of an array at a specifie
Example: "valueAtIndex(['Mercury', 'Venus', 'Earth', 'Mars'], 2) should return 'Earth'"
===================== */

let valueAtIndex = () => {};
let valueAtIndex = (list, i) => { return list[i] };

console.log('valueAtIndex success:', valueAtIndex([9, 8, 7, 6, 5], 2) === 7);

Expand All @@ -67,22 +67,39 @@ Instructions: "Write a function that returns the value of an object at a specifi
Example: "valueAtKey({'name': 'Nathan'}, 'name') should return 'Nathan'"
===================== */

let valueAtKey = () => {};
let valueAtKey = (dic, i) => {
// let arry = Object.entries(list);
return dic[i]
};

console.log('valueAtKey success:', valueAtKey({ 'foo': 'bar' }, 'foo') === 'bar');

/* =====================
Instructions: Write a function that returns true if a number is even.
===================== */

let isEven = () => {};
let isEven = (i) => {
if(i % 2 == 0) {
return true
}
else {
return false
}
};

console.log('isEven success:', isEven(2) === true && isEven(3) === false);

/* =====================
Instructions: Write a function that returns true if a number is odd.
===================== */

let isOdd = () => {};
let isOdd = (i) => {
if (i % 2 == 1){
return true
}
else {
return false
}
};

console.log('isOdd success:', isOdd(4) === false && isOdd(5) === true);
94 changes: 81 additions & 13 deletions exercises/part3-iterators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ Instructions: Write a function which counts the number of times a value occurs i
Example: countItem(['a', 'b', 'a'], 'a') should return 2
===================== */

let countItem = (arr, item) => {};
let countItem = (arr, item) => {
let cnt = 0;
for (i of arr) {
if (item == i){
cnt += 1;
}
}
return cnt;
};

console.log('countItem success:', countItem([1, 2, 3, 4, 5, 4, 4], 4) === 3);

Expand All @@ -27,8 +35,22 @@ Instructions: Write a function which takes an array and returns a new array with
original array.
===================== */

let isEven = (n) => {};
let filterEven = (arr) => {};
let isEven = (n) => {
if ( n % 2 == 0){
return true
}
return false
};

let filterEven = (arr) => {
let EvenArr = [];
for (let i = 0; i < arr.length; i++ ){
if(isEven(arr[i])) {
EvenArr.push(arr[i]);
}
}
return EvenArr
};

console.log('filterEven success:', _(filterEven([1, 2, 3, 4, 5, 4, 4])).isEqual([2, 4, 4, 4]));

Expand All @@ -38,8 +60,21 @@ Instructions: Write a function which takes an array and returns a new array with
function MUST use the isOdd function and MUST NOT change the original array.
===================== */

let isOdd = (n) => {};
let filterOdd = (arr) => {};
let isOdd = (n) => {
if ( n % 2 != 0){
return true
}
return false
};
let filterOdd = (arr) => {
let OddArr = [];
for (let i = 0; i < arr.length; i++ ){
if(isOdd(arr[i])) {
OddArr.push(arr[i]);
}
}
return OddArr
};

console.log('filterOdd success:', _(filterOdd([1, 2, 3, 4, 5, 4, 4])).isEqual([1, 3, 5]));

Expand All @@ -50,8 +85,16 @@ Instructions: Write a function which takes an array and returns a new array with
original array.
===================== */

let plusOne = (n) => {};
let mapIncrement = (arr) => {};
let plusOne = (n) => {
return n+1
};
let mapIncrement = (arr) => {
let PlusArr = [];
for (let i =0; i < arr.length; i++){
PlusArr.push(plusOne(arr[i]));
}
return PlusArr
};

console.log('mapIncrement success:', _(mapIncrement([1, 2, 3, 4, 5, 4, 4])).isEqual([2, 3, 4, 5, 6, 5, 5]));

Expand All @@ -62,8 +105,16 @@ Instructions: Write a function which takes an array and returns a new array with
original array.
===================== */

let timesThree = (n) => {};
let mapTriple = (arr) => {};
let timesThree = (n) => {
return n*3;
};
let mapTriple = (arr) => {
let TimesArr = [];
for (let i =0; i < arr.length; i++){
TimesArr.push(timesThree(arr[i]));
}
return TimesArr
};

console.log('mapTriple success:', _(mapTriple([1, 2, 3, 4, 5, 4, 4])).isEqual([3, 6, 9, 12, 15, 12, 12]));

Expand All @@ -74,8 +125,17 @@ Instructions: Write a function which takes an array and returns the sum of all
array.
===================== */

let add = (n1, n2) => {};
let reduceSum = (arr) => {};
let add = (n1, n2) => {
return n1+n2;
};
let reduceSum = (arr) => {
let s=0;
for (i =0; i< arr.length; i++){
s = add(s, arr[i]);
}
return s;

};

console.log('reduceSum success:', reduceSum([1, 2, 3, 4, 5, 4, 4]) === 23);

Expand All @@ -86,7 +146,15 @@ Instructions: Write a function which takes an array and returns the product of
original array.
===================== */

let multiply = (n1, n2) => {};
let reduceProduct = (arr) => {};
let multiply = (n1, n2) => {
return n1*n2;
};
let reduceProduct = (arr) => {
let s=1;
for (i=0; i<arr.length; i++){
s = multiply(s,arr[i]);
}
return s;
};

console.log('reduceProduct success:', reduceProduct([1, 2, 3, 4, 5, 4, 4]) === 1920);
34 changes: 30 additions & 4 deletions exercises/part4-functions-as-values/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ Instructions: Write a function which takes an array and returns a new array,
on whether the item satisfies some condition).
===================== */

let filter = (arr, pred) => {};
let filter = (arr, pred) => {
let newarr = [];
for (let i = 0; i < arr.length ; i++){
if(pred(arr[i])){
newarr.push(arr[i]);
}
}
return newarr;
};

console.log('filter success #1:', _(filter([1, 2, 3, 4, 5, 4, 4], isEven)).isEqual([2, 4, 4, 4]));
console.log('filter success #2:', _(filter([1, 2, 3, 4, 5, 4, 4], isOdd)).isEqual([1, 3, 5]));
Expand All @@ -27,7 +35,13 @@ Instructions: Write a function which takes an array and returns a new array,
where each item has a function applied to it.
===================== */

let map = (arr, func) => {};
let map = (arr, func) => {
let newArr = [];
for (let i =0; i < arr.length; i++){
newArr.push(func(arr[i]));
}
return newArr;
};

console.log('map success #1:', _(map([1, 2, 3, 4, 5, 4, 4], plusOne)).isEqual([2, 3, 4, 5, 6, 5, 5]));
console.log('map success #2:', _(map([1, 2, 3, 4, 5, 4, 4], timesThree)).isEqual([3, 6, 9, 12, 15, 12, 12]));
Expand All @@ -52,7 +66,13 @@ Instructions: Write a function which takes an array and returns the value of

===================== */

let reduce = (arr, func, initial) => {};
let reduce = (arr, func, initial) => {
let r = initial;
for( i = 0 ; i<arr.length ; i++){
r = func(r, arr[i]);
}
return r;
};

console.log('reduce success #1:', reduce([1, 2, 3, 4, 5, 4, 4], add, 0) === 23);
console.log('reduce success #2:', reduce([1, 2, 3, 4, 5, 4, 4], multiply, 1) === 1920);
Expand All @@ -65,6 +85,12 @@ Bonus: Create a function called sumSquares that takes an array and returns
`multiply` functions that you developed before).
===================== */

let sumSquares = (arr) => {};
let squared = (i) =>{
return i * i;
}

let sumSquares = (arr) => {
return reduce(map(arr, squared), add, 0);
};

console.log('sumSquares success:', sumSquares([1, 2, 3, 4]) === 30);