From 39325c46b400456fcb1968a0b044267d3c641557 Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Tue, 18 Oct 2022 21:00:42 -0400 Subject: [PATCH 1/9] first commit function/datatype --- src/01-functions.js | 45 +++++++++++++++++++++++++------- src/02-data-types.js | 62 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 20 deletions(-) diff --git a/src/01-functions.js b/src/01-functions.js index 8d772b6a..b7a42631 100644 --- a/src/01-functions.js +++ b/src/01-functions.js @@ -9,7 +9,9 @@ * alwaysTrue(); * //> true */ -function alwaysTrue() {} +function alwaysTrue() { + return true +} /** * greet() @@ -24,7 +26,9 @@ function alwaysTrue() {} * greet("Ian"); * //> "Hello, Ian." */ -function greet() {} +function greet(name) { + return `Hello, ${name}.` +} /** * add() @@ -39,7 +43,9 @@ function greet() {} * add(10, 20); * //> 30 */ -function add() {} +function add(a, b) { +return a + b +} /** * multiply() @@ -54,7 +60,9 @@ function add() {} * multiply(2, 5); * //> 10 */ -function multiply() {} +function multiply(a, b) { + return a * b +} /** * average() @@ -74,7 +82,9 @@ function multiply() {} * average(10, 6); * //> 8 // 10 + 6 = 16; 16/2 = 8; */ -function average() {} +function average(a, b) { + return (a + b) / 2 +} /** * averageThree() @@ -93,7 +103,9 @@ function average() {} * averageThree(10, 11, 19); * //> 10 // 10 + 11 + 19 = 30; 30 / 3 = 10; */ -function averageThree() {} +function averageThree(a, b, c) { + return (a + b + c) / 3 +} /** * compareTypes() @@ -112,7 +124,13 @@ function averageThree() {} * compareTypes("left", 10); * //> false */ -function compareTypes() {} +function compareTypes(a, b) { + if(typeof a === typeof b){ + return true + } else { + return false + } +} /** * flipSign() @@ -131,7 +149,9 @@ function compareTypes() {} * flipSign(50); * //> -50 */ -function flipSign() {} +function flipSign(a) { + return -a +} /** * joinStringsWithSpaces() @@ -151,7 +171,9 @@ function flipSign() {} * joinStringsWithSpaces("left", "right", "up", "down", "away"); * //> "left right up down away" */ -function joinStringsWithSpaces() {} +function joinStringsWithSpaces(a, b, c, d, e) { + return `${a} ${b} ${c} ${d} ${e}` +} /** * getFirstAndLastCharacter() @@ -168,7 +190,10 @@ function joinStringsWithSpaces() {} * getFirstAndLastCharacter("upwards"); * //> "us" */ -function getFirstAndLastCharacter() {} +function getFirstAndLastCharacter(a) { + + return a[0] + a[a.length -1] +} // Do not change the code below. module.exports = { diff --git a/src/02-data-types.js b/src/02-data-types.js index 79cf96cf..17f64cca 100644 --- a/src/02-data-types.js +++ b/src/02-data-types.js @@ -9,7 +9,9 @@ * getLengthOfString("down"); * //> 4 */ -function getLengthOfString(str) {} +function getLengthOfString(str) { + return str.length +} /** * convertToNumber() @@ -22,7 +24,9 @@ function getLengthOfString(str) {} * convertToNumber("111"); * //> 111 */ -function convertToNumber(val) {} +function convertToNumber(val) { + return Number(val) +} /** * convertToString() @@ -36,7 +40,9 @@ function convertToNumber(val) {} * convertToString(99); * //> "99" */ -function convertToString(val) {} +function convertToString(val) { + return `${val}` +} /** * convertToShoutingText() @@ -49,7 +55,9 @@ function convertToString(val) {} * convertToShoutingText("Hello There"); * //> "HELLO THERE" */ -function convertToShoutingText(text) {} +function convertToShoutingText(text) { + return text.toUpperCase() +} /** * convertToWhisperText() @@ -62,7 +70,9 @@ function convertToShoutingText(text) {} * convertToWhisperText("Hello There"); * //> "hello there" */ -function convertToWhisperText(text) {} +function convertToWhisperText(text) { + return text.toLowerCase() +} /** * checkIfCharacterIsInString() @@ -79,7 +89,14 @@ function convertToWhisperText(text) {} * checkIfCharacterIsInString("hello there", "a"); * //> false */ -function checkIfCharacterIsInString(text, character) {} +function checkIfCharacterIsInString(text, character) { + if (text.includes(character)){ + return true + } else{ + return false + } + } + /** * isEven() @@ -95,7 +112,14 @@ function checkIfCharacterIsInString(text, character) {} * isEven(11); * //> false */ -function isEven(num) {} +function isEven(num) { + if (num % 2 === 0){ + return true + }else{ + return false + } + } + /** * isOdd() @@ -111,7 +135,13 @@ function isEven(num) {} * isOdd(11); * //> true */ -function isOdd(num) {} +function isOdd(num) { + if (num % 2 !== 0){ + return true + }else { + return false + } +} /** * isTruthy() @@ -127,7 +157,13 @@ function isOdd(num) {} * isTruthy(null); * //> false */ -function isTruthy(val) {} +function isTruthy(val) { + if (val){ + return true + }else{ + return false + } +} /** * isFalsy() @@ -143,7 +179,13 @@ function isTruthy(val) {} * isFalsy(" "); * //> false */ -function isFalsy(val) {} +function isFalsy(val) { + if (!val){ + return true + }else{ + return false + } +} // Do not change the code below. module.exports = { From 26f308f2c4a5ac1145f1ce2d99d7835f273a9f21 Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Wed, 19 Oct 2022 18:34:38 -0400 Subject: [PATCH 2/9] finished control flow --- src/03-control-flow.js | 100 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 11 deletions(-) diff --git a/src/03-control-flow.js b/src/03-control-flow.js index 8f675935..7e28b106 100644 --- a/src/03-control-flow.js +++ b/src/03-control-flow.js @@ -14,7 +14,13 @@ * isEqual(10, "10"); * //> false */ -function isEqual(a, b) {} +function isEqual(a, b) { + if (a === b) { + return true + } else { + return false + } +} /** * findLarger() @@ -28,7 +34,13 @@ function isEqual(a, b) {} * findLarger(19, 7); * //> 19 */ -function findLarger(a, b) {} +function findLarger(a, b) { + if (a > b) { + return a + } else { + return b + } +} /** * findLargerOrTie() @@ -45,7 +57,15 @@ function findLarger(a, b) {} * findLargerOrTie(0, 0); * //> "tie" */ -function findLargerOrTie(a, b) {} +function findLargerOrTie(a, b) { + if (a > b) { + return a + } else if (a < b) { + return b + } else if (a === b) { + return "tie" + } +} /** * positiveNegativeOrZero() @@ -65,7 +85,15 @@ function findLargerOrTie(a, b) {} * positiveNegativeOrZero(1); * //> "Positive" */ -function positiveNegativeOrZero(a) {} +function positiveNegativeOrZero(a) { + if (a > 0) { + return "Positive" + } else if (a < 0) { + return "Negative" + } else if (a === 0) { + return "Zero" + } +} /** * aroundTheWorldGreeting() @@ -86,7 +114,17 @@ function positiveNegativeOrZero(a) {} * aroundTheWorldGreeting(); * //> "Hello World" */ -function aroundTheWorldGreeting(language) {} +function aroundTheWorldGreeting(language) { + if (language === "english") { + return "Hello World" + } else if (language === "spanish") { + return "Hola Mundo" + } else if (language === "german") { + return "Hallo Welt" + } else { + return "Hello World" + } +} /** * aroundTheWorldGreetingWithSwitch() @@ -109,7 +147,21 @@ function aroundTheWorldGreeting(language) {} * aroundTheWorldGreetingWithSwitch(); * //> "Hello World" */ -function aroundTheWorldGreetingWithSwitch(language) {} +function aroundTheWorldGreetingWithSwitch(language) { + switch (language) { + case "english": + return "Hello World"; + break; + case "spanish": + return "Hola Mundo"; + break; + case "german": + return "Hallo Welt" + break; + default: + return "Hello World" + } +} /** * calculateLetterGrade() @@ -128,8 +180,19 @@ function aroundTheWorldGreetingWithSwitch(language) {} * calculateLetterGrade(99); * //> "A" */ -function calculateLetterGrade(grade) {} - +function calculateLetterGrade(grade) { + if (grade >= 90) { + return "A" + } else if (grade >= 80 && grade < 90) { + return "B" + } else if (grade >= 70 && grade < 80) { + return "C" + } else if (grade >= 60 && grade < 70) { + return "D" + } else if (grade < 60) { + return "F" + } +} /** * animalCounts() * --------------------- @@ -146,7 +209,13 @@ function calculateLetterGrade(grade) {} * animalCounts("pig", 1); * //> "1 pig" */ -function animalCounts(animal, numberOfAnimals) {} +function animalCounts(animal, numberOfAnimals) { + if (numberOfAnimals > 1) { + return `${numberOfAnimals} ${animal}s` + } else { + return `${numberOfAnimals} ${animal}` + } +} /** * evenOrOdd() @@ -163,7 +232,13 @@ function animalCounts(animal, numberOfAnimals) {} * evenOrOdd(48); * //> "Even" */ -function evenOrOdd(a) {} +function evenOrOdd(a) { + if (a % 2 !== 0) { + return "Odd" + } else if (a % 2 === 0) { + return "Even" + } +} /** * evenOrOddWithTernary() @@ -179,7 +254,10 @@ function evenOrOdd(a) {} * evenOrOddWithTernary(8); * //> "Even" */ -function evenOrOddWithTernary(a) {} +function evenOrOddWithTernary(a) { + return a % 2 === 0 ? "Even" : "Odd"; +} + // Do not change any code below this line. module.exports = { From bde2c941ce956e2191db87914bd098855c0288b1 Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Wed, 19 Oct 2022 20:40:23 -0400 Subject: [PATCH 3/9] finished Arrays --- src/04-arrays.js | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/04-arrays.js b/src/04-arrays.js index 5317f7c3..6317f86a 100644 --- a/src/04-arrays.js +++ b/src/04-arrays.js @@ -10,7 +10,10 @@ * createEmptyArray(); * //> [] */ -function createEmptyArray() {} +function createEmptyArray() { + let arr = [] + return arr +} /** * createArrayWithTwoElements() @@ -24,7 +27,10 @@ function createEmptyArray() {} * createArrayWithTwoElements(true, false); * //> [ true, false ] */ -function createArrayWithTwoElements(a, b) {} +function createArrayWithTwoElements(a, b) { + let newArr = [a, b]; + return [newArr[0], newArr[newArr.length - 1]] +} /** * getArrayLength() @@ -37,7 +43,9 @@ function createArrayWithTwoElements(a, b) {} * getArrayLength([ 10, 20, 30 ]); * //> 3 */ -function getArrayLength(array) {} +function getArrayLength(array) { + return array.length +} /** * getFirstElementOfArray() @@ -51,7 +59,9 @@ function getArrayLength(array) {} * getFirstElementOfArray([ 10, 20, 30 ]); * //> 10 */ -function getFirstElementOfArray(array) {} +function getFirstElementOfArray(array) { + return array[0] +} /** * getLastElementOfArray() @@ -65,7 +75,9 @@ function getFirstElementOfArray(array) {} * getLastElementOfArray([ null, undefined ]); * //> undefined */ -function getLastElementOfArray(array) {} +function getLastElementOfArray(array) { + return array[array.length - 1] +} /** * addElementToEndOfArray() @@ -79,7 +91,10 @@ function getLastElementOfArray(array) {} * addElementToEndOfArray([ 10 ], 9); * //> [ 10, 9 ] */ -function addElementToEndOfArray(array, element) {} +function addElementToEndOfArray(array, element) { + array.push(element) + return array +} /** * removeElementFromEndOfArray() @@ -92,7 +107,9 @@ function addElementToEndOfArray(array, element) {} * removeElementFromEndOfArray([ 10, 9, 8 ]); * //> 8 */ -function removeElementFromEndOfArray(array) {} +function removeElementFromEndOfArray(array) { + return array.pop() +} /** * addElementToFrontOfArray() @@ -106,7 +123,10 @@ function removeElementFromEndOfArray(array) {} * addElementToFrontOfArray([ 10 ], 9); * //> [ 9, 10 ] */ -function addElementToFrontOfArray(array, element) {} +function addElementToFrontOfArray(array, element) { + array.unshift(element) + return array +} /** * removeElementFromFrontOfArray() @@ -119,7 +139,9 @@ function addElementToFrontOfArray(array, element) {} * removeElementFromFrontOfArray([ 10, 9, 8 ]); * //> 10 */ -function removeElementFromFrontOfArray(array) {} +function removeElementFromFrontOfArray(array) { + return array.shift() + } /** * getMiddleElement() @@ -135,7 +157,9 @@ function removeElementFromFrontOfArray(array) {} * getMiddleElement([ 10, null, "30" ]); * //> null */ -function getMiddleElement(array) {} +function getMiddleElement(array) { + return array[(array.length - 1) / 2] + } // Do not change any code below this line. module.exports = { From b92ff3830160870bad5cfb71c9f471a3358014be Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Wed, 19 Oct 2022 22:01:39 -0400 Subject: [PATCH 4/9] objects finished --- src/05-objects.js | 49 +++++++++++++++++++++++++++++++++++++---------- src/06-loops.js | 27 ++++++++++++++++---------- 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/05-objects.js b/src/05-objects.js index 3e97313c..87daaab7 100644 --- a/src/05-objects.js +++ b/src/05-objects.js @@ -9,7 +9,10 @@ * createEmptyObject() * //> {} */ -function createEmptyObject() {} +function createEmptyObject() { + let obj = {} + return obj +} /** * createObjectWithValue() @@ -22,7 +25,10 @@ function createEmptyObject() {} * createObjectWithValue(19); * //> { whateverKey: 19 } */ -function createObjectWithValue(val) {} +function createObjectWithValue(val) { + let obj = { val } + return obj +} /** * createObjectWithKey() @@ -35,7 +41,10 @@ function createObjectWithValue(val) {} * createObjectWithKey("left"); * //> { left: false } */ -function createObjectWithKey(key) {} +function createObjectWithKey(key) { + let obj = { [key]: 1 } + return obj +} /** * createObjectWithKeyValuePair() @@ -49,7 +58,10 @@ function createObjectWithKey(key) {} * createObjectWithKeyValuePair("left", "right"); * //> { left: "right" } */ -function createObjectWithKeyValuePair(key, val) {} +function createObjectWithKeyValuePair(key, val) { + let obj = { [key]: val } + return obj +} /** * getNameFromObject() @@ -62,7 +74,9 @@ function createObjectWithKeyValuePair(key, val) {} * getNameFromObject({ name: "Jay" }); * //> "Jay" */ -function getNameFromObject(object) {} +function getNameFromObject(object) { + return object.name +} /** * getAgeFromObject() @@ -75,7 +89,9 @@ function getNameFromObject(object) {} * getAgeFromObject({ age: 30 }); * //> 30 */ -function getAgeFromObject(object) {} +function getAgeFromObject(object) { + return object.age +} /** * addValueToObject() @@ -89,7 +105,10 @@ function getAgeFromObject(object) {} * addValueToObject({ left: true }, false); * //> { left: true, key: false } */ -function addValueToObject(object, val) {} +function addValueToObject(object, val) { + object.val = val + return object +} /** * addKeyToObject() @@ -103,7 +122,11 @@ function addValueToObject(object, val) {} * addKeyToObject({ right: false }, "left"); * //> { right: false, left: true } */ -function addKeyToObject(object, key) {} +function addKeyToObject(object, key) { + object[key] = 1 + return object + +} /** * addKeyValuePairToObject() @@ -118,7 +141,10 @@ function addKeyToObject(object, key) {} * addKeyValuePairToObject({ up: true }, "left", false); * //> { up: true, left: false } */ -function addKeyValuePairToObject(object, key, val) {} +function addKeyValuePairToObject(object, key, val) { + object[key] = val + return object +} /** * deleteKeyFromObject() @@ -132,7 +158,10 @@ function addKeyValuePairToObject(object, key, val) {} * deleteKeyFromObject({ left: true, right: false }, "left"); * //> { right: false } */ -function deleteKeyFromObject(object, key) {} +function deleteKeyFromObject(object, key) { + delete object[key] + return object + } // Do not change any code below this line. module.exports = { diff --git a/src/06-loops.js b/src/06-loops.js index 1116cc27..2f0c0200 100644 --- a/src/06-loops.js +++ b/src/06-loops.js @@ -11,7 +11,7 @@ * //> 25 * (3 + 4 + 5 + 6 + 7 = 25) */ -function rangeSum(min, max) {} +function rangeSum(min, max) { } /** * rangeOdd() @@ -25,7 +25,7 @@ function rangeSum(min, max) {} * rangeOdd(10, 15); * //> [ 15, 13, 11 ] */ -function rangeOdd(min, max) {} +function rangeOdd(min, max) { } /** * rangeEveryOther() @@ -39,7 +39,7 @@ function rangeOdd(min, max) {} * rangeEveryOther(11, 18); * //> [ 11, 13, 15, 17 ] */ -function rangeEveryOther(min, max) {} +function rangeEveryOther(min, max) { } /** * containsWhileLoop() @@ -61,7 +61,7 @@ function rangeEveryOther(min, max) {} * containsWhileLoop([ "left", "up", "right" ], "down"); * //> false */ -function containsWhileLoop(array, target) {} +function containsWhileLoop(array, target) { } /** * containsForLoop() @@ -83,7 +83,14 @@ function containsWhileLoop(array, target) {} * containsForLoop([ "left", "up", "right" ], "down"); * //> false */ -function containsForLoop(array, target) {} +function containsForLoop(array, target) { + for (let i = 0; i <= array.length; i++) { + if (array[i] === target) { + return true + } + }return false +} + /** * targetCount() @@ -97,7 +104,7 @@ function containsForLoop(array, target) {} * targetCount([ 10, 20, 10, 20, 30 ], 10); * //> 2 */ -function targetCount(array, target) {} +function targetCount(array, target) { } /** * firstIndexFound() @@ -117,7 +124,7 @@ function targetCount(array, target) {} * firstIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function firstIndexFound(array, target) {} +function firstIndexFound(array, target) { } /** * lastIndexFound() @@ -137,7 +144,7 @@ function firstIndexFound(array, target) {} * lastIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function lastIndexFound(array, target) {} +function lastIndexFound(array, target) { } /** * timesIndex() @@ -151,7 +158,7 @@ function lastIndexFound(array, target) {} * //> [ 7 * 0, 10 * 1, 11 * 2 ] * //> [ 0, 10, 22 ] */ -function timesIndex(array) {} +function timesIndex(array) { } /** * cumulativeSum() @@ -165,7 +172,7 @@ function timesIndex(array) {} * //> [ 5, 5 + 2, 5 + 2 + 9 ] * //> [ 5, 7, 16 ] */ -function cumulativeSum(array) {} +function cumulativeSum(array) { } // Do not change anything below this line. module.exports = { From b8fcd1c27e063a777f9930bfa209d7132f0f935e Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Sat, 22 Oct 2022 23:42:55 -0400 Subject: [PATCH 5/9] finished loop --- __tests__/07-even-more-loops.test.js | 2 +- src/05-objects.js | 2 +- src/06-loops.js | 96 ++++++++++++++++++++++++---- src/07-even-more-loops.js | 7 +- 4 files changed, 92 insertions(+), 15 deletions(-) diff --git a/__tests__/07-even-more-loops.test.js b/__tests__/07-even-more-loops.test.js index becd856a..6bdba0af 100644 --- a/__tests__/07-even-more-loops.test.js +++ b/__tests__/07-even-more-loops.test.js @@ -11,7 +11,7 @@ const { removeDuplicates, } = require("../src/07-even-more-loops"); -describe("shoutForLoop", () => { +describe.only("shoutForLoop", () => { test("adds an ! to the end of each element", () => { expect(shoutForLoop([])).toEqual([]); expect(shoutForLoop(["I", "am", "a", "happy", "array"])).toEqual([ diff --git a/src/05-objects.js b/src/05-objects.js index 87daaab7..b7bb2fe0 100644 --- a/src/05-objects.js +++ b/src/05-objects.js @@ -142,7 +142,7 @@ function addKeyToObject(object, key) { * //> { up: true, left: false } */ function addKeyValuePairToObject(object, key, val) { - object[key] = val + object[key]=val return object } diff --git a/src/06-loops.js b/src/06-loops.js index 2f0c0200..eafb4c29 100644 --- a/src/06-loops.js +++ b/src/06-loops.js @@ -11,7 +11,17 @@ * //> 25 * (3 + 4 + 5 + 6 + 7 = 25) */ -function rangeSum(min, max) { } + +// add numbers between min and max inclusive +// return sum + +function rangeSum(min, max) { + let sum = 0 + for (let i = min; i <= max; i++) { + sum += i + } + return sum +} /** * rangeOdd() @@ -25,7 +35,22 @@ function rangeSum(min, max) { } * rangeOdd(10, 15); * //> [ 15, 13, 11 ] */ -function rangeOdd(min, max) { } + +// created odd arr variable +// loop over given nums +// check if it is odd +// add odd to arr +//return arr + +function rangeOdd(min, max) { + let odd = [] + for (let i = max; i >= min; i--) { + if (i % 2 !== 0) { + odd.push(i) + } + } + return odd +} /** * rangeEveryOther() @@ -39,7 +64,13 @@ function rangeOdd(min, max) { } * rangeEveryOther(11, 18); * //> [ 11, 13, 15, 17 ] */ -function rangeEveryOther(min, max) { } +function rangeEveryOther(min, max) { + let arr = [] + for (let i = min; i <= max; i += 2) { + arr.push(i) + } + return arr +} /** * containsWhileLoop() @@ -61,7 +92,16 @@ function rangeEveryOther(min, max) { } * containsWhileLoop([ "left", "up", "right" ], "down"); * //> false */ -function containsWhileLoop(array, target) { } +function containsWhileLoop(array, target) { + let i = 0 + while (i < array.length) { + if (array[i] === target) { + return true + } i++; + } return false +} + + /** * containsForLoop() @@ -84,12 +124,12 @@ function containsWhileLoop(array, target) { } * //> false */ function containsForLoop(array, target) { - for (let i = 0; i <= array.length; i++) { + for (let i = 0; i < array.length; i++) { if (array[i] === target) { return true } - }return false -} + } return false +} /** @@ -104,7 +144,13 @@ function containsForLoop(array, target) { * targetCount([ 10, 20, 10, 20, 30 ], 10); * //> 2 */ -function targetCount(array, target) { } +function targetCount(array, target) { + let counter = 0 + for (let i = 0; i < array.length; i++) + if (array[i] === target) { + counter += 1 + } return counter +} /** * firstIndexFound() @@ -124,7 +170,15 @@ function targetCount(array, target) { } * firstIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function firstIndexFound(array, target) { } +function firstIndexFound(array, target) { + for (let i = 0; i < array.length; i++) { + if (array[i] === target) { + return i + } + } return -1 +} + + /** * lastIndexFound() @@ -144,7 +198,13 @@ function firstIndexFound(array, target) { } * lastIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function lastIndexFound(array, target) { } +function lastIndexFound(array, target) { + for (let i = array.length - 1; i > 0; i--) { + if (array[i] === target) { + return i + } + } return -1 +} /** * timesIndex() @@ -158,7 +218,12 @@ function lastIndexFound(array, target) { } * //> [ 7 * 0, 10 * 1, 11 * 2 ] * //> [ 0, 10, 22 ] */ -function timesIndex(array) { } +function timesIndex(array) { + let newArr = [] + for (let i = 0; i < array.length; i++) { + newArr.push(array[i] * i) + } return newArr +} /** * cumulativeSum() @@ -172,7 +237,14 @@ function timesIndex(array) { } * //> [ 5, 5 + 2, 5 + 2 + 9 ] * //> [ 5, 7, 16 ] */ -function cumulativeSum(array) { } +function cumulativeSum(array) { + let newArr = [] + let sum = 0 + for (let i = 0; i < array.length; i++) { + newArr.push(sum += array[i]) + } + return newArr +} // Do not change anything below this line. module.exports = { diff --git a/src/07-even-more-loops.js b/src/07-even-more-loops.js index 250fde4a..a63e351e 100644 --- a/src/07-even-more-loops.js +++ b/src/07-even-more-loops.js @@ -12,7 +12,12 @@ * shoutForLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutForLoop(array) {} +function shoutForLoop(array) { + for(let i = 0; i <= array.length; i++){ + return array + "!" + } + +} /** * shoutWhileLoop() From f9e7f8bb1ae74d5879deb31eac64bcacba5d9f30 Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Sun, 23 Oct 2022 10:04:50 -0400 Subject: [PATCH 6/9] finished even more loops --- __tests__/07-even-more-loops.test.js | 2 +- src/07-even-more-loops.js | 78 +++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/__tests__/07-even-more-loops.test.js b/__tests__/07-even-more-loops.test.js index 6bdba0af..becd856a 100644 --- a/__tests__/07-even-more-loops.test.js +++ b/__tests__/07-even-more-loops.test.js @@ -11,7 +11,7 @@ const { removeDuplicates, } = require("../src/07-even-more-loops"); -describe.only("shoutForLoop", () => { +describe("shoutForLoop", () => { test("adds an ! to the end of each element", () => { expect(shoutForLoop([])).toEqual([]); expect(shoutForLoop(["I", "am", "a", "happy", "array"])).toEqual([ diff --git a/src/07-even-more-loops.js b/src/07-even-more-loops.js index a63e351e..5f7e8c38 100644 --- a/src/07-even-more-loops.js +++ b/src/07-even-more-loops.js @@ -13,10 +13,10 @@ * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ function shoutForLoop(array) { - for(let i = 0; i <= array.length; i++){ - return array + "!" - } - + let newArr = [] + for (let i = 0; i < array.length; i++) { + newArr.push(array[i] + "!") + } return newArr } /** @@ -33,7 +33,14 @@ function shoutForLoop(array) { * shoutWhileLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutWhileLoop(array) {} +function shoutWhileLoop(array) { + let i = 0 + let newArr = [] + while (i < array.length) { + newArr.push(array[i] + "!") + i++; + } return newArr +} /** * shoutForOfLoop() @@ -49,7 +56,12 @@ function shoutWhileLoop(array) {} * shoutForOfLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutForOfLoop(array) {} +function shoutForOfLoop(array) { + let newArr = [] + for (element of array) { + newArr.push(element + "!") + } return newArr +} /** * sumArray() @@ -62,7 +74,12 @@ function shoutForOfLoop(array) {} * sumArray([ 10, 0, 10, 11 ]); * //> 31 */ -function sumArray(array) {} +function sumArray(array) { + let sum = 0 + for (let i = 0; i < array.length; i++) { + sum += array[i] + } return sum +} /** * oddArray() @@ -75,7 +92,14 @@ function sumArray(array) {} * oddArray([ 11, 15, 20, 22, 37 ]); * //> [ 11, 15, 37 ] */ -function oddArray(array) {} +function oddArray(array) { + let newArr = [] + for (let i = 0; i < array.length; i++) { + if (array[i] % 2 !== 0) { + newArr.push(array[i]) + } + } return newArr +} /** * evenArray() @@ -88,7 +112,14 @@ function oddArray(array) {} * evenArray([ 11, 15, 20, 22, 37 ]); * //> [ 20, 22 ] */ -function evenArray(array) {} +function evenArray(array) { + let newArr = [] + for (let i = 0; i < array.length; i++) { + if (array[i] % 2 === 0) { + newArr.push(array[i]) + } + } return newArr +} /** * findSmallest() @@ -101,7 +132,10 @@ function evenArray(array) {} * findSmallest([ 0, 11, -2, 5 ]); * //> -2 */ -function findSmallest(array) {} +function findSmallest(array) { + for (let i = 0; i < array.length; i++) { + } return Math.min(...array) +} /** * findLargest() @@ -114,7 +148,10 @@ function findSmallest(array) {} * findLargest([ 0, 11, -2, 5 ]); * //> 11 */ -function findLargest(array) {} +function findLargest(array) { + for (let i = 0; i < array.length; i++) { + } return Math.max(...array) +} /** * findEqual() @@ -131,8 +168,14 @@ function findLargest(array) {} * findEqual([ 0, 11, -2, 5 ], 9); * //> false */ -function findEqual(array, selected) {} - +function findEqual(array, selected) { + let arrEqual = [] + for (let i = 0; i < array.length; i++) { + if (array[i] === selected) { + return true + } + } return false +} /** * removeDuplicates() * --------------------- @@ -148,7 +191,14 @@ function findEqual(array, selected) {} * //> [ 1, 11, 2, 3, 4, 9 ] */ -function removeDuplicates(array) {} +function removeDuplicates(array) { + let newArr = [] + for (let i = 0; i < array.length; i++) { + if (!newArr.includes(array[i])) { + newArr.push(array[i]) + } + } return newArr +} // Do not change any code below this line. module.exports = { From b268408b59a061f26c02c9ffb625c51ff3e5e3e5 Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Sun, 23 Oct 2022 11:04:56 -0400 Subject: [PATCH 7/9] finished accessing --- src/08-accessing.js | 55 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/src/08-accessing.js b/src/08-accessing.js index 079908aa..f4c1b9f1 100644 --- a/src/08-accessing.js +++ b/src/08-accessing.js @@ -32,7 +32,9 @@ * getFirstName(person); * //> "Rachel" */ -function getFirstName(person) {} +function getFirstName(person) { + return person.names.first +} /** * getLastName() @@ -45,7 +47,9 @@ function getFirstName(person) {} * getLastName(person); * //> "Rojas" */ -function getLastName(person) {} +function getLastName(person) { + return person.names.last +} /** * getAddressStreet() @@ -58,7 +62,9 @@ function getLastName(person) {} * getAddressStreet(person); * //> "697 Pine Drive" */ -function getAddressStreet(person) {} +function getAddressStreet(person) { + return person.address.street +} /** * getCountOfPhoneNumbers() @@ -71,7 +77,12 @@ function getAddressStreet(person) {} * getCountOfPhoneNumbers(person); * //> 2 */ -function getCountOfPhoneNumbers(person) {} +function getCountOfPhoneNumbers(person) { + let counter = 0 + for (let i = 0; i < person.numbers.length; i++) { + counter += 1 + } return counter +} /** * getFirstPhoneNumber() @@ -87,7 +98,9 @@ function getCountOfPhoneNumbers(person) {} * getFirstPhoneNumber(person); * //> 7185550921 */ -function getFirstPhoneNumber(person) {} +function getFirstPhoneNumber(person) { + return person.numbers[0] +} /** * getLastPhoneNumber() @@ -103,7 +116,9 @@ function getFirstPhoneNumber(person) {} * getLastPhoneNumber(person); * //> 7185558611 */ -function getLastPhoneNumber(person) {} +function getLastPhoneNumber(person) { + return person.numbers[person.numbers.length-1] + } /** * getFullName() @@ -116,7 +131,9 @@ function getLastPhoneNumber(person) {} * getFullName(person); * //> "Rachel Eleanor Rojas" */ -function getFullName(person) {} +function getFullName(person) { + return `${person.names.first} ${person.names.middle} ${person.names.last}` +} /** * getCityAndState() @@ -132,7 +149,9 @@ function getFullName(person) {} * getCityAndState(person); * //> "Staten Island, NY" */ -function getCityAndState(person) {} +function getCityAndState(person) { + return `${person.address.city}, ${person.address.state}` + } /** * getFullAddress() @@ -148,7 +167,9 @@ function getCityAndState(person) {} * getFullAddress(person); * //> "697 Pine Drive 2A, Staten Island, NY, 10306" */ -function getFullAddress(person) {} +function getFullAddress(person) { + return `${person.address.street} ${person.address.unit}, ${person.address.city}, ${person.address.state}, ${person.address.zip}` +} /** * getFlatObject() @@ -171,8 +192,20 @@ function getFullAddress(person) {} numbers: [7185550921, 7185558611], }; */ -function getFlatObject(person) {} - +function getFlatObject(person) { + let flatObj= { + first: person.names.first, + middle: person.names.middle, + last: person.names.last, + street: person.address.street, + unit: person.address.unit, + city: person.address.city, + state: person.address.state, + zip: person.address.zip, + numbers: person.numbers +} +return flatObj +} // Do not change the code below. module.exports = { getFirstName, From 9743073000631b071e3307bdf67b6c2170b4bf6c Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Sun, 23 Oct 2022 13:28:06 -0400 Subject: [PATCH 8/9] working of word problems --- __tests__/09-word-problems.test.js | 2 +- src/09-word-problems.js | 34 +++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/__tests__/09-word-problems.test.js b/__tests__/09-word-problems.test.js index 64d4ee5d..d48b9459 100644 --- a/__tests__/09-word-problems.test.js +++ b/__tests__/09-word-problems.test.js @@ -72,7 +72,7 @@ describe("getCartTotal()", () => { }); }); -describe("compareLocations()", () => { +describe.only("compareLocations()", () => { test("should say that the addresses are not near each other if there is no matching component", () => { const address1 = { street: "8785 Trenton St.", diff --git a/src/09-word-problems.js b/src/09-word-problems.js index 2f63d723..4449eede 100644 --- a/src/09-word-problems.js +++ b/src/09-word-problems.js @@ -19,7 +19,19 @@ * applyDiscount(1000, 9, true); * //> 700 */ -function applyDiscount(priceInCents, age, hasMembership) {} +function applyDiscount(priceInCents, age, hasMembership) { + let total = 0; + if ((age <= 10 && hasMembership) || (age >= 65 && hasMembership)) { + total = priceInCents - priceInCents * 0.3; + } else if (hasMembership) { + total = priceInCents - priceInCents * 0.2; + } else if (age <= 10 || age >= 65) { + total = priceInCents - priceInCents * 0.1; + } else { + total = priceInCents; + } + return total; +} /** * getCartTotal() @@ -40,7 +52,13 @@ function applyDiscount(priceInCents, age, hasMembership) {} getCartTotal(cart); * //> "$30.00" */ -function getCartTotal(products) {} +function getCartTotal(products) { + let total = 0; + for (let i = 0; i < products.length; i++) { + total += products[i].priceInCents * products[i].quantity * 0.01; + } + return `$${total.toFixed(2)}`; +} /** * compareLocations() @@ -80,7 +98,17 @@ function getCartTotal(products) {} compareLocations(address1, address2); //> "Same city." */ -function compareLocations(address1, address2) {} +function compareLocations(address1, address2) { + // if (`${address1.street} ${address1.city} ${address1.state} ${address1.zip} === ${address2.street} ${address2.city} ${address2.state} ${address2.zip}`) { + // return "Same building."; + // } else if (`${address1.city} ${address1.state} ${address1.zip} === ${address2.city} ${address2.state} ${address2.zip}`) { + // return "Same city."; + // } else if (`${address1.state} === ${address2.state}`) { + // return "Same state."; + // } else { + // } + // return "Addresses are not near each other."; +} /** * gradeAssignments() From 80cef5d6cdb2ebf757a912f83ae9024b81bfbd1b Mon Sep 17 00:00:00 2001 From: MaxWattan Date: Sun, 23 Oct 2022 21:08:02 -0400 Subject: [PATCH 9/9] finished skill sharpening --- __tests__/09-word-problems.test.js | 2 +- src/06-loops.js | 3 +- src/09-word-problems.js | 84 ++++++++++++++++++++++++++---- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/__tests__/09-word-problems.test.js b/__tests__/09-word-problems.test.js index d48b9459..64d4ee5d 100644 --- a/__tests__/09-word-problems.test.js +++ b/__tests__/09-word-problems.test.js @@ -72,7 +72,7 @@ describe("getCartTotal()", () => { }); }); -describe.only("compareLocations()", () => { +describe("compareLocations()", () => { test("should say that the addresses are not near each other if there is no matching component", () => { const address1 = { street: "8785 Trenton St.", diff --git a/src/06-loops.js b/src/06-loops.js index eafb4c29..a79a17c7 100644 --- a/src/06-loops.js +++ b/src/06-loops.js @@ -203,7 +203,8 @@ function lastIndexFound(array, target) { if (array[i] === target) { return i } - } return -1 + } + return -1 } /** diff --git a/src/09-word-problems.js b/src/09-word-problems.js index 4449eede..7751711a 100644 --- a/src/09-word-problems.js +++ b/src/09-word-problems.js @@ -99,16 +99,48 @@ function getCartTotal(products) { //> "Same city." */ function compareLocations(address1, address2) { - // if (`${address1.street} ${address1.city} ${address1.state} ${address1.zip} === ${address2.street} ${address2.city} ${address2.state} ${address2.zip}`) { - // return "Same building."; - // } else if (`${address1.city} ${address1.state} ${address1.zip} === ${address2.city} ${address2.state} ${address2.zip}`) { - // return "Same city."; - // } else if (`${address1.state} === ${address2.state}`) { - // return "Same state."; - // } else { - // } - // return "Addresses are not near each other."; + if ( + address1.street === address2.street && + address1.city === address2.city && + address1.state === address2.state && + address1.zip === address2.zip + ) { + return "Same building."; + } else if ( + address1.city === address2.city && + address1.state === address2.state && + address1.zip === address2.zip + ) { + return "Same city."; + } else if (address1.state === address2.state) { + return "Same state."; + } else { + return "Addresses are not near each other."; + } } +// if ( +// address1.street && +// address1.city && +// address1.state && +// address1.zip === address2.street && +// address2.city && +// address2.state && +// address2.zip +// ) { +// return "Same building."; +// } else if ( +// address1.city && +// address1.state && +// address1.zip === address2.city && +// address2.state && +// address2.zip +// ) { +// return "Same city."; +// } else if (address1.state === address2.state) { +// return "Same state."; +// } else { +// } +// return "Addresses are not near each other."; /** * gradeAssignments() @@ -155,7 +187,26 @@ function compareLocations(address1, address2) { //> }, //> ]; */ -function gradeAssignments(assignments) {} +function gradeAssignments(assignments) { + for (let i = 0; i < assignments.length; i++) { + if (assignments[i].kind === `PASS-FAIL`) { + assignments[i].score.received === assignments[i].score.max + ? (assignments[i]["status"] = `PASSED`) + : (assignments[i]["status"] = `FAILED`); + } else if (assignments[i].kind === `PERCENTAGE`) { + let percentage = + (assignments[i].score.received / assignments[i].score.max) * 100; + percentage >= 80 + ? (assignments[i]["status"] = `PASSED: ${percentage.toFixed(1)}%`) + : (assignments[i]["status"] = `FAILED: ${percentage.toFixed(1)}%`); + } else { + assignments[i][ + "status" + ] = `SCORE: ${assignments[i].score.received}/${assignments[i].score.max}`; + } + } + return assignments; +} /** * createLineOrder() @@ -180,7 +231,18 @@ function gradeAssignments(assignments) {} createLineOrder(people); //> [ "Ray Anderson", "America Marsh", "Wade Carson", "Patience Patel" ] */ -function createLineOrder(people) {} +function createLineOrder(people) { + let memberArray = []; + let notMemberArray = []; + for (let i = 0; i < people.length; i++) { + if (people[i].hasMembership) { + memberArray.push(people[i].name); + } else { + notMemberArray.push(people[i].name); + } + } + return memberArray.concat(notMemberArray); +} module.exports = { applyDiscount,