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

Simran Arora- Check1 #18

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions exercises/part1-fizzbuzz/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>FizzBuzz: Looping</title>
<h1>FizzBuzz Looping</h1>
</head>

<body>
Expand Down
15 changes: 15 additions & 0 deletions exercises/part1-fizzbuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ Start code
===================== */


for (let num = 1; num <= 100; num++ ) {
if (num % 15 === 0) {
console.log("FizzBuzz");
}
else if (num % 5 === 0) {
console.log("Buzz");
}
else if (num % 3 === 0 ) {
console.log("Fizz");
}
else {
console.log(num);
}
}



/* =====================
Expand Down
1 change: 1 addition & 0 deletions exercises/part2-basic-functions/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<html>
<head>
<title>Basic Functions</title>
<h1>Basic Functions</h1>
</head>

<body>
Expand Down
18 changes: 9 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 = (plusNum) => {return plusNum+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 = (num) => {return num*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 = (num, num1) => {return num+num1};

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 = (one, two) => {return one*two};

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 = (value, valuenum) => {return value[valuenum]};

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

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

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 = (even) => {return even%2==0};

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 = (odd) => {return odd%2!==0};

console.log('isOdd success:', isOdd(4) === false && isOdd(5) === true);
114 changes: 101 additions & 13 deletions exercises/part3-iterators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ 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 = count + 1;
} else {
count;
}
}
return count;
};


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

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

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

let filterEven = (arr) => {
let arrEven = [];
let index = 0;
for (let i = 0; i < arr.length; i++) {
if (isEven(arr[i]) == true) {
arrEven[index] = arr[i];
index = index + 1;
} else {
arrEven;
}
}
return arrEven;
};


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

Expand All @@ -38,8 +71,28 @@ 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 = (num) => {
let div2 = num%2;
if (div2 != 0) {
return true;
} else {
return false;
}
};

let filterOdd = (arr) => {
let arrOdd = [];
let index = 0;
for (let i = 0; i < arr.length; i++) {
if (isOdd(arr[i]) == true) {
arrOdd[index] = arr[i];
index = index + 1;
} else {
arrOdd;
}
}
return arrOdd;
};

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

Expand All @@ -50,8 +103,17 @@ 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 add1 = [];
for (let i = 0; i < arr.length; i++){
add1[i] = plusOne(arr[i]);
}
return add1;
};

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

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

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

Expand All @@ -74,8 +144,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 numAdd = 0;
for (let i = 0; i < arr.length; i++){
numAdd = add(numAdd, arr[i]);
}
return numAdd;
};

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

Expand All @@ -86,7 +165,16 @@ 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 mul = 1;
for (let i=0; i<arr.length; i++) {
mul = multiply(mul, arr[i]);
}
return mul;
};

console.log('reduceProduct success:', reduceProduct([1, 2, 3, 4, 5, 4, 4]) === 1920);
33 changes: 29 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,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) => {
let mapArr = [];
for (let i = 0; i< arr.length; i++) {
mapArr.push(func(arr[i]));
}
return mapArr;
};


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 +67,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 final = initial;
for(let num of arr) {
final = func(final, num);
}
return final;
};

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

let sumSquares = (arr) => {};
let sumSquares = (arr) => {
const square = arr.map(num => num * num);
const sum = square.reduce( (first, second) => first + second, 0);
return sum;
};

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