From 3fd13992f9d12998dd570a258e50436807070ec4 Mon Sep 17 00:00:00 2001 From: BrandonV21 Date: Mon, 13 Dec 2021 13:04:52 +0000 Subject: [PATCH 1/3] Completed demo --- package-lock.json | 1 + src/demo/demo.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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/demo/demo.js b/src/demo/demo.js index 9201bd8a..c6f97712 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 = numOne + numTwo // Set this variable to numOne plus numTwo // do not edit this section module.exports = { From c9b8537265d14e750dfb8fec1d83857eecefb1e7 Mon Sep 17 00:00:00 2001 From: BrandonV21 Date: Tue, 14 Dec 2021 11:40:52 +0000 Subject: [PATCH 2/3] Variables completed --- src/variables/assignment.js | 4 +++- src/variables/declaration.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/variables/assignment.js b/src/variables/assignment.js index 98f2e31c..4cd58664 100644 --- a/src/variables/assignment.js +++ b/src/variables/assignment.js @@ -4,8 +4,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..7287716b 100644 --- a/src/variables/declaration.js +++ b/src/variables/declaration.js @@ -3,6 +3,9 @@ // // TODO: Declare the variables firstName and age so that the tests pass +const firstName = 'Jane' +const age = 35 + // do not edit below this line let firstNameExport = '' try { firstNameExport = firstName } catch (e) {} From 0bf25eb7874f39d3f95df1009f07d5f4eb1a8d65 Mon Sep 17 00:00:00 2001 From: BrandonV21 Date: Tue, 11 Jan 2022 18:27:44 +0000 Subject: [PATCH 3/3] Updated --- src/conditional-flow/boolean-conditions.js | 11 ++++-- src/conditional-flow/multiple-conditions.js | 30 +++++++++++---- src/conditional-flow/numeric-conditions.js | 24 ++++++++---- src/conditional-flow/string-conditions.js | 37 +++++++++++++------ src/data-types/arrays/accessing-elements.js | 8 ++-- .../arrays/adding-removing-elements.js | 14 +++---- src/data-types/numbers.js | 12 +++--- src/data-types/objects/creating-objects.js | 13 ++++++- src/data-types/objects/object-keys.js | 9 ++++- src/data-types/objects/objects-and-arrays.js | 9 ++++- src/data-types/strings.js | 8 ++-- src/functions/calling-functions.js | 6 +-- .../creating-functions-multiple-args.js | 20 ++++++++-- src/functions/creating-functions.js | 14 +++++-- src/loops/example.js | 2 +- src/loops/for-loop-and-arrays.js | 18 +++++++-- src/loops/for-loop-basic.js | 15 ++++++-- 17 files changed, 177 insertions(+), 73 deletions(-) diff --git a/src/conditional-flow/boolean-conditions.js b/src/conditional-flow/boolean-conditions.js index 70b623d4..0aefd1c9 100644 --- a/src/conditional-flow/boolean-conditions.js +++ b/src/conditional-flow/boolean-conditions.js @@ -2,9 +2,14 @@ // "Well done, you passed!" if the value is true, or "Sorry, try again" // if the value is false. function getResult (didPass) { - - // TODO: write code in this function body to pass the tests - +// TODO: write code in this function body to pass the tests + result = '' + if (didPass) { + didPass = 'Well done, you passed!' + } else { + didPass = 'Sorry, try again' + } + return didPass } module.exports = { diff --git a/src/conditional-flow/multiple-conditions.js b/src/conditional-flow/multiple-conditions.js index 80420abf..febdf298 100644 --- a/src/conditional-flow/multiple-conditions.js +++ b/src/conditional-flow/multiple-conditions.js @@ -2,18 +2,24 @@ // 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 - + 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) { - - // TODO: write code in this function body to pass the tests - +// 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 @@ -29,8 +35,18 @@ function isHelloOrGoodbye (val1) { // 13-19 | Teenager // 20+ | Adult function getAgeDescription (age) { - - // TODO: write code in this function body to pass the tests +// TODO: write code in this function body to pass the tests + if (age === 0) { + return 'Baby' + } else if (age > 0 && age <= 4) { + return 'Toddler' + } else if (age >= 5 && age <= 12) { + return 'Child' + } else if (age >= 13 && age <= 19) { + return 'Teenager' + } else if (age >= 20) { + return 'Adult' + } } module.exports = { diff --git a/src/conditional-flow/numeric-conditions.js b/src/conditional-flow/numeric-conditions.js index 0f6656a5..bded2f80 100644 --- a/src/conditional-flow/numeric-conditions.js +++ b/src/conditional-flow/numeric-conditions.js @@ -2,23 +2,33 @@ // This function should return true if there are no elements in the array, false otherwise function isArrayEmpty (array) { - // TODO: write code in this function body to pass the tests - + if (array.length === 0) { + return true + } else { + return false + } } // 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 - + if (num1 > num2) { + return true + } else { + return false + } } // This function should return the lowest number in the passed array function findLowest (nums) { - - // TODO: write code in this function body to pass the tests - +// TODO: write code in this function body to pass the tests + // if (nums) { + // return nums[3] + // } else if (nums) { + // return nums[2] + // } + return Math.min(...nums) } module.exports = { diff --git a/src/conditional-flow/string-conditions.js b/src/conditional-flow/string-conditions.js index d91bfaf0..5040d84e 100644 --- a/src/conditional-flow/string-conditions.js +++ b/src/conditional-flow/string-conditions.js @@ -1,32 +1,47 @@ // 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 + 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) { - - // TODO: write code in this function body to pass the tests - +// 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 string val1 is is longer // than string val2 function isLongerThan (val1, val2) { - // TODO: write code in this function body to pass the tests - + if (val1.length > val2.length) { + return true + } else { + return false + } } // 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 + const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] + let countVowels = 0 + for (i = 0; i < val1.length; i++) { + if (vowels.includes(val1[i])) { + ++countVowels + } + } + return countVowels % 2 !== 0 } // this function should return the middle character of a string if it has an odd number 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..80fa3d2e 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..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..1116df0f 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/object-keys.js b/src/data-types/objects/object-keys.js index 4999e940..de45cec1 100644 --- a/src/data-types/objects/object-keys.js +++ b/src/data-types/objects/object-keys.js @@ -15,12 +15,17 @@ const isbn13 = '978-0132350884' // 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 +book.category = 'Programming' +book.pages = 464 +book.isbn.isbn13 = isbn13 +delete book.dimensions +delete book.isbn.asin // 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 // 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..d08f325a 100644 --- a/src/data-types/objects/objects-and-arrays.js +++ b/src/data-types/objects/objects-and-arrays.js @@ -21,11 +21,16 @@ const basket = { // 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 +basket.items[0].price = 2 +const oranges = { name: 'Oranges', price: 0.75, quantity: 4 } +basket.items.push(oranges) +// basket.items.push({ name: 'Oranges', price: 0.75, quantity: 4 }) + // Set this variable to the length of the baskets voucher codes array -const numberOfVoucherCodes = null +const numberOfVoucherCodes = basket.voucherCodes.length // Set this variable to the first element in of the baskets voucher codes array -const firstVoucherCode = null +const firstVoucherCode = basket.voucherCodes[0] // Do not edit this exported object module.exports = { diff --git a/src/data-types/strings.js b/src/data-types/strings.js index a8eb8c5b..c5a5a483 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/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..426dc40c 100644 --- a/src/functions/creating-functions-multiple-args.js +++ b/src/functions/creating-functions-multiple-args.js @@ -9,7 +9,13 @@ // -1, 1 | [-1, 0, 1] // // TODO: write code below - +function getRange (lower, upper) { + const numbers = [] + for (let i = lower; i <= upper; i++) { + numbers.push(i) + } + return numbers +} // 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 @@ -21,9 +27,15 @@ // error, 10 | ERROR!!!!!!!!!! // // TODO: write code below - +function stringExclamations (string, number) { + let addExclamation = '' + for (let i = 0; i < number; i++) { + addExclamation += '!' + } + return string.toUpperCase() + addExclamation +} // 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: getRange, // change undefined to be the name of the function defined to create the range of numbers (the first todo) + b: stringExclamations // 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..c8c8d562 100644 --- a/src/functions/creating-functions.js +++ b/src/functions/creating-functions.js @@ -7,7 +7,10 @@ // 2 | 3 // // TODO: write code below - +function increment (num) { + num++ + return num +} // Define a function that takes any person's name and returns it with a smiley :)! // Remember to make the name capitalized! // @@ -18,9 +21,12 @@ // Aiyana | Hi, Aiyana :) // // TODO: write code below - +function sayHi (name) { + // return 'Hi, ' + name[0].toUpperCase() + name.slice(1) + ' :)' + return 'Hi, ' + name[0].toUpperCase() + name.substring(1) + ' :)' +} // 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: increment, // change undefined to be the name of the function you defined to increment a number (the first TODO) + b: sayHi // change undefined to be the name of the function you defined to say hi (the second TODO) } diff --git a/src/loops/example.js b/src/loops/example.js index eee24f17..0822a46e 100644 --- a/src/loops/example.js +++ b/src/loops/example.js @@ -8,7 +8,7 @@ for (let i = 1; i < 6; i++) { nums.push(i) } -// TODO: Write a for loop to add the all but the last number of nums to the nums2 array +// TODO: Write a for loop to add all but the last number of nums2 to the nums3 array for (let i = 0; i < nums2.length - 1; i++) { nums3.push(nums2[i]) } diff --git a/src/loops/for-loop-and-arrays.js b/src/loops/for-loop-and-arrays.js index 682995ec..17c019fa 100644 --- a/src/loops/for-loop-and-arrays.js +++ b/src/loops/for-loop-and-arrays.js @@ -7,19 +7,31 @@ 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 = sum + nums[i] +} // 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 < nums.length; i++) { + doubledNums.push(nums[i] * 2) +} // Use a for loop to set word equal to all the letters in the letters array word = '' +for (let i = 0; i < letters.length; i++) { + word = word + letters[i] +} // Use a for loop to populate everySecondNum with every second number from the nums array const everySecondNum = [] - +for (let i = 1; i < nums.length; i += 2) { + everySecondNum.push(nums[i]) +} // Use a for loop to populate numsReversed with the numbers from nums in reverse order const numsReversed = [] - +for (let i = nums.length - 1; i > -1; i--) { + numsReversed.push(nums[i]) +} // do not change below this line module.exports = { a: sum, diff --git a/src/loops/for-loop-basic.js b/src/loops/for-loop-basic.js index 38a5c971..8768fd54 100644 --- a/src/loops/for-loop-basic.js +++ b/src/loops/for-loop-basic.js @@ -4,12 +4,21 @@ const evenNums = [] 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 += 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 = {