diff --git a/package-lock.json b/package-lock.json index d72a0a81..ccf2ff2a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1308,14 +1308,20 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001244", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001244.tgz", - "integrity": "sha512-Wb4UFZPkPoJoKKVfELPWytRzpemjP/s0pe22NriANru1NoI+5bGNxzKtk7edYL8rmCWTfQO8eRiF0pn1Dqzx7Q==", - "dev": true, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/browserslist" - } + "version": "1.0.30001422", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001422.tgz", + "integrity": "sha512-hSesn02u1QacQHhaxl/kNMZwqVG35Sz/8DgvmgedxSH8z9UUpcDYSPYgsj3x5dQNRcNp6BwpSfQfVzYUTm+fog==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/caniuse-lite" + } + ] }, "node_modules/chalk": { "version": "4.1.1", @@ -4980,9 +4986,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001244", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001244.tgz", - "integrity": "sha512-Wb4UFZPkPoJoKKVfELPWytRzpemjP/s0pe22NriANru1NoI+5bGNxzKtk7edYL8rmCWTfQO8eRiF0pn1Dqzx7Q==", + "version": "1.0.30001422", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001422.tgz", + "integrity": "sha512-hSesn02u1QacQHhaxl/kNMZwqVG35Sz/8DgvmgedxSH8z9UUpcDYSPYgsj3x5dQNRcNp6BwpSfQfVzYUTm+fog==", "dev": true }, "chalk": { diff --git a/src/01-functions.js b/src/01-functions.js index 8d772b6a..fa4b88cd 100644 --- a/src/01-functions.js +++ b/src/01-functions.js @@ -9,7 +9,9 @@ * alwaysTrue(); * //> true */ -function alwaysTrue() {} +function alwaysTrue() { +return (true)// logs boolean true +} /** * greet() @@ -24,7 +26,9 @@ function alwaysTrue() {} * greet("Ian"); * //> "Hello, Ian." */ -function greet() {} +function greet(name) { +return `Hello, ${name}.`//return the string Hello, and the string variable name using ``string interpolation +} /** * add() @@ -39,7 +43,10 @@ function greet() {} * add(10, 20); * //> 30 */ -function add() {} +function add(a , b) { + number = (a + b)//intiated variable assigned to the sum of number a & number b + return(number)// return the number variable +} /** * multiply() @@ -54,7 +61,10 @@ function add() {} * multiply(2, 5); * //> 10 */ -function multiply() {} +function multiply( a, b) { + number = a*b//intiated variable assigned to the product of number a & number b + return(number)// return the number variable +} /** * average() @@ -74,7 +84,10 @@ function multiply() {} * average(10, 6); * //> 8 // 10 + 6 = 16; 16/2 = 8; */ -function average() {} +function average(a , b) { + number = ((a+b)/2)//intiated variable assigned to the sum of number a & number b divided by 2 + return(number)//return the number variable +} /** * averageThree() @@ -93,7 +106,10 @@ function average() {} * averageThree(10, 11, 19); * //> 10 // 10 + 11 + 19 = 30; 30 / 3 = 10; */ -function averageThree() {} +function averageThree(a,b,c) { + number =((a + b + c)/3)//intiated variable assigned to the sum of number a,number b, & number c divided by 3 + return(number)// return the number variable +} /** * compareTypes() @@ -112,7 +128,12 @@ function averageThree() {} * compareTypes("left", 10); * //> false */ -function compareTypes() {} +function compareTypes(a,b) { + if (typeof(a) === typeof(b)){//if the data type of a strictly equal the data type of b + return(true)//return the boolean true + }else//otherwise + return(false)//return the boolean false +} /** * flipSign() @@ -131,7 +152,10 @@ function compareTypes() {} * flipSign(50); * //> -50 */ -function flipSign() {} +function flipSign(a) { + number = (a*(-1))//intiate variable number assigned to the product of variable a & -1 + return(number)//returns number variable +} /** * joinStringsWithSpaces() @@ -151,7 +175,11 @@ 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 + //`${a} ${b} ${c} ${d} ${e}` + //easier solution +} /** * getFirstAndLastCharacter() @@ -168,7 +196,10 @@ function joinStringsWithSpaces() {} * getFirstAndLastCharacter("upwards"); * //> "us" */ -function getFirstAndLastCharacter() {} +function getFirstAndLastCharacter(a) { + return((a.charAt(0)+(a.charAt(a.length - 1)))) + //return character at index 0 of string variable a plus the character at the last index of the string variable a +} // Do not change the code below. module.exports = { diff --git a/src/02-data-types.js b/src/02-data-types.js index 79cf96cf..73db67b0 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 ~~val +} /** * convertToString() @@ -36,7 +40,9 @@ function convertToNumber(val) {} * convertToString(99); * //> "99" */ -function convertToString(val) {} +function convertToString(val) { + return(val.toString()) +} /** * convertToShoutingText() @@ -49,7 +55,9 @@ function convertToString(val) {} * convertToShoutingText("Hello There"); * //> "HELLO THERE" */ -function convertToShoutingText(text) {} +function convertToShoutingText(text) { + return text.toUpperCase(text) +} /** * convertToWhisperText() @@ -62,7 +70,9 @@ function convertToShoutingText(text) {} * convertToWhisperText("Hello There"); * //> "hello there" */ -function convertToWhisperText(text) {} +function convertToWhisperText(text) { + return text.toLowerCase(text) +} /** * checkIfCharacterIsInString() @@ -79,7 +89,9 @@ function convertToWhisperText(text) {} * checkIfCharacterIsInString("hello there", "a"); * //> false */ -function checkIfCharacterIsInString(text, character) {} +function checkIfCharacterIsInString(text, character) { + return text.includes(character) +} /** * isEven() @@ -95,7 +107,12 @@ 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 +128,12 @@ function isEven(num) {} * isOdd(11); * //> true */ -function isOdd(num) {} +function isOdd(num) { + if ((num % 2) !== 0){ + return true +} else +return false +} /** * isTruthy() @@ -127,7 +149,12 @@ function isOdd(num) {} * isTruthy(null); * //> false */ -function isTruthy(val) {} +function isTruthy(val) { + if (!!val){ + return true + }else + return false +} /** * isFalsy() @@ -143,7 +170,12 @@ 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 = { diff --git a/src/03-control-flow.js b/src/03-control-flow.js index 8f675935..246e4b39 100644 --- a/src/03-control-flow.js +++ b/src/03-control-flow.js @@ -14,7 +14,12 @@ * isEqual(10, "10"); * //> false */ -function isEqual(a, b) {} +function isEqual(a, b) { + if (a === b){ + return true + }else + return false +} /** * findLarger() @@ -28,7 +33,12 @@ 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 +55,14 @@ function findLarger(a, b) {} * findLargerOrTie(0, 0); * //> "tie" */ -function findLargerOrTie(a, b) {} +function findLargerOrTie(a, b) { +if(a == b){ + return("tie") +}else if(a > b){ + return a +}else +return b +} /** * positiveNegativeOrZero() @@ -65,7 +82,14 @@ function findLargerOrTie(a, b) {} * positiveNegativeOrZero(1); * //> "Positive" */ -function positiveNegativeOrZero(a) {} +function positiveNegativeOrZero(a) { + if(a === 0){ + return "Zero" + }else if (a < 0){ + return "Negative" + }else if (a > 0) + return "Positive" +} /** * aroundTheWorldGreeting() @@ -86,7 +110,14 @@ 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,8 +140,18 @@ function aroundTheWorldGreeting(language) {} * aroundTheWorldGreetingWithSwitch(); * //> "Hello World" */ -function aroundTheWorldGreetingWithSwitch(language) {} - +function aroundTheWorldGreetingWithSwitch(language) { + switch(language){ + case "spanish": + return "Hola Mundo"; + break; + case "german": + return "Hallo Welt"; + break; + default: + return "Hello World" +} +} /** * calculateLetterGrade() * --------------------- @@ -128,7 +169,18 @@ function aroundTheWorldGreetingWithSwitch(language) {} * calculateLetterGrade(99); * //> "A" */ -function calculateLetterGrade(grade) {} +function calculateLetterGrade(grade) { + if(grade >= 90){ + return "A" + }else if(grade < 90 && grade >= 80){ + return "B" + }else if(grade < 80 && grade >= 70){ + return "C" + }else if (grade < 70 && grade >= 60){ + return "D" + }else + return "F" +} /** * animalCounts() @@ -146,7 +198,12 @@ function calculateLetterGrade(grade) {} * animalCounts("pig", 1); * //> "1 pig" */ -function animalCounts(animal, numberOfAnimals) {} +function animalCounts(animal, numberOfAnimals) { + if (numberOfAnimals === 1){ + return `${numberOfAnimals} ${animal}` + }else + return `${numberOfAnimals} ${animal}s` +} /** * evenOrOdd() @@ -163,7 +220,12 @@ function animalCounts(animal, numberOfAnimals) {} * evenOrOdd(48); * //> "Even" */ -function evenOrOdd(a) {} +function evenOrOdd(a) { + if ((a% 2) !== 0){ + return "Odd" +} else +return "Even" +} /** * evenOrOddWithTernary() @@ -179,7 +241,9 @@ function evenOrOdd(a) {} * evenOrOddWithTernary(8); * //> "Even" */ -function evenOrOddWithTernary(a) {} +function evenOrOddWithTernary(a) { + return ((a% 2) !== 0) ? "Odd": "Even"; +} // Do not change any code below this line. module.exports = { diff --git a/src/04-arrays.js b/src/04-arrays.js index 5317f7c3..d91b5a3f 100644 --- a/src/04-arrays.js +++ b/src/04-arrays.js @@ -10,7 +10,10 @@ * createEmptyArray(); * //> [] */ -function createEmptyArray() {} +function createEmptyArray() { + let emptyArray =[] + return emptyArray +} /** * createArrayWithTwoElements() @@ -24,7 +27,11 @@ function createEmptyArray() {} * createArrayWithTwoElements(true, false); * //> [ true, false ] */ -function createArrayWithTwoElements(a, b) {} +function createArrayWithTwoElements(a, b) { + let newArray = [] + newArray.push(a,b) + return newArray +} /** * getArrayLength() @@ -37,7 +44,9 @@ function createArrayWithTwoElements(a, b) {} * getArrayLength([ 10, 20, 30 ]); * //> 3 */ -function getArrayLength(array) {} +function getArrayLength(array) { +return array.length +} /** * getFirstElementOfArray() @@ -51,7 +60,9 @@ function getArrayLength(array) {} * getFirstElementOfArray([ 10, 20, 30 ]); * //> 10 */ -function getFirstElementOfArray(array) {} +function getFirstElementOfArray(array) { + return array[0] +} /** * getLastElementOfArray() @@ -65,7 +76,9 @@ function getFirstElementOfArray(array) {} * getLastElementOfArray([ null, undefined ]); * //> undefined */ -function getLastElementOfArray(array) {} +function getLastElementOfArray(array) { + return array[array.length -1] +} /** * addElementToEndOfArray() @@ -79,7 +92,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 +108,9 @@ function addElementToEndOfArray(array, element) {} * removeElementFromEndOfArray([ 10, 9, 8 ]); * //> 8 */ -function removeElementFromEndOfArray(array) {} +function removeElementFromEndOfArray(array) { + return array.pop() +} /** * addElementToFrontOfArray() @@ -106,7 +124,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 +140,9 @@ function addElementToFrontOfArray(array, element) {} * removeElementFromFrontOfArray([ 10, 9, 8 ]); * //> 10 */ -function removeElementFromFrontOfArray(array) {} +function removeElementFromFrontOfArray(array) { + return array.shift() +} /** * getMiddleElement() @@ -135,7 +158,10 @@ function removeElementFromFrontOfArray(array) {} * getMiddleElement([ 10, null, "30" ]); * //> null */ -function getMiddleElement(array) {} +function getMiddleElement(array) { + // + return array[Math.round((array.length - 1) / 2)] +} // Do not change any code below this line. module.exports = { diff --git a/src/05-objects.js b/src/05-objects.js index 3e97313c..5aa3dd43 100644 --- a/src/05-objects.js +++ b/src/05-objects.js @@ -9,7 +9,10 @@ * createEmptyObject() * //> {} */ -function createEmptyObject() {} +function createEmptyObject() { + oblivion = {} + return oblivion +} /** * createObjectWithValue() @@ -22,7 +25,12 @@ function createEmptyObject() {} * createObjectWithValue(19); * //> { whateverKey: 19 } */ -function createObjectWithValue(val) {} +function createObjectWithValue(val) { + let obj ={} + obj = { + key : val} + return obj +} /** * createObjectWithKey() @@ -35,7 +43,11 @@ function createObjectWithValue(val) {} * createObjectWithKey("left"); * //> { left: false } */ -function createObjectWithKey(key) {} +function createObjectWithKey(key) { + obj= {} + obj[key] = key + return obj +} /** * createObjectWithKeyValuePair() @@ -49,7 +61,11 @@ function createObjectWithKey(key) {} * createObjectWithKeyValuePair("left", "right"); * //> { left: "right" } */ -function createObjectWithKeyValuePair(key, val) {} +function createObjectWithKeyValuePair(key, val) { + let obj ={} + obj[key] = val + return (obj) +} /** * getNameFromObject() @@ -62,7 +78,9 @@ function createObjectWithKeyValuePair(key, val) {} * getNameFromObject({ name: "Jay" }); * //> "Jay" */ -function getNameFromObject(object) {} +function getNameFromObject(object) { + return object.name +} /** * getAgeFromObject() @@ -75,7 +93,9 @@ function getNameFromObject(object) {} * getAgeFromObject({ age: 30 }); * //> 30 */ -function getAgeFromObject(object) {} +function getAgeFromObject(object) { + return object.age +} /** * addValueToObject() @@ -89,7 +109,11 @@ function getAgeFromObject(object) {} * addValueToObject({ left: true }, false); * //> { left: true, key: false } */ -function addValueToObject(object, val) {} +function addValueToObject(object, val) { + object = { + key : val} + return object +} /** * addKeyToObject() @@ -103,7 +127,11 @@ function addValueToObject(object, val) {} * addKeyToObject({ right: false }, "left"); * //> { right: false, left: true } */ -function addKeyToObject(object, key) {} +function addKeyToObject(object, key) { + + object[key] = key + return object +} /** * addKeyValuePairToObject() @@ -118,7 +146,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 +163,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..f5ec7e8a 100644 --- a/src/06-loops.js +++ b/src/06-loops.js @@ -11,7 +11,14 @@ * //> 25 * (3 + 4 + 5 + 6 + 7 = 25) */ -function rangeSum(min, max) {} +function rangeSum(min, max) { + let sum = 0// initate variable sum; assigns sum to interger 0 + for ( i = min; i <= max; i++){// initiate for loop index equal min int as long as index is less than max int;(i = i+1) + sum += i;//sum = sum + the index of intergers within the range of min int and max int + } + return sum// sum of all intergers with in the range of given numbers +} + /** * rangeOdd() @@ -25,7 +32,14 @@ function rangeSum(min, max) {} * rangeOdd(10, 15); * //> [ 15, 13, 11 ] */ -function rangeOdd(min, max) {} +function rangeOdd(min, max) { + let odd =[]// intiate variable "odd" assigning it to an empty array + for ( i = max; i >= min; i --){// initiate for loop index equal max int as long as index is greater than min int;(i = i-1)>0 + if (i % 2 !== 0)//if the remainder index divided by 2 is not equal to 0 + odd.push(i)//insert the indexes that fit the condition into the odd array + } +return odd//return array of odd numbers +} /** * rangeEveryOther() @@ -39,7 +53,15 @@ function rangeOdd(min, max) {} * rangeEveryOther(11, 18); * //> [ 11, 13, 15, 17 ] */ -function rangeEveryOther(min, max) {} +function rangeEveryOther(min, max) { + let other= []//intiate variable "other" assigning it to an empty array + for(i = min; i <= max; i+= 2){//for loop initiated;index set to min less than equal to max; index set to increment by 2 + other.push(i)//insert the indexes that fit the condition into the odd array + } + return other// returns an array in increasing order of every other element + + } + /** * containsWhileLoop() @@ -61,7 +83,15 @@ function rangeEveryOther(min, max) {} * containsWhileLoop([ "left", "up", "right" ], "down"); * //> false */ -function containsWhileLoop(array, target) {} +function containsWhileLoop(array, target) { +i = array.length;// assign i to the index of the [] array +while(i--){// while (i = i - 1) > 0, run until condition is false + if (array[i] === target){//if array index is stricly equal to the array element target + return true;// return boolean true + }//ends if statement + }//ends while loop + return false;// return boolean false +}//ends function /** * containsForLoop() @@ -83,7 +113,15 @@ function containsWhileLoop(array, target) {} * containsForLoop([ "left", "up", "right" ], "down"); * //> false */ -function containsForLoop(array, target) {} +function containsForLoop(array, target) { + for (i=0 ;i < array.length; i++ ){//intiate for loop;if index 0 less than length of array increment by 1 + if (array[i] === target){//if array index is stricly equal to the array element target + return true;// return boolean true + }//ends if statement + }//ends for loop + return false;// return boolean false + }//ends function + /** * targetCount() @@ -97,7 +135,15 @@ function containsForLoop(array, target) {} * targetCount([ 10, 20, 10, 20, 30 ], 10); * //> 2 */ -function targetCount(array, target) {} +function targetCount(array, target) { + let counter = 0; + for (array[target] of array){ + if (array[target] == target){ + counter++; + } + } + return counter +} /** * firstIndexFound() @@ -117,7 +163,13 @@ function targetCount(array, target) {} * firstIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function firstIndexFound(array, target) {} +function firstIndexFound(array, target) { +for(i=0;i <= array.length; i++){ + if (array[i] === target){ + return i + } +}return -1 +} /** * lastIndexFound() @@ -137,7 +189,13 @@ function firstIndexFound(array, target) {} * lastIndexFound([ "left", "right", "left" ], "up"); * //> -1 */ -function lastIndexFound(array, target) {} +function lastIndexFound(array, target) { + for(i= array.length -1 ;i >= 0; i--){//decrement + if (array[i] === target){ + return i + } + }return -1 +} /** * timesIndex() @@ -151,7 +209,15 @@ function lastIndexFound(array, target) {} * //> [ 7 * 0, 10 * 1, 11 * 2 ] * //> [ 0, 10, 22 ] */ -function timesIndex(array) {} +function timesIndex(array) { + timesArray = [] + for (i =0; i < array.length; i++){ + + timesArray.push(array[i]*i) + } + return timesArray +} + /** * cumulativeSum() @@ -165,7 +231,16 @@ function timesIndex(array) {} * //> [ 5, 5 + 2, 5 + 2 + 9 ] * //> [ 5, 7, 16 ] */ -function cumulativeSum(array) {} +function cumulativeSum(array) { + let timesArray = [] + let sum = 0 + for (i = 0; i < array.length; i++){ + sum = sum + array[i] + // timesArray.push(i += array[array.length + 1]) + timesArray.push(sum) + } + return timesArray +} // 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..1d1e3e87 100644 --- a/src/07-even-more-loops.js +++ b/src/07-even-more-loops.js @@ -12,7 +12,15 @@ * shoutForLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutForLoop(array) {} +function shoutForLoop(array) { + let shoutArray = []; + let excite; + for(i =0; i < array.length; i++){ + excite = `${array[i]}!` + shoutArray.push(excite) + } + return shoutArray +} /** * shoutWhileLoop() @@ -28,7 +36,17 @@ function shoutForLoop(array) {} * shoutWhileLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutWhileLoop(array) {} +function shoutWhileLoop(array) { + let shoutArray = []; + let excite; + i = 0 + while(i < array.length){ + excite = `${array[i]}!` + shoutArray.push(excite) + i++ + } + return shoutArray +} /** * shoutForOfLoop() @@ -44,7 +62,12 @@ function shoutWhileLoop(array) {} * shoutForOfLoop([ "A", "Very", "Happy", "Array" ]); * //> [ "A!", "Very!", "Happy!", "Array!" ]; */ -function shoutForOfLoop(array) {} +function shoutForOfLoop(array) { +for (i of array){//initate for of loop i is the index of array + array[array.indexOf(i)] = `${array[array.indexOf(i)]}!`//added ! through string interpolation + } + return array +} /** * sumArray() @@ -57,7 +80,16 @@ function shoutForOfLoop(array) {} * sumArray([ 10, 0, 10, 11 ]); * //> 31 */ -function sumArray(array) {} +function sumArray(array) { + //variable sum declared and assigned the value 0 + //variable i is assigned to the length of the array numbers +let sum = 0, i = array.length; +// while loop will run until i hit 0 +//i-- represents that if (i = i - 1) > 0, run until condition is false +while (i--) + sum += array[i] +return (sum) +} /** * oddArray() @@ -70,7 +102,14 @@ function sumArray(array) {} * oddArray([ 11, 15, 20, 22, 37 ]); * //> [ 11, 15, 37 ] */ -function oddArray(array) {} +function oddArray(array) { + let odd= [] + for (i=0; i < array.length; i++){ + if ( array[i] % 2 !== 0) + odd.push(array[i]) + } + return (odd) +} /** * evenArray() @@ -83,7 +122,14 @@ function oddArray(array) {} * evenArray([ 11, 15, 20, 22, 37 ]); * //> [ 20, 22 ] */ -function evenArray(array) {} +function evenArray(array) { + let even = [] + for (i=0; i < array.length; i++){ + if ( array[i] % 2 === 0) + even.push(array[i]) + } + return (even) +} /** * findSmallest() @@ -96,7 +142,10 @@ function evenArray(array) {} * findSmallest([ 0, 11, -2, 5 ]); * //> -2 */ -function findSmallest(array) {} +function findSmallest(array) { + array.sort(); + return array[0] +} /** * findLargest() @@ -109,7 +158,10 @@ function findSmallest(array) {} * findLargest([ 0, 11, -2, 5 ]); * //> 11 */ -function findLargest(array) {} +function findLargest(array) { + array.sort(); + return array[array.length -1] +} /** * findEqual() @@ -126,7 +178,15 @@ function findLargest(array) {} * findEqual([ 0, 11, -2, 5 ], 9); * //> false */ -function findEqual(array, selected) {} +function findEqual(array, selected) { + for (i=0 ;i < array.length; i++ ){//intiate for loop;if index 0 less than length of array increment by 1 + if (array[i] === selected){//if array index is stricly equal to the array element target + return true;// return boolean true + }//ends if statement + }//ends for loop + return false;// return boolean false + }//ends function + /** * removeDuplicates() @@ -143,7 +203,15 @@ function findEqual(array, selected) {} * //> [ 1, 11, 2, 3, 4, 9 ] */ -function removeDuplicates(array) {} +function removeDuplicates(array) { + let duplicates = []; + for(i=0;i < array.length; i++){ + if (!duplicates.includes(array[i])) {//if array does not include the index of array + duplicates.push(array[i]);//push excluded indexs tothe duplicate array + } + } + return duplicates +} // Do not change any code below this line. module.exports = { diff --git a/src/08-accessing.js b/src/08-accessing.js index 079908aa..87418c0d 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,10 @@ function getAddressStreet(person) {} * getCountOfPhoneNumbers(person); * //> 2 */ -function getCountOfPhoneNumbers(person) {} +function getCountOfPhoneNumbers(person) { + return person.numbers.length + +} /** * getFirstPhoneNumber() @@ -87,8 +96,9 @@ function getCountOfPhoneNumbers(person) {} * getFirstPhoneNumber(person); * //> 7185550921 */ -function getFirstPhoneNumber(person) {} - +function getFirstPhoneNumber(person) { +return person.numbers[0] +} /** * getLastPhoneNumber() * --------------------- @@ -103,7 +113,9 @@ function getFirstPhoneNumber(person) {} * getLastPhoneNumber(person); * //> 7185558611 */ -function getLastPhoneNumber(person) {} +function getLastPhoneNumber(person) { + return person.numbers[person.numbers.length - 1] +} /** * getFullName() @@ -116,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() @@ -132,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() @@ -148,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() @@ -171,7 +189,29 @@ function getFullAddress(person) {} numbers: [7185550921, 7185558611], }; */ -function getFlatObject(person) {} +function getFlatObject(person) { + //create a new object + let newList = {} + + //set its values equal to the original stores values + + newList.first = person.names.first + newList.middle = person.names.middle + newList.last = person.names.last + + + //make copy of the objects key:value + newList.street = person.address.street + newList.unit = person.address.unit + newList.city = person.address.city + newList.state = person.address.state + newList.zip = person.address.zip +newList.numbers =[] +for(let i =0; i< person.numbers.length; i++){ + newList.numbers[i] = person.numbers[i] +} + return newList +} // Do not change the code below. module.exports = { diff --git a/src/09-word-problems.js b/src/09-word-problems.js index 2f63d723..f90fc446 100644 --- a/src/09-word-problems.js +++ b/src/09-word-problems.js @@ -19,7 +19,23 @@ * applyDiscount(1000, 9, true); * //> 700 */ -function applyDiscount(priceInCents, age, hasMembership) {} +function applyDiscount(priceInCents, age, hasMembership) { + let total = priceInCents + if (age <= 10 && (!hasMembership)){ + total = priceInCents - ((10/100)*priceInCents) + return total + }else if ((age <= 10) || (age >= 65) && (hasMembership)){ + total = priceInCents- ((30/100)*priceInCents) + return total; + } else if ((age >= 65) && (!hasMembership)){ + total = priceInCents - ((10/100)*priceInCents) + return total; + } else if (hasMembership){ + total = priceInCents - ((20/100)*priceInCents) + return total; + } else + return total +} /** * getCartTotal() @@ -40,7 +56,14 @@ function applyDiscount(priceInCents, age, hasMembership) {} getCartTotal(cart); * //> "$30.00" */ -function getCartTotal(products) {} +function getCartTotal(products) { + let totalCost = 0; + for (let i = 0; i < products.length; i++) { + totalCost += products[i].priceInCents * products[i].quantity +} + return `$${(totalCost/100).toFixed(2)}`// $___.__ + + } /** * compareLocations() @@ -80,7 +103,16 @@ function getCartTotal(products) {} compareLocations(address1, address2); //> "Same city." */ -function compareLocations(address1, address2) {} +function compareLocations(address1, address2) { + if (address1.state !== address2.state){ +return "Addresses are not near each other." + }else if ((address1.city,address1.state,address1.zip) === (address2.city,address2.state,address2.zip) && (address1.street !== address2.street)){ + return "Same city." + }else if ((address1.street,address1.city,address1.state,address1.zip) === (address2.street,address2.city,address2.state,address2.zip)){ + return "Same building." + }else (address1.state === address2.state) + return "Same state." +} /** * gradeAssignments() @@ -127,8 +159,23 @@ function compareLocations(address1, address2) {} //> }, //> ]; */ -function gradeAssignments(assignments) {} - +function gradeAssignments(assignments) { + for(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 + (assignments[i]["status"] = "FAILED") + }else if(assignments[i].kind === "PERCENTAGE"){ + if(assignments[i].score.received/assignments[i].score.max >= 0.8){ + (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 if(assignments[i].kind === "ESSAY"){ + assignments[i]["status"] = (`SCORE: ${assignments[i].score.received}/${assignments[i].score.max}`); + } +}return assignments +} /** * createLineOrder() * --------------------- @@ -152,7 +199,22 @@ 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,