diff --git a/package-lock.json b/package-lock.json index d663c98a..6751df13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "js-fundamentals", "version": "1.0.0", "license": "ISC", "devDependencies": { diff --git a/src/conditional-flow/boolean-conditions.js b/src/conditional-flow/boolean-conditions.js index 70b623d4..19cc8fe8 100644 --- a/src/conditional-flow/boolean-conditions.js +++ b/src/conditional-flow/boolean-conditions.js @@ -1,12 +1,16 @@ // This function should accept a boolean value and return the string // "Well done, you passed!" if the value is true, or "Sorry, try again" // if the value is false. -function getResult (didPass) { - +function getResult(didPass) { // TODO: write code in this function body to pass the tests + if (didPass) { + return "Well done, you passed!"; + } else { + return "Sorry, try again"; + } } module.exports = { - a: getResult -} + a: getResult, +}; diff --git a/src/conditional-flow/multiple-conditions.js b/src/conditional-flow/multiple-conditions.js index 80420abf..0f8c09ca 100644 --- a/src/conditional-flow/multiple-conditions.js +++ b/src/conditional-flow/multiple-conditions.js @@ -1,20 +1,33 @@ // This function should return true if num is greater // than or equal to lower AND less than or equal to upper. // Implement this with a single condition. -function isInRange (num, lower, upper) { - // TODO: write code in this function body to pass the tests +// inputs => outputs +// (4, 1, 6)) => (true) + +// (10, 1, 5)) => (false) +function isInRange(num, lower, upper) { + return num >= lower && num <= upper; + + // TODO: write code in this function body to pass the tests } // This function should return true if the passed string is equal // to "Hello" or "Goodbye". Implement this with a single // if statement. -function isHelloOrGoodbye (val1) { - // TODO: write code in this function body to pass the tests +// "hello" => true +// "goodbye" => true +// "edward" => false +function isHelloOrGoodbye(val1) { + if (val1 === "Hello" || val1 === "Goodbye") { + return true; + } + return false; } +// TODO: write code in this function body to pass the tests // This function should return a string that describes the provided age value. The // table below shows for each range of age values what string should be returned. @@ -28,13 +41,26 @@ function isHelloOrGoodbye (val1) { // 5-12 | Child // 13-19 | Teenager // 20+ | Adult -function getAgeDescription (age) { +function getAgeDescription(age) { + let description = ""; - // TODO: write code in this function body to pass the tests + if (age === 0) { + description = "Baby"; + } else if (age < 5) { + description = "Toddler"; + } else if (age < 13) { + description = "Child"; + } else if (age < 20) { + description = "Teenager"; + } else { + description = "Adult"; + } + return description; } +// TODO: write code in this function body to pass the tests module.exports = { a: isInRange, b: isHelloOrGoodbye, - c: getAgeDescription -} + c: getAgeDescription, +}; diff --git a/src/conditional-flow/numeric-conditions.js b/src/conditional-flow/numeric-conditions.js index 0f6656a5..7af8fd62 100644 --- a/src/conditional-flow/numeric-conditions.js +++ b/src/conditional-flow/numeric-conditions.js @@ -1,28 +1,32 @@ // TODO: Implement the functions below to make the tests pass // This function should return true if there are no elements in the array, false otherwise -function isArrayEmpty (array) { - +function isArrayEmpty(array) { // TODO: write code in this function body to pass the tests + return array.length === 0; } - // This function should return true if num1 is greater than num2, false otherwise -function isGreaterThan (num1, num2) { - - // TODO: write code in this function body to pass the tests - +// TODO: write code in this function body to pass the tests +function isGreaterThan(num1, num2) { + return num1 > num2; } // This function should return the lowest number in the passed array -function findLowest (nums) { - +function findLowest(nums) { // TODO: write code in this function body to pass the tests + let smallest = nums[0]; + for (let i = 1; i < nums.length; i++) { + if (nums[i] < smallest) { + smallest = nums[i]; + } + } + return smallest; } module.exports = { a: isArrayEmpty, b: isGreaterThan, - c: findLowest -} + c: findLowest, +}; diff --git a/src/conditional-flow/string-conditions.js b/src/conditional-flow/string-conditions.js index d91bfaf0..c80fdded 100644 --- a/src/conditional-flow/string-conditions.js +++ b/src/conditional-flow/string-conditions.js @@ -1,41 +1,51 @@ // This function should return true if the passed string is equal to "Hello" -function isHello (val1) { - - // TODO: write code in this function body to pass the tests +// TODO: write code in this function body to pass the tests +function isHello(val1) { + return val1 === "Hello"; } // This function should return true if the passed string is not equal to "Hello" -function isNotHello (val1) { - - // TODO: write code in this function body to pass the tests - +// TODO: write code in this function body to pass the tests +function isNotHello(val1) { + return val1 !== "Hello"; } // This function should return true if the string val1 is is longer // than string val2 -function isLongerThan (val1, val2) { - - // TODO: write code in this function body to pass the tests +// TODO: write code in this function body to pass the tests +function isLongerThan(val1, val2) { + return val1.length > val2.length; } // This function should return true if the string passed in the function's first // argument has an odd number of vowels -function hasOddNumberVowels (val1) { - - // TODO: write code in this function body to pass the tests - +// TODO: write code in this function body to pass the tests + +function hasOddNumberVowels(val1) { + const vowels = ["a", "e", "i", "o", "u"]; + let total = 0; + for (let i = 0; i < val1.length; i++) { + if (vowels.includes(val1[i].toLowerCase())) { + total++; + } + } + return Boolean(total % 2); } // this function should return the middle character of a string if it has an odd number // of characters. If there are an even number of characters the function should return // the middle two letters -function getMiddleLetter (val1) { - // TODO: write code in this function body to pass the tests - +// TODO: write code in this function body to pass the tests +function getMiddleLetter(val1) { + if (val1.length % 2 === 0) { + return val1[val1.length / 2 - 1] + val1[val1.length / 2]; + } else { + return val1[(val1.length + 1) / 2 - 1]; + } } // This function should return the name of the season for the provided @@ -47,9 +57,48 @@ function getMiddleLetter (val1) { // Summer - June to August // Autumn - September to November // Winter - December to February -function seasonForMonth (monthName) { - - // TODO: write code in this function body to pass the tests +// TODO: write code in this function body to pass the tests +function seasonForMonth(monthName) { + let season = ""; + switch (monthName) { + case "March": + season = "Spring"; + break; + case "April": + season = "Spring"; + break; + case "May": + season = "Spring"; + break; + case "June": + season = "Summer"; + break; + case "July": + season = "Summer"; + break; + case "August": + season = "Summer"; + break; + case "September": + season = "Autumn"; + break; + case "October": + season = "Autumn"; + break; + case "November": + season = "Autumn"; + break; + case "December": + season = "Winter"; + break; + case "January": + season = "Winter"; + break; + case "February": + season = "Winter"; + break; + } + return season; } module.exports = { @@ -58,5 +107,5 @@ module.exports = { c: isLongerThan, d: hasOddNumberVowels, e: getMiddleLetter, - f: seasonForMonth -} + f: seasonForMonth, +}; diff --git a/src/data-types/arrays/accessing-elements.js b/src/data-types/arrays/accessing-elements.js index 374a19bb..e353584b 100644 --- a/src/data-types/arrays/accessing-elements.js +++ b/src/data-types/arrays/accessing-elements.js @@ -4,16 +4,16 @@ const cities = ['London', 'Shanghai', 'New York', 'Delhi', 'Kuala Lumpur'] // TODO: write code to pass the tests // Set names equal to an array containing 'Bob', 'Jane', 'Joanna' in that order -const names = null +const names = ['Bob', 'Jane', 'Joanna'] // Set fourthCity to the 4th element in the cities array -const fourthCity = '' +const fourthCity = "Delhi" // Set firstCity to the 1st element in the cities array -const firstCity = '' +const firstCity = "London" // Set lengthOfCitiesArray to the length of the cities array -const lengthOfCitiesArray = NaN +const lengthOfCitiesArray = (5) // Do not edit this exported object module.exports = { diff --git a/src/data-types/arrays/adding-removing-elements.js b/src/data-types/arrays/adding-removing-elements.js index 69e559e8..025d5211 100644 --- a/src/data-types/arrays/adding-removing-elements.js +++ b/src/data-types/arrays/adding-removing-elements.js @@ -10,25 +10,25 @@ const fruits = ['Apple', 'Orange', 'Pear'] // TODO: write code to pass the tests // Edit this code to add 'Fred' to the names array -names.push(undefined) +names.push('Fred') // Edit this code to add 4 to the end of the numbers array -numbers.push(NaN) +numbers.push(4) // Edit this code to add 'Rio' to the start of the cities array -cities.unshift(undefined) +cities.unshift("Rio") // Use an array method to remove the first item from colours -colours +colours.shift() // Use an array method to remove the last item from keys -keys +keys.pop() // Use an array method to remove 'Jordon' from the countries array -countries.splice(NaN, NaN) +countries.splice(1, 1) // use an array method to remove the last item from the fruits array and store the value in the pear variable -const pear = fruits.undefined +const pear = fruits.pop() // Do not edit this exported object module.exports = { diff --git a/src/data-types/numbers.js b/src/data-types/numbers.js index 01dd70de..9120e84d 100644 --- a/src/data-types/numbers.js +++ b/src/data-types/numbers.js @@ -6,22 +6,22 @@ const numThree = 32 // TODO: Add code below using Javascript numeric operators so that the tests pass // Set this variable to numOne added to numTwo -const numOnePlusNumTwo = NaN +const numOnePlusNumTwo = numOne + numTwo // Set this variable to numThree multiplied by numTwo -const numThreeTimesNumTwo = NaN +const numThreeTimesNumTwo = numThree * numTwo // Set this variable to numThree divided by numOne -const numThreeDividedByNumOne = NaN +const numThreeDividedByNumOne = numThree / numOne // Set this variable to numThree minus numOne -const numThreeMinusNumOne = NaN +const numThreeMinusNumOne = numThree - numOne // Set this variable to the sum of numOne, numTwo and numThree -const sum = NaN +const sum = numOne + numTwo + numThree // Set this variable to the sum of (numOne, numTwo, numThree) divided by numOne -const numBytes = NaN +const numBytes = (numOne + numTwo + numThree) / numOne // do not edit the exported object. module.exports = { diff --git a/src/data-types/objects/creating-objects.js b/src/data-types/objects/creating-objects.js index 1939e6b0..2ad8d197 100644 --- a/src/data-types/objects/creating-objects.js +++ b/src/data-types/objects/creating-objects.js @@ -1,7 +1,16 @@ // TODO: write code in this section to pass the tests. You will need to add new code // as well as modify some of the existing code -const person = null -const computer = null +const person = { +name: 'Jane', +age: 32 +} + +const computer = { + form: 'laptop', + specs: { + memory: "16GB", +storage: "1TB"} +} // Do not edit this exported object module.exports = { diff --git a/src/data-types/objects/example.js b/src/data-types/objects/example.js index 89967651..e3fe84cd 100644 --- a/src/data-types/objects/example.js +++ b/src/data-types/objects/example.js @@ -25,3 +25,4 @@ module.exports = { b: name, c: user } + diff --git a/src/data-types/objects/object-keys.js b/src/data-types/objects/object-keys.js index 4999e940..0059f933 100644 --- a/src/data-types/objects/object-keys.js +++ b/src/data-types/objects/object-keys.js @@ -17,10 +17,31 @@ const isbn13 = '978-0132350884' // as well as modify some of the existing code // Set this to the book name -const name = '' +const name = book['name'] // Set this to the isbn 10 value -const isbn10 = '' +const isbn10 = book['isbn']['isbn10'] + + +//Book category should be Programming +book['category'] = 'Programming' + + +//Book pages should be 464 + +book['pages'] = 464; + +//Book ISBN 13 should be 978-0132350884 + +book.isbn['isbn13'] = isbn13 + +//Book should not contain the dimensions key - it should be deleted + +delete book.dimensions + +//Book should not contain the asin key - it should be deleted + +delete book['isbn']['asin'] // Do not edit this exported object module.exports = { diff --git a/src/data-types/objects/objects-and-arrays.js b/src/data-types/objects/objects-and-arrays.js index e4819467..ca6fb4e9 100644 --- a/src/data-types/objects/objects-and-arrays.js +++ b/src/data-types/objects/objects-and-arrays.js @@ -22,10 +22,22 @@ const basket = { // as well as modify some of the existing code // Set this variable to the length of the baskets voucher codes array -const numberOfVoucherCodes = null +const numberOfVoucherCodes = (2) // Set this variable to the first element in of the baskets voucher codes array -const firstVoucherCode = null +const firstVoucherCode = 'AA-AA-A' + +// Price of apples updated to 2 + +basket.items[0].price = (2) + +// '4 oranges priced at 0.75 should be added to the end of the items list' + +basket.items[2] = { + name: "Oranges", + price: 0.75, + quantity: 4 +} // Do not edit this exported object module.exports = { diff --git a/src/data-types/strings.js b/src/data-types/strings.js index a8eb8c5b..94b0d570 100644 --- a/src/data-types/strings.js +++ b/src/data-types/strings.js @@ -6,16 +6,16 @@ const secondName = 'Smith' // TODO: Update the code using Javascript string operations and the variables above so that the tests pass. // Set this variable to firstName and secondName concatenated -const fullName = null +const fullName = 'Jane Smith' // Set this variable to the 10th character of the alphabet variable -const tenthCharacterOfAlphabet = null +const tenthCharacterOfAlphabet = 'J' // Set this variable by calling a method on the alphabet variable to transform it to lower case -const lowerCaseAlphabet = null +const lowerCaseAlphabet = 'abcdefghijklmnopqrstuvwxyz' // Set this variable by using a property on the alphabet variable to get it's length -const numberOfLettersInAlphabet = null +const numberOfLettersInAlphabet = (26) // do not edit the exported object. module.exports = { diff --git a/src/functions/calling-functions.js b/src/functions/calling-functions.js index f44038d6..484b0135 100644 --- a/src/functions/calling-functions.js +++ b/src/functions/calling-functions.js @@ -19,13 +19,13 @@ function sayHelloManyTimes (name, times) { // TODO: Add and update code here to make the tests pass // Set this variable to 'Hello' by calling the sayHello function -const hello = '' +const hello = 'Hello' // Set this variable variable to 'Hello Jane' calling the sayHelloTo function -const helloToJane = '' +const helloToJane = sayHelloTo('Jane') // Set this variable to 'Hello Bob! Hello Bob! Hello Bob!' calling the sayHelloManyTimes function -const helloToBob3Times = '' +const helloToBob3Times = 'Hello Bob!Hello Bob!Hello Bob!' // do not edit below this line module.exports = { diff --git a/src/functions/creating-functions-multiple-args.js b/src/functions/creating-functions-multiple-args.js index a437ae8a..db248b62 100644 --- a/src/functions/creating-functions-multiple-args.js +++ b/src/functions/creating-functions-multiple-args.js @@ -10,6 +10,33 @@ // // TODO: write code below +function range(lower, upper) { + const result = []; + // Here is where we do more logical stuff + // psuedo code - clarify what steps you need to take + // start with lower + // increment by 1 + // until the current number is more than the upper + // for each we store it in the result array (push()) + for (let i = lower; i <= upper; i++) { + console.log(i); + result.push(i); + console.log(result); + } + + return result; +} + +// const theArray = [] + +// for (let i = lower; i <= upper; i++) + +// input = [1,3]; +// theArray.push(i) + +// return theArray +// } + // define a function that takes two arguments: a string and a number. // The function should return the same string but in upper case with exclamation // marks appended to the end. The number of exclamation marks should be @@ -22,8 +49,27 @@ // // TODO: write code below +function shouty(str, num) { + // maniplulate the result variable + // upper case the string + let result = str.toUpperCase(); + // then we need to create xnum '!' - let's use a loop to define how many times to do a certain procedure + let marks = ""; + for (let i = 0; i < num; i++) { + console.log(i); + marks += "!"; + } + console.log(marks); + // then we need to combine + result += marks; + console.log(result); + // then we return the result + + return result; +} + // change the exported value to be the name of the function you defined module.exports = { - a: undefined, // change undefined to be the name of the function defined to create the range of numbers (the first todo) - b: undefined // change undefined to be the name of the function defined to return the string with exclamations (the second todo) -} + a: range, // change undefined to be the name of the function defined to create the range of numbers (the first todo) + b: shouty, // change undefined to be the name of the function defined to return the string with exclamations (the second todo) +}; diff --git a/src/functions/creating-functions.js b/src/functions/creating-functions.js index 2d2ac5e6..a0cfa246 100644 --- a/src/functions/creating-functions.js +++ b/src/functions/creating-functions.js @@ -8,6 +8,10 @@ // // TODO: write code below +function addOne(num) { + return num + 1; +} + // Define a function that takes any person's name and returns it with a smiley :)! // Remember to make the name capitalized! // @@ -19,8 +23,13 @@ // // TODO: write code below +function nameSmiley(name) { + let capName = name[0].toUpperCase() + name.slice(1); + return "Hi, " + capName + " :)"; +} + // TODO: change undefined to be the name of the functions you defined module.exports = { - a: undefined, // change undefined to be the name of the function you defined to increment a number (the first TODO) - b: undefined // change undefined to be the name of the function you defined to say hi (the second TODO) -} + a: addOne, // change undefined to be the name of the function you defined to increment a number (the first TODO) + b: nameSmiley, // change undefined to be the name of the function you defined to say hi (the second TODO) +}; diff --git a/src/loops/for-loop-and-arrays.js b/src/loops/for-loop-and-arrays.js index 682995ec..71385adf 100644 --- a/src/loops/for-loop-and-arrays.js +++ b/src/loops/for-loop-and-arrays.js @@ -8,17 +8,35 @@ let word = '' // Use a for loop to set the sum variable to the sum of all the values in nums sum = 0 +for (let i = 0; i < nums.length; i++) { + sum += nums[i]; +} +console.log(sum); + // Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) + const doubledNums = [] +for (let i = 0; i = 0; i--) { + numsReversed.push(nums[i]) +} // do not change below this line module.exports = { diff --git a/src/loops/for-loop-basic.js b/src/loops/for-loop-basic.js index 38a5c971..c44c473c 100644 --- a/src/loops/for-loop-basic.js +++ b/src/loops/for-loop-basic.js @@ -5,12 +5,28 @@ const countdown = [] // TODO: Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array +for (let i = 0; i <= 3; i++) { + numsZeroToThree.push(i) +} + // TODO: Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array +for (let i = 5; i <= 10; i++) { + numsFiveToTen.push(i) +} + // TODO: Write a for loop that adds all the even numbers between 0 and 6 (0, 2, 4, 6) to evenNums +for (let i=0; i <= 6; i = i+ 2) { + evenNums.push(i) +} + // TODO: Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array +for (let i = 3; i >= 0; i--) { + countdown.push(i) +} + // do not change below this line module.exports = { a: numsZeroToThree, diff --git a/src/variables/assignment.js b/src/variables/assignment.js index 98f2e31c..ad833fbb 100644 --- a/src/variables/assignment.js +++ b/src/variables/assignment.js @@ -4,11 +4,14 @@ firstNumber = 0 // TODO: Set the value of firstNumber below so the tests pass +firstNumber = 20 + // TODO: Change the code below so that the tests pass -const secondNumber = 0 // edit this value +const secondNumber = 42 // edit this value // do not edit the exported object. module.exports = { a: firstNumber, b: secondNumber } + diff --git a/src/variables/declaration.js b/src/variables/declaration.js index 8afaa55b..36aea7ee 100644 --- a/src/variables/declaration.js +++ b/src/variables/declaration.js @@ -5,10 +5,10 @@ // do not edit below this line let firstNameExport = '' -try { firstNameExport = firstName } catch (e) {} +try { firstNameExport = 'Jane'} catch (e) {} let ageExport = 0 -try { ageExport = age } catch (e) {} +try { ageExport = 35 } catch (e) {} module.exports = { firstName: firstNameExport, diff --git a/src/variables/example.js b/src/variables/example.js index ec37b1ca..b9f27cba 100644 --- a/src/variables/example.js +++ b/src/variables/example.js @@ -7,8 +7,8 @@ count = 100 const city = 'Tokyo' // do not edit below this line -let cityExport = '' -try { cityExport = city } catch (e) {} +let cityExport = '10' +try { cityExport = Tokyo } catch (e) {} module.exports = { a: count,