From 8e615e20fc9f2c7c0db469be5b2e82737ea82fb8 Mon Sep 17 00:00:00 2001 From: JohnnyMCR Date: Tue, 18 Oct 2022 22:03:28 -0400 Subject: [PATCH 1/5] SkillSharpening --- src/01-functions.js | 42 ++++++++++++++++++++++++++++++++---------- src/02-data-types.js | 25 +++++++++++++++++++------ 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/01-functions.js b/src/01-functions.js index 8d772b6a..1d42fb5b 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,11 @@ 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 +147,9 @@ function compareTypes() {} * flipSign(50); * //> -50 */ -function flipSign() {} +function flipSign(a) { + return -a +} /** * joinStringsWithSpaces() @@ -151,7 +169,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 +188,9 @@ function joinStringsWithSpaces() {} * getFirstAndLastCharacter("upwards"); * //> "us" */ -function getFirstAndLastCharacter() {} +function getFirstAndLastCharacter(a) { + return `${a.charAt(0)}${a.charAt(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..c81985b2 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 parseInt(val) +} /** * convertToString() @@ -36,7 +40,9 @@ function convertToNumber(val) {} * convertToString(99); * //> "99" */ -function convertToString(val) {} +function convertToString(val) { + return String(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,10 @@ function convertToWhisperText(text) {} * checkIfCharacterIsInString("hello there", "a"); * //> false */ -function checkIfCharacterIsInString(text, character) {} +function checkIfCharacterIsInString(text, character) { + if (text !== character) + console.log +} /** * isEven() From 742f78815f35597607d97d46bdcdc231f67a01e8 Mon Sep 17 00:00:00 2001 From: JohnnyMCR Date: Wed, 19 Oct 2022 21:30:01 -0400 Subject: [PATCH 2/5] JavaMachine --- src/02-data-types.js | 37 +++++++++++++++++++++++++++------- src/03-control-flow.js | 45 +++++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/02-data-types.js b/src/02-data-types.js index c81985b2..74f0a13f 100644 --- a/src/02-data-types.js +++ b/src/02-data-types.js @@ -90,9 +90,12 @@ function convertToWhisperText(text) { * //> false */ function checkIfCharacterIsInString(text, character) { - if (text !== character) - console.log -} + // if (text === character){ + // return true + // } else if (text !== character) + // return false + return text.includes(character) +} /** * isEven() @@ -108,7 +111,9 @@ function checkIfCharacterIsInString(text, character) { * isEven(11); * //> false */ -function isEven(num) {} +function isEven(num) { + return (num % 2 === 0) +} /** * isOdd() @@ -124,7 +129,13 @@ function isEven(num) {} * isOdd(11); * //> true */ -function isOdd(num) {} +function isOdd(num) { + if (num % 2 === 0) { + return false; + } else { + return true; + } +} /** * isTruthy() @@ -140,7 +151,13 @@ function isOdd(num) {} * isTruthy(null); * //> false */ -function isTruthy(val) {} +function isTruthy(val) { + if (!!val == false){ + return false; + } else { + return true; + } +} /** * isFalsy() @@ -156,7 +173,13 @@ function isTruthy(val) {} * isFalsy(" "); * //> false */ -function isFalsy(val) {} +function isFalsy(val) { + if (!val == true){ + return true; + } else { + return false; + } +} // Do not change the code below. module.exports = { diff --git a/src/03-control-flow.js b/src/03-control-flow.js index 8f675935..8a8f5f24 100644 --- a/src/03-control-flow.js +++ b/src/03-control-flow.js @@ -14,7 +14,9 @@ * isEqual(10, "10"); * //> false */ -function isEqual(a, b) {} +function isEqual(a, b) { + return a ===b +} /** * findLarger() @@ -28,7 +30,9 @@ function isEqual(a, b) {} * findLarger(19, 7); * //> 19 */ -function findLarger(a, b) {} +function findLarger(a, b) { + return Math.max(a , b) +} /** * findLargerOrTie() @@ -45,7 +49,15 @@ function findLarger(a, b) {} * findLargerOrTie(0, 0); * //> "tie" */ -function findLargerOrTie(a, b) {} +function findLargerOrTie(a, b) { + if (Math.max(a) > Math.max(b)) { + return a + } else if(Math.max(a) < Math.max(b)) { + return b + } else { + return "tie" + } +} /** * positiveNegativeOrZero() @@ -65,7 +77,16 @@ function findLargerOrTie(a, b) {} * positiveNegativeOrZero(1); * //> "Positive" */ -function positiveNegativeOrZero(a) {} +function positiveNegativeOrZero(a) { + let i = 0 + if (i > a) { + return "Negative" + } else if(i < a) { + return "Positive" + } else { + return "Zero" + } +} /** * aroundTheWorldGreeting() @@ -86,7 +107,13 @@ function positiveNegativeOrZero(a) {} * aroundTheWorldGreeting(); * //> "Hello World" */ -function aroundTheWorldGreeting(language) {} +function aroundTheWorldGreeting(language) { + if (language == `spanish`){ + return `Hola Mundo` + } else if(language == `german`){ + return `Hallo Welt` + } else return `Hello World` +} /** * aroundTheWorldGreetingWithSwitch() @@ -109,7 +136,9 @@ function aroundTheWorldGreeting(language) {} * aroundTheWorldGreetingWithSwitch(); * //> "Hello World" */ -function aroundTheWorldGreetingWithSwitch(language) {} +function aroundTheWorldGreetingWithSwitch(language) { + +} /** * calculateLetterGrade() @@ -179,7 +208,9 @@ 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 473cb205fdd86e72421df8a5c17ae5ddd65950d8 Mon Sep 17 00:00:00 2001 From: JohnnyMCR Date: Sun, 23 Oct 2022 08:57:22 -0400 Subject: [PATCH 3/5] JavaMachine update --- src/03-control-flow.js | 4 ++-- src/04-arrays.js | 36 +++++++++++++++++++++++++++--------- src/05-objects.js | 6 ++++-- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/03-control-flow.js b/src/03-control-flow.js index 8a8f5f24..2f31dc0c 100644 --- a/src/03-control-flow.js +++ b/src/03-control-flow.js @@ -86,7 +86,7 @@ function positiveNegativeOrZero(a) { } else { return "Zero" } -} +} /** * aroundTheWorldGreeting() @@ -137,7 +137,7 @@ function aroundTheWorldGreeting(language) { * //> "Hello World" */ function aroundTheWorldGreetingWithSwitch(language) { - + } /** diff --git a/src/04-arrays.js b/src/04-arrays.js index 5317f7c3..47e987e7 100644 --- a/src/04-arrays.js +++ b/src/04-arrays.js @@ -10,7 +10,9 @@ * createEmptyArray(); * //> [] */ -function createEmptyArray() {} +function createEmptyArray() { + return [].concat([]) +} /** * createArrayWithTwoElements() @@ -24,7 +26,9 @@ function createEmptyArray() {} * createArrayWithTwoElements(true, false); * //> [ true, false ] */ -function createArrayWithTwoElements(a, b) {} +function createArrayWithTwoElements(a, b) { + return [a].concat(b) +} /** * getArrayLength() @@ -37,7 +41,9 @@ function createArrayWithTwoElements(a, b) {} * getArrayLength([ 10, 20, 30 ]); * //> 3 */ -function getArrayLength(array) {} +function getArrayLength(array) { + return array.length +} /** * getFirstElementOfArray() @@ -51,7 +57,9 @@ function getArrayLength(array) {} * getFirstElementOfArray([ 10, 20, 30 ]); * //> 10 */ -function getFirstElementOfArray(array) {} +function getFirstElementOfArray(array) { + return array.shift() +} /** * getLastElementOfArray() @@ -65,7 +73,9 @@ function getFirstElementOfArray(array) {} * getLastElementOfArray([ null, undefined ]); * //> undefined */ -function getLastElementOfArray(array) {} +function getLastElementOfArray(array) { + return array.pop() +} /** * addElementToEndOfArray() @@ -79,7 +89,9 @@ function getLastElementOfArray(array) {} * addElementToEndOfArray([ 10 ], 9); * //> [ 10, 9 ] */ -function addElementToEndOfArray(array, element) {} +function addElementToEndOfArray(array, element) { + // return push.element() +} /** * removeElementFromEndOfArray() @@ -92,7 +104,9 @@ function addElementToEndOfArray(array, element) {} * removeElementFromEndOfArray([ 10, 9, 8 ]); * //> 8 */ -function removeElementFromEndOfArray(array) {} +function removeElementFromEndOfArray(array) { + return array.pop() +} /** * addElementToFrontOfArray() @@ -119,7 +133,9 @@ function addElementToFrontOfArray(array, element) {} * removeElementFromFrontOfArray([ 10, 9, 8 ]); * //> 10 */ -function removeElementFromFrontOfArray(array) {} +function removeElementFromFrontOfArray(array) { + return array.shift() +} /** * getMiddleElement() @@ -135,7 +151,9 @@ function removeElementFromFrontOfArray(array) {} * getMiddleElement([ 10, null, "30" ]); * //> null */ -function getMiddleElement(array) {} +function getMiddleElement(array) { + return array.Math.floor() +} // Do not change any code below this line. module.exports = { diff --git a/src/05-objects.js b/src/05-objects.js index 3e97313c..040ad8e7 100644 --- a/src/05-objects.js +++ b/src/05-objects.js @@ -9,7 +9,7 @@ * createEmptyObject() * //> {} */ -function createEmptyObject() {} +function createEmptyObject(object) {} /** * createObjectWithValue() @@ -22,7 +22,9 @@ function createEmptyObject() {} * createObjectWithValue(19); * //> { whateverKey: 19 } */ -function createObjectWithValue(val) {} +function createObjectWithValue(val) { + return object.value.object +} /** * createObjectWithKey() From 23f90d34e04b80824ea047d786b7c0677f6dfbb7 Mon Sep 17 00:00:00 2001 From: JohnnyMCR Date: Sun, 23 Oct 2022 11:15:14 -0400 Subject: [PATCH 4/5] JavaMachine updated --- src/03-control-flow.js | 38 +++++++++++++++-- src/05-objects.js | 42 ++++++++++++++----- src/06-loops.js | 92 +++++++++++++++++++++++++++++++++++++----- src/08-accessing.js | 4 +- 4 files changed, 152 insertions(+), 24 deletions(-) diff --git a/src/03-control-flow.js b/src/03-control-flow.js index 2f31dc0c..b7fc0a08 100644 --- a/src/03-control-flow.js +++ b/src/03-control-flow.js @@ -137,7 +137,19 @@ function aroundTheWorldGreeting(language) { * //> "Hello World" */ function aroundTheWorldGreetingWithSwitch(language) { + let myDefault = "Hello World" + switch (language){ + case "english": + myDefault = "Hello World"; + break; + case "spanish": + myDefault = "Hola Mundo"; + break; + case "german": + myDefault = "Hallo Welt"; + break; + } return myDefault } /** @@ -157,7 +169,19 @@ function aroundTheWorldGreetingWithSwitch(language) { * calculateLetterGrade(99); * //> "A" */ -function calculateLetterGrade(grade) {} +function calculateLetterGrade(grade) { + if (grade >= 90){ + return "A" + } else if (grade >= 80){ + return "B" + } else if (grade >= 70){ + return "C" + } else if (grade >= 60){ + return "D" + } else if (grade >= 60){ + return "E" + } else return "F" +} /** * animalCounts() @@ -175,7 +199,11 @@ 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() @@ -192,7 +220,11 @@ function animalCounts(animal, numberOfAnimals) {} * evenOrOdd(48); * //> "Even" */ -function evenOrOdd(a) {} +function evenOrOdd(a) { + if (a % 2 === 0){ + return `Even` + } else return "Odd" +} /** * evenOrOddWithTernary() diff --git a/src/05-objects.js b/src/05-objects.js index 040ad8e7..c9a3d409 100644 --- a/src/05-objects.js +++ b/src/05-objects.js @@ -9,7 +9,9 @@ * createEmptyObject() * //> {} */ -function createEmptyObject(object) {} +function createEmptyObject(object) { + return {} +} /** * createObjectWithValue() @@ -23,7 +25,7 @@ function createEmptyObject(object) {} * //> { whateverKey: 19 } */ function createObjectWithValue(val) { - return object.value.object + return {any:val} } /** @@ -37,7 +39,9 @@ function createObjectWithValue(val) { * createObjectWithKey("left"); * //> { left: false } */ -function createObjectWithKey(key) {} +function createObjectWithKey(key) { + return {[key] : true} +} /** * createObjectWithKeyValuePair() @@ -51,7 +55,9 @@ function createObjectWithKey(key) {} * createObjectWithKeyValuePair("left", "right"); * //> { left: "right" } */ -function createObjectWithKeyValuePair(key, val) {} +function createObjectWithKeyValuePair(key, val) { + return {[key] : val} +} /** * getNameFromObject() @@ -64,7 +70,9 @@ function createObjectWithKeyValuePair(key, val) {} * getNameFromObject({ name: "Jay" }); * //> "Jay" */ -function getNameFromObject(object) {} +function getNameFromObject(object) { + return object.name +} /** * getAgeFromObject() @@ -77,7 +85,9 @@ function getNameFromObject(object) {} * getAgeFromObject({ age: 30 }); * //> 30 */ -function getAgeFromObject(object) {} +function getAgeFromObject(object) { + return object.age +} /** * addValueToObject() @@ -91,7 +101,10 @@ function getAgeFromObject(object) {} * addValueToObject({ left: true }, false); * //> { left: true, key: false } */ -function addValueToObject(object, val) {} +function addValueToObject(object, val) { + object[`newValue`] = val + return object +} /** * addKeyToObject() @@ -105,7 +118,10 @@ function addValueToObject(object, val) {} * addKeyToObject({ right: false }, "left"); * //> { right: false, left: true } */ -function addKeyToObject(object, key) {} +function addKeyToObject(object, key) { + object[key] = true + return object +} /** * addKeyValuePairToObject() @@ -120,7 +136,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() @@ -134,7 +153,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..bc07be31 100644 --- a/src/06-loops.js +++ b/src/06-loops.js @@ -11,7 +11,12 @@ * //> 25 * (3 + 4 + 5 + 6 + 7 = 25) */ -function rangeSum(min, max) {} +function rangeSum(min, max) { + let addedSum = 0 + for(let i = min; i <= max; i++){ + addedSum = i + addedSum + } return addedSum +} /** * rangeOdd() @@ -25,7 +30,14 @@ function rangeSum(min, max) {} * rangeOdd(10, 15); * //> [ 15, 13, 11 ] */ -function rangeOdd(min, max) {} +function rangeOdd(min, max) { + let newArray = [] + for (let i = max; i >= min; i --){ + if (i % 2 === 1){ + newArray.push(i) + } + } return newArray +} /** * rangeEveryOther() @@ -39,7 +51,18 @@ function rangeOdd(min, max) {} * rangeEveryOther(11, 18); * //> [ 11, 13, 15, 17 ] */ -function rangeEveryOther(min, max) {} +function rangeEveryOther(min, max) { + let wastedNumber = [] + let startArray = [] + + for (let i = min; i <= max; i ++){ + if (i === min){ + startArray.push(i) + } else if (startArray.length != wastedNumber.length){ + wastedNumber.push(i) + } else startArray.push(i) + } return startArray +} /** * containsWhileLoop() @@ -61,7 +84,14 @@ 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 + } else i++ + } return false +} /** * containsForLoop() @@ -83,7 +113,13 @@ 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 +133,14 @@ function containsForLoop(array, target) {} * targetCount([ 10, 20, 10, 20, 30 ], 10); * //> 2 */ -function targetCount(array, target) {} +function targetCount(array, target) { + let targetCounts = 0 + for (let i = 0; i < array.length; i++){ + if (array[i] === target){ + targetCounts ++ + } + } return targetCounts +} /** * firstIndexFound() @@ -117,7 +160,15 @@ function targetCount(array, target) {} * firstIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function firstIndexFound(array, target) {} +function firstIndexFound(array, target) { + let firstIndex = 0 + for (let i = 0; i < array.length; i++){ + if (array[i] === target){ + firstIndex = i + return firstIndex + } + } return -1 +} /** * lastIndexFound() @@ -137,7 +188,15 @@ function firstIndexFound(array, target) {} * lastIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function lastIndexFound(array, target) {} +function lastIndexFound(array, target) { + let lastIndex = 0 + for (let i = array.length; i > 0; i--){ + if (array[i] === target){ + lastIndex = i + return lastIndex + } + } return -1 +} /** * timesIndex() @@ -151,7 +210,12 @@ function lastIndexFound(array, target) {} * //> [ 7 * 0, 10 * 1, 11 * 2 ] * //> [ 0, 10, 22 ] */ -function timesIndex(array) {} +function timesIndex(array) { + multipliedIndexArr = [] + for (let i = 0; i < array.length; i ++){ + multipliedIndexArr.push(array[i] * i) + } return multipliedIndexArr +} /** * cumulativeSum() @@ -165,7 +229,15 @@ function timesIndex(array) {} * //> [ 5, 5 + 2, 5 + 2 + 9 ] * //> [ 5, 7, 16 ] */ -function cumulativeSum(array) {} +function cumulativeSum(array) { + newArr = [] + newNum = 0 + for (let i = 0; i < array.length; i++){ + newNum += array[i] + newArr.push(newNum) + } + return newArr +} // Do not change anything below this line. module.exports = { diff --git a/src/08-accessing.js b/src/08-accessing.js index 079908aa..1544d075 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.index(0) +} /** * getLastName() From 1ed4201a48cfcd229ebd4d1ddcc91624dd1682e9 Mon Sep 17 00:00:00 2001 From: JohnnyMCR Date: Sun, 23 Oct 2022 12:46:59 -0400 Subject: [PATCH 5/5] JavaMachine completed --- src/04-arrays.js | 14 ++++--- src/07-even-more-loops.js | 79 +++++++++++++++++++++++++++++++++------ src/08-accessing.js | 50 ++++++++++++++++++++----- src/09-word-problems.js | 60 ++++++++++++++++++++++++++--- 4 files changed, 172 insertions(+), 31 deletions(-) diff --git a/src/04-arrays.js b/src/04-arrays.js index 47e987e7..32678149 100644 --- a/src/04-arrays.js +++ b/src/04-arrays.js @@ -58,7 +58,7 @@ function getArrayLength(array) { * //> 10 */ function getFirstElementOfArray(array) { - return array.shift() + return array[0] } /** @@ -74,7 +74,7 @@ function getFirstElementOfArray(array) { * //> undefined */ function getLastElementOfArray(array) { - return array.pop() + return array[array.length -1] } /** @@ -90,7 +90,8 @@ function getLastElementOfArray(array) { * //> [ 10, 9 ] */ function addElementToEndOfArray(array, element) { - // return push.element() + array.push(element) + return array } /** @@ -120,7 +121,10 @@ function removeElementFromEndOfArray(array) { * addElementToFrontOfArray([ 10 ], 9); * //> [ 9, 10 ] */ -function addElementToFrontOfArray(array, element) {} +function addElementToFrontOfArray(array, element) { + array.unshift(element); + return array +} /** * removeElementFromFrontOfArray() @@ -152,7 +156,7 @@ function removeElementFromFrontOfArray(array) { * //> null */ function getMiddleElement(array) { - return array.Math.floor() + return array[(Math.floor(array.length/2))] } // Do not change any code below this line. diff --git a/src/07-even-more-loops.js b/src/07-even-more-loops.js index 250fde4a..c132a169 100644 --- a/src/07-even-more-loops.js +++ b/src/07-even-more-loops.js @@ -12,7 +12,11 @@ * shoutForLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutForLoop(array) {} +function shoutForLoop(array) { + for (let i = 0; i < array.length; i++){ + array[i] += "!" + } return array +} /** * shoutWhileLoop() @@ -28,7 +32,13 @@ function shoutForLoop(array) {} * shoutWhileLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutWhileLoop(array) {} +function shoutWhileLoop(array) { + let i = 0 + while(i < array.length){ + array[i] += "!" + i++ + } return array +} /** * shoutForOfLoop() @@ -44,7 +54,13 @@ function shoutWhileLoop(array) {} * shoutForOfLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutForOfLoop(array) {} +function shoutForOfLoop(array) { + let newArr = [] + for (let value of array){ + value += "!" + newArr.push(value) + } return newArr +} /** * sumArray() @@ -57,7 +73,12 @@ function shoutForOfLoop(array) {} * sumArray([ 10, 0, 10, 11 ]); * //> 31 */ -function sumArray(array) {} +function sumArray(array) { + let total = 0 + for (let i = 0; i < array.length; i ++){ + total += array[i] + } return total +} /** * oddArray() @@ -70,7 +91,14 @@ function sumArray(array) {} * oddArray([ 11, 15, 20, 22, 37 ]); * //> [ 11, 15, 37 ] */ -function oddArray(array) {} +function oddArray(array) { + let oddArr = [] + for (let i = 0; i < array.length; i++){ + if (array[i] % 2 === 1){ + oddArr.push(array[i]) + } + } return oddArr +} /** * evenArray() @@ -83,7 +111,14 @@ function oddArray(array) {} * evenArray([ 11, 15, 20, 22, 37 ]); * //> [ 20, 22 ] */ -function evenArray(array) {} +function evenArray(array) { + let evenArr = [] + for (let i = 0; i < array.length; i ++){ + if (array[i] % 2 === 0){ + evenArr.push(array[i]) + } + } return evenArr +} /** * findSmallest() @@ -96,7 +131,14 @@ function evenArray(array) {} * findSmallest([ 0, 11, -2, 5 ]); * //> -2 */ -function findSmallest(array) {} +function findSmallest(array) { + let smallest = array [0] + for (let i = 0; i < array.length; i ++){ + if (array[i] < smallest){ + smallest = array[i] + } + } return smallest +} /** * findLargest() @@ -109,8 +151,14 @@ function findSmallest(array) {} * findLargest([ 0, 11, -2, 5 ]); * //> 11 */ -function findLargest(array) {} - +function findLargest(array) { + largest = array[0] + for (let i = 0; i < array.length; i++){ + if (array[i] > largest){ + largest = array[i] + } +} return largest +} /** * findEqual() * --------------------- @@ -126,7 +174,9 @@ function findLargest(array) {} * findEqual([ 0, 11, -2, 5 ], 9); * //> false */ -function findEqual(array, selected) {} +function findEqual(array, selected) { + return array.includes(selected) +} /** * removeDuplicates() @@ -143,7 +193,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 = { diff --git a/src/08-accessing.js b/src/08-accessing.js index 1544d075..009c783b 100644 --- a/src/08-accessing.js +++ b/src/08-accessing.js @@ -33,7 +33,7 @@ * //> "Rachel" */ function getFirstName(person) { - return person.index(0) + return person.names.first } /** @@ -47,7 +47,9 @@ function getFirstName(person) { * getLastName(person); * //> "Rojas" */ -function getLastName(person) {} +function getLastName(person) { + return person.names.last +} /** * getAddressStreet() @@ -60,7 +62,9 @@ function getLastName(person) {} * getAddressStreet(person); * //> "697 Pine Drive" */ -function getAddressStreet(person) {} +function getAddressStreet(person) { + return person.address.street +} /** * getCountOfPhoneNumbers() @@ -73,7 +77,9 @@ function getAddressStreet(person) {} * getCountOfPhoneNumbers(person); * //> 2 */ -function getCountOfPhoneNumbers(person) {} +function getCountOfPhoneNumbers(person) { + return person.numbers.length +} /** * getFirstPhoneNumber() @@ -89,7 +95,9 @@ function getCountOfPhoneNumbers(person) {} * getFirstPhoneNumber(person); * //> 7185550921 */ -function getFirstPhoneNumber(person) {} +function getFirstPhoneNumber(person) { + return person.numbers[0] +} /** * getLastPhoneNumber() @@ -105,7 +113,9 @@ function getFirstPhoneNumber(person) {} * getLastPhoneNumber(person); * //> 7185558611 */ -function getLastPhoneNumber(person) {} +function getLastPhoneNumber(person) { + return person.numbers[person.numbers.length -1] +} /** * getFullName() @@ -118,7 +128,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() @@ -134,7 +146,9 @@ function getFullName(person) {} * getCityAndState(person); * //> "Staten Island, NY" */ -function getCityAndState(person) {} +function getCityAndState(person) { + return `${person.address.city}, ${person.address.state}` +} /** * getFullAddress() @@ -150,7 +164,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() @@ -173,7 +189,21 @@ function getFullAddress(person) {} numbers: [7185550921, 7185558611], }; */ -function getFlatObject(person) {} +function getFlatObject(person) { + let newObj = {} + + newObj.first = person.names.first + newObj.middle = person.names.middle + newObj.last = person.names.last + newObj.street = person.address.street + newObj.unit = person.address.unit + newObj.city = person.address.city + newObj.state = person.address.state + newObj.zip = person.address.zip + newObj.numbers = person.numbers + + return newObj +} // Do not change the code below. module.exports = { diff --git a/src/09-word-problems.js b/src/09-word-problems.js index 2f63d723..f3cbb2f7 100644 --- a/src/09-word-problems.js +++ b/src/09-word-problems.js @@ -19,7 +19,16 @@ * applyDiscount(1000, 9, true); * //> 700 */ -function applyDiscount(priceInCents, age, hasMembership) {} +function applyDiscount(priceInCents, age, hasMembership) { + let ageMembership = (age <= 10 || age >= 65) + if (hasMembership && (ageMembership)){ + return priceInCents * .70 + } else if (hasMembership){ + return priceInCents * .80 + } else if (ageMembership){ + return priceInCents * .90 + } else return priceInCents +} /** * getCartTotal() @@ -40,7 +49,12 @@ 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 + } return `$${(total/100).toFixed(2)}` + } /** * compareLocations() @@ -80,7 +94,15 @@ function getCartTotal(products) {} compareLocations(address1, address2); //> "Same city." */ -function compareLocations(address1, address2) {} +function compareLocations(address1, address2) { + 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." +} /** * gradeAssignments() @@ -127,7 +149,22 @@ function compareLocations(address1, address2) {} //> }, //> ]; */ -function gradeAssignments(assignments) {} +function gradeAssignments(assignments) { + for (let i = 0; i < assignments.length; i++){ + if ( assignments[i].kind === 'PASS-FAIL'){ + if (assignments[i].score.received === assignments[i].score.max){ + assignments[i].status = "PASSED" + } else if (assignments[i].score.received !== assignments[i].score.max){ + assignments[i].status = "FAILED" + } + } else if (assignments[i].kind === 'PERCENTAGE'){ + if ((assignments[i].score.received / assignments[i].score.max) >= .80){ + assignments[i].status = `PASSED: ${((assignments[i].score.received / assignments[i].score.max) * 100).toFixed(1)}%` + } else assignments[i].status = `FAILED: ${((assignments[i].score.received / assignments[i].score.max) * 100).toFixed(1)}%` + } else assignments[i].status = `SCORE: ${assignments[i].score.received}/${assignments[i].score.max}` + } + return assignments + } /** * createLineOrder() @@ -152,7 +189,20 @@ function gradeAssignments(assignments) {} createLineOrder(people); //> [ "Ray Anderson", "America Marsh", "Wade Carson", "Patience Patel" ] */ -function createLineOrder(people) {} +function createLineOrder(people) { + memberArr = [] + + for (let i = 0; i < people.length; i++){ + if (people[i].hasMembership === true){ + memberArr.push(people[i].name) + } + } + for (let j = 0; j < people.length; j++){ + if (people[j].hasMembership === false){ + memberArr.push(people[j].name) + } + } return memberArr +} module.exports = { applyDiscount,