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

Updated Function Exercises Morgan Griffiths #14

Open
wants to merge 7 commits 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
21 changes: 21 additions & 0 deletions exercises/part1-fizzbuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,24 @@ Start code
End code

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

// Step 1: Write a program that uses console.log to print each of the numbers from 1 to 100.
for (let i = 1; i<= 100; i++){
console.log(i);
}

// Step 2: Update the program so that, for multiples of three, it prints "Fizz" instead of the number and, for multiples of five, it prints "Buzz" instead of the number.


// Step 3: For numbers which are multiples of *both* three and five, make the program print "FizzBuzz" instead of "Fizz" or "Buzz".
for (let i = 1; i<= 100; i++){
if (i % 3 == 0 && i % 5 == 0) {
console.log('FizzBuzz');
} else if (i % 3 == 0) {
console.log('Fizz');
} else if (i % 5 == 0) {
console.log('Buzz');
} else {
console.log(i);
}
}
40 changes: 31 additions & 9 deletions exercises/part2-basic-functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ 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) => {
let age2 = 2022 - birthYear;
return age2;
};

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

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

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

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

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

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

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

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

let add = () => {};
let add = (num1, num2) => { return num1 + num2};

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

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

let multiply = () => {};
let multiply = (num1, num2) => { return num1*num2 };

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

/* =====================
Instructions: "Write a function that returns the value of an array at a specified index"
Example: "valueAtIndex(['Mercury', 'Venus', 'Earth', 'Mars'], 2) should return 'Earth'"
===================== */
const numArray = [9, 8, 7, 6, 5];

let valueAtIndex = () => {};
let valueAtIndex = (numArray, index) => {
return (numArray[index]);
};

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

Expand All @@ -67,22 +75,36 @@ 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 = (obj, key) => {
return obj[key];
};

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 = (n) => {
if (n%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 = (n) => {
if (n%2 != 0) {
return true;
} else {
return false;
}
};

console.log('isOdd success:', isOdd(4) === false && isOdd(5) === true);
91 changes: 77 additions & 14 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 count = 0;
for(let i = 0; i < arr.length; i++) {
if (arr[i] === item) {
count = 1 + count;
}
} return count;
};


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

Expand All @@ -27,8 +35,23 @@ 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;
} else {
return false;
}
};


let filterEven = (arr) => {
const evenArr = [];
for( let i=0; i<arr.length; i+=1) {
if(isEven(arr[i]) == true)
{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 +61,22 @@ 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;
} else {
return false;
}
};

let filterOdd = (arr) => {
const oddArr = [];
for( let i=0; i<arr.length; i+=1) {
if(isOdd(arr[i]) == true)
{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 +87,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) => {
const plusOneArr = [];
for( let i=0; i<arr.length; i+=1) {
plusOneArr.push(plusOne(arr[i]));
} return plusOneArr;
};

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

Expand All @@ -62,8 +107,14 @@ 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) => {
const timesThreeArr = [];
for(let i=0; i<arr.length; i+=1) {
timesThreeArr.push(timesThree(arr[i]));
} return timesThreeArr;
};

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,15 @@ Instructions: Write a function which takes an array and returns the sum of all
array.
===================== */

let add = (n1, n2) => {};
let reduceSum = (arr) => {};
let add = (num1, num2) => { return num1 + num2};


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

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

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

let multiply = (n1, n2) => {};
let reduceProduct = (arr) => {};
let multiply = (num1, num2) => { return num1*num2 };

console.log('reduceProduct success:', reduceProduct([1, 2, 3, 4, 5, 4, 4]) === 1920);
let reduceProduct = (arr) => {
let product = 1;
for(let i=0; i<arr.length; i+=1) {
product = multiply(product, arr[i]);
} return product;
};
console.log('reduceProduct success:', reduceProduct([1, 2, 3, 4, 5, 4, 4]) === 1920);
29 changes: 24 additions & 5 deletions exercises/part4-functions-as-values/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ Instructions: Write a function which takes an array and returns a new array,
function that takes one item as input and returns either true or false based
on whether the item satisfies some condition).
===================== */

let filter = (arr, pred) => {};
let filter = (arr, pred) => {
const newArr = [];
for (let i = 0; i <arr.length; i+=1) {
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 +33,14 @@ 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) => {
const newArr = [];
for( let i=0; i< arr.length; i+=1) {
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 +65,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 n = initial;
for (let i = 0; i< arr.length; i+=1) {
n = func.call(undefined, n, arr[i], i, arr);
}
return n;
};

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 +84,6 @@ Bonus: Create a function called sumSquares that takes an array and returns
`multiply` functions that you developed before).
===================== */

let sumSquares = (arr) => {};
let sumSquares = arr => arr.reduce((a, num) => a + num **2, 0);

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