diff --git a/exercises/part1-fizzbuzz/index.js b/exercises/part1-fizzbuzz/index.js index a4302f9..ab6cdc8 100644 --- a/exercises/part1-fizzbuzz/index.js +++ b/exercises/part1-fizzbuzz/index.js @@ -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); diff --git a/exercises/part2-basic-functions/index.js b/exercises/part2-basic-functions/index.js index ee2138b..15ca984 100644 --- a/exercises/part2-basic-functions/index.js +++ b/exercises/part2-basic-functions/index.js @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -67,7 +67,10 @@ 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'); @@ -75,7 +78,14 @@ 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); @@ -83,6 +93,13 @@ 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); diff --git a/exercises/part3-iterators/index.js b/exercises/part3-iterators/index.js index ff4696f..040ba02 100644 --- a/exercises/part3-iterators/index.js +++ b/exercises/part3-iterators/index.js @@ -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); @@ -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])); @@ -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])); @@ -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])); @@ -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])); @@ -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); @@ -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 {}; +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])); @@ -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])); @@ -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 {}; +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);