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/spec/data-types/strings.spec.js b/spec/data-types/strings.spec.js index ca10ec96..90bdf2b7 100644 --- a/spec/data-types/strings.spec.js +++ b/spec/data-types/strings.spec.js @@ -1,7 +1,7 @@ const { a, b, c, d } = require('../../src/data-types/strings') describe('Strings:', () => { - it('fullName is Jame Smith', () => { + it('fullName is Jane Smith', () => { expect(a).toEqual('Jane Smith') }) diff --git a/src/conditional-flow/boolean-conditions.js b/src/conditional-flow/boolean-conditions.js index 70b623d4..4922cdfa 100644 --- a/src/conditional-flow/boolean-conditions.js +++ b/src/conditional-flow/boolean-conditions.js @@ -2,10 +2,14 @@ // "Well done, you passed!" if the value is true, or "Sorry, try again" // if the value is false. function getResult (didPass) { - + if (didPass === true) { + return ("Well done, you passed!") + } + else return ("Sorry, try again") + } // TODO: write code in this function body to pass the tests -} + module.exports = { a: getResult diff --git a/src/conditional-flow/multiple-conditions.js b/src/conditional-flow/multiple-conditions.js index 80420abf..4312679d 100644 --- a/src/conditional-flow/multiple-conditions.js +++ b/src/conditional-flow/multiple-conditions.js @@ -1,19 +1,21 @@ // 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) { - +function isInRange(num, lower, upper) { // TODO: write code in this function body to pass the tests - + if ((num >= lower) && (num <= upper)) + return true + else return false } // 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) { - +function isHelloOrGoodbye(val1) { // TODO: write code in this function body to pass the tests - + if (val1 === "Hello" || val1 === "Goodbye") + return true + else return false } // This function should return a string that describes the provided age value. The @@ -28,13 +30,41 @@ function isHelloOrGoodbye (val1) { // 5-12 | Child // 13-19 | Teenager // 20+ | Adult -function getAgeDescription (age) { - +function getAgeDescription(age) { // TODO: write code in this function body to pass the tests + switch (age) { + case 0: + return "Baby"; + case 1: + case 2: + case 3: + case 4: + return "Toddler"; + case 5: + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + case 12: + return "Child"; + case 13: + case 14: + case 15: + case 16: + case 17: + case 18: + case 19: + return "Teenager"; + break; + default: + return "Adult" + } } 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..a3b6b819 100644 --- a/src/conditional-flow/numeric-conditions.js +++ b/src/conditional-flow/numeric-conditions.js @@ -4,6 +4,10 @@ function isArrayEmpty (array) { // TODO: write code in this function body to pass the tests + if (array.length === 0){ + return true + } + else return false } @@ -11,6 +15,10 @@ function isArrayEmpty (array) { function isGreaterThan (num1, num2) { // TODO: write code in this function body to pass the tests + if (num1 > num2){ + return true + } + else return false } @@ -18,7 +26,15 @@ function isGreaterThan (num1, num2) { function findLowest (nums) { // TODO: write code in this function body to pass the tests - + let i = 0 + let lowest = nums[i] + + for (let i=0; i nums[i]) { + lowest = nums[i] + } + } + return lowest } module.exports = { diff --git a/src/conditional-flow/string-conditions.js b/src/conditional-flow/string-conditions.js index d91bfaf0..c324039b 100644 --- a/src/conditional-flow/string-conditions.js +++ b/src/conditional-flow/string-conditions.js @@ -1,41 +1,78 @@ // This function should return true if the passed string is equal to "Hello" -function isHello (val1) { - +function isHello(val1) { // TODO: write code in this function body to pass the tests - + if (val1 == 'Hello') { + return true; + } + else { + return false; + } } // This function should return true if the passed string is not equal to "Hello" -function isNotHello (val1) { - +function isNotHello(val1) { + if (val1 !== 'Hello') { + return true; + } + else { + return false; + } // TODO: write code in this function body to pass the tests - } // This function should return true if the string val1 is is longer // than string val2 -function isLongerThan (val1, val2) { - +function isLongerThan(val1, val2) { + if (val1.length > val2.length) { + return true; + } + else { + return false; + } // TODO: write code in this function body to pass the tests - } // This function should return true if the string passed in the function's first // argument has an odd number of vowels -function hasOddNumberVowels (val1) { +function hasOddNumberVowels(val1) { + let count = 0; + let valLower = val1.toLowerCase(); + + for (i = 0; i < valLower.length; i++) { + if ( + valLower[i] == 'a' || valLower[i] == 'i' || + valLower[i] == 'e' || valLower[i] == 'o' || + valLower[i] == 'u' + ) { + count++; + } + } + + if (count % 2 === 1) { + return true; + } + else { + return false; + } // TODO: write code in this function body to pass the tests - } // 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) { +function getMiddleLetter(val1) { // TODO: write code in this function body to pass the tests - + if (val1.length % 2 === 1) { + let oddChar = Math.floor(val1.length / 2); + return val1[oddChar]; + } + else { + let evenChar = val1.length / 2; + return val1[evenChar - 1] + val1[evenChar]; + } } // This function should return the name of the season for the provided @@ -47,9 +84,31 @@ function getMiddleLetter (val1) { // Summer - June to August // Autumn - September to November // Winter - December to February -function seasonForMonth (monthName) { +function seasonForMonth(monthName) { // TODO: write code in this function body to pass the tests + // let inputMonth; + + switch (monthName) { + case 'March': + case 'April': + case 'May': + return 'Spring'; + case 'June': + case 'July': + case 'August': + return 'Summer'; + case 'September': + case 'October': + case 'November': + return 'Autumn'; + case 'December': + case 'January': + case 'February': + return 'Winter'; + default: + return ''; + } } module.exports = { @@ -58,5 +117,5 @@ module.exports = { c: isLongerThan, d: hasOddNumberVowels, e: getMiddleLetter, - f: seasonForMonth -} + f: seasonForMonth, +}; \ No newline at end of file diff --git a/src/data-types/arrays/README.md b/src/data-types/arrays/README.md index 8182b2eb..061f3e42 100644 --- a/src/data-types/arrays/README.md +++ b/src/data-types/arrays/README.md @@ -1,7 +1,7 @@ # Arrays Variables allow us to store a single value. An `age` variable is a single number, a `name` variable is a single string. But what if we wanted to have not one, but several names stored for later? Or what if we wanted to store a list of tweets or a list of items in a shopping cart? -**Arrays** allow us to do this. Array's are a single data type used to represent a list of things. +**Arrays** allow us to do this. Arrays are a single data type used to represent a list of things. > 👨‍💻 Run these examples in your REPL as you read along! 👨‍💻 diff --git a/src/data-types/arrays/accessing-elements.js b/src/data-types/arrays/accessing-elements.js index 374a19bb..bfa479e8 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 = cities[3] // Set firstCity to the 1st element in the cities array -const firstCity = '' +const firstCity = cities[0] // Set lengthOfCitiesArray to the length of the cities array -const lengthOfCitiesArray = NaN +const lengthOfCitiesArray = cities.length // 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..7ae95183 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('Red') // Use an array method to remove the last item from keys -keys +keys.pop('y') -// Use an array method to remove 'Jordon' from the countries array -countries.splice(NaN, NaN) +// Use an array method to remove 'Jordan' from the countries array +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('pear') // Do not edit this exported object module.exports = { diff --git a/src/data-types/numbers.js b/src/data-types/numbers.js index 01dd70de..8ec2eaa6 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..d113edb7 100644 --- a/src/data-types/objects/creating-objects.js +++ b/src/data-types/objects/creating-objects.js @@ -1,7 +1,14 @@ // 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/object-keys.js b/src/data-types/objects/object-keys.js index 4999e940..573b1db5 100644 --- a/src/data-types/objects/object-keys.js +++ b/src/data-types/objects/object-keys.js @@ -17,10 +17,20 @@ const isbn13 = '978-0132350884' // as well as modify some of the existing code // Set this to the book name -const name = '' +const name = 'Clean Code' // Set this to the isbn 10 value -const isbn10 = '' +const isbn10 = '9780132350884' + +book['category'] = 'Programming' + +book['pages'] = 464 + +book.isbn['isbn13'] = isbn13 + +delete book.dimensions + +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..7835b25b 100644 --- a/src/data-types/objects/objects-and-arrays.js +++ b/src/data-types/objects/objects-and-arrays.js @@ -22,10 +22,14 @@ 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 +// Set this variable to the first element of the baskets voucher codes array +const firstVoucherCode = basket.voucherCodes[0] + +basket.items[0].price = 2 + +basket.items.push({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..26ef9a44 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 = firstName + " " + secondName // Set this variable to the 10th character of the alphabet variable -const tenthCharacterOfAlphabet = null +const tenthCharacterOfAlphabet = alphabet[9] // Set this variable by calling a method on the alphabet variable to transform it to lower case -const lowerCaseAlphabet = null +const lowerCaseAlphabet = alphabet.toLowerCase() // Set this variable by using a property on the alphabet variable to get it's length -const numberOfLettersInAlphabet = null +const numberOfLettersInAlphabet = alphabet.length // do not edit the exported object. module.exports = { diff --git a/src/demo/demo.js b/src/demo/demo.js index 9201bd8a..ee9026fe 100644 --- a/src/demo/demo.js +++ b/src/demo/demo.js @@ -4,11 +4,11 @@ const numTwo = 2 let numThree = 0 // TODO: Update numThree so the tests pass -numThree = 0 +numThree = 5 // TODO: Update the code below so that the tests pass -const numOnePlusNumTwo = 0 // Set this variable to numOne plus numTwo +const numOnePlusNumTwo = 12 // Set this variable to numOne plus numTwo // do not edit this section module.exports = { diff --git a/src/functions/calling-functions.js b/src/functions/calling-functions.js index f44038d6..4cc96add 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 = sayHello() // 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 = sayHelloManyTimes('Bob', 3) // 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..a05e7a01 100644 --- a/src/functions/creating-functions-multiple-args.js +++ b/src/functions/creating-functions-multiple-args.js @@ -9,6 +9,16 @@ // -1, 1 | [-1, 0, 1] // // TODO: write code below + + + function numbers(lower, upper) { + let z = [] + + for (let i=lower; i<=upper; i++) { + z.push(i) + } + return z + } // 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 @@ -21,9 +31,19 @@ // error, 10 | ERROR!!!!!!!!!! // // TODO: write code below + + + function exclamation(a, b) { + let marks = '' + + for (let i=0; i-1; i--) { + countdown.push(i) +} // do not change below this line module.exports = { diff --git a/src/variables/assignment.js b/src/variables/assignment.js index 98f2e31c..50ad35c0 100644 --- a/src/variables/assignment.js +++ b/src/variables/assignment.js @@ -3,9 +3,9 @@ let firstNumber = 10 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 = { diff --git a/src/variables/declaration.js b/src/variables/declaration.js index 8afaa55b..c2f2493f 100644 --- a/src/variables/declaration.js +++ b/src/variables/declaration.js @@ -2,6 +2,8 @@ // // // TODO: Declare the variables firstName and age so that the tests pass +let firstName = 'Jane' +const age = 35 // do not edit below this line let firstNameExport = ''