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/conditional-flow/boolean-conditions.spec.js b/spec/conditional-flow/boolean-conditions.spec.js index 53669409..ad7ec91c 100644 --- a/spec/conditional-flow/boolean-conditions.spec.js +++ b/spec/conditional-flow/boolean-conditions.spec.js @@ -1,10 +1,10 @@ -const { a } = require('../../src/conditional-flow/boolean-conditions') +const { a } = require("../../src/conditional-flow/boolean-conditions"); describe("Boolean conditions getResult:", () => { it("Returns 'Well done, you passed!' with true", () => { - expect(a(true)).toEqual('Well done, you passed!') - }) + expect(a(true)).toEqual("Well done, you passed!"); + }); it("Returns 'Sorry, try again' with false", () => { - expect(a(false)).toEqual('Sorry, try again') - }) -}) + expect(a(false)).toEqual("Sorry, try again"); + }); +}); diff --git a/spec/conditional-flow/multiple-conditions.spec.js b/spec/conditional-flow/multiple-conditions.spec.js index 6192a922..87d48634 100644 --- a/spec/conditional-flow/multiple-conditions.spec.js +++ b/spec/conditional-flow/multiple-conditions.spec.js @@ -1,66 +1,65 @@ -const {a, b, c} = require('../../src/conditional-flow/multiple-conditions') +const { a, b, c } = require("../../src/conditional-flow/multiple-conditions"); describe("Multiple Conditions isInRange:", () => { it("4 is between 1 and 6", () => { - expect(a(4, 1, 6)).toEqual(true) - }) + expect(a(4, 1, 6)).toEqual(true); + }); it("10 is not between 1 and 5", () => { - expect(a(10, 1, 5)).toEqual(false) - }) + expect(a(10, 1, 5)).toEqual(false); + }); it("10 is between 1 and 10", () => { - expect(a(10, 1, 10)).toEqual(true) - }) + expect(a(10, 1, 10)).toEqual(true); + }); it("8 is between 8 and 8", () => { - expect(a(8, 8, 8)).toEqual(true) - }) -}) + expect(a(8, 8, 8)).toEqual(true); + }); +}); describe("Multiple Conditions isHelloOrGoodbye:", () => { it("'Hello' returns true", () => { - expect(b('Hello')).toEqual(true) - }) + expect(b("Hello")).toEqual(true); + }); it("'Hey!' returns false", () => { - expect(b('Hey!')).toEqual(false) - }) + expect(b("Hey!")).toEqual(false); + }); it("'Goodbye' returns true", () => { - expect(b('Goodbye')).toEqual(true) - }) -}) - + expect(b("Goodbye")).toEqual(true); + }); +}); + describe("Multiple Conditions getAgeDescription:", () => { it("0 is a Baby", () => { - expect(c(0)).toEqual("Baby") - }) + expect(c(0)).toEqual("Baby"); + }); it("1-4 is a Toddler", () => { - expect(c(1)).toEqual("Toddler") - expect(c(2)).toEqual("Toddler") - expect(c(3)).toEqual("Toddler") - expect(c(4)).toEqual("Toddler") - }) + expect(c(1)).toEqual("Toddler"); + expect(c(2)).toEqual("Toddler"); + expect(c(3)).toEqual("Toddler"); + expect(c(4)).toEqual("Toddler"); + }); it("5-12 is a Child", () => { - expect(c(5)).toEqual("Child") - expect(c(6)).toEqual("Child") - expect(c(7)).toEqual("Child") - expect(c(9)).toEqual("Child") - expect(c(10)).toEqual("Child") - expect(c(11)).toEqual("Child") - expect(c(12)).toEqual("Child") - }) + expect(c(5)).toEqual("Child"); + expect(c(6)).toEqual("Child"); + expect(c(7)).toEqual("Child"); + expect(c(9)).toEqual("Child"); + expect(c(10)).toEqual("Child"); + expect(c(11)).toEqual("Child"); + expect(c(12)).toEqual("Child"); + }); it("13-19 is a Teenager", () => { - expect(c(13)).toEqual("Teenager") - expect(c(14)).toEqual("Teenager") - expect(c(15)).toEqual("Teenager") - expect(c(16)).toEqual("Teenager") - expect(c(17)).toEqual("Teenager") - expect(c(18)).toEqual("Teenager") - expect(c(19)).toEqual("Teenager") - }) + expect(c(13)).toEqual("Teenager"); + expect(c(14)).toEqual("Teenager"); + expect(c(15)).toEqual("Teenager"); + expect(c(16)).toEqual("Teenager"); + expect(c(17)).toEqual("Teenager"); + expect(c(18)).toEqual("Teenager"); + expect(c(19)).toEqual("Teenager"); + }); it("20+ is an Adult", () => { - expect(c(20)).toEqual("Adult") - expect(c(25)).toEqual("Adult") - expect(c(40)).toEqual("Adult") - }) - -}) + expect(c(20)).toEqual("Adult"); + expect(c(25)).toEqual("Adult"); + expect(c(40)).toEqual("Adult"); + }); +}); diff --git a/spec/conditional-flow/numeric-conditions.spec.js b/spec/conditional-flow/numeric-conditions.spec.js index eb75bcdb..6ce55d6e 100644 --- a/spec/conditional-flow/numeric-conditions.spec.js +++ b/spec/conditional-flow/numeric-conditions.spec.js @@ -1,46 +1,46 @@ -const {a, b, c} = require('../../src/conditional-flow/numeric-conditions') +const { a, b, c } = require("../../src/conditional-flow/numeric-conditions"); describe("Numeric Conditions isArrayEmpty:", () => { it("[] is empty", () => { - expect(a([])).toEqual(true) - }) + expect(a([])).toEqual(true); + }); it("[1] is not empty", () => { - expect(a([1])).toEqual(false) - }) + expect(a([1])).toEqual(false); + }); it("['hello', 'Ed'] is not empty", () => { - expect(a(['hello', 'Ed'])).toEqual(false) - }) -}) + expect(a(["hello", "Ed"])).toEqual(false); + }); +}); describe("Numeric conditions isGreaterThan:", () => { it("3 is greater than 2", () => { - expect(b(3, 2)).toEqual(true) - }) + expect(b(3, 2)).toEqual(true); + }); it("0 is not greater than 10", () => { - expect(b(0, 10)).toEqual(false) - }) + expect(b(0, 10)).toEqual(false); + }); it("42 is not greater than 42", () => { - expect(b(42, 42)).toEqual(false) - }) + expect(b(42, 42)).toEqual(false); + }); it("-1 is greater than -3", () => { - expect(b(-1, -3)).toEqual(true) - }) -}) + expect(b(-1, -3)).toEqual(true); + }); +}); describe("Numeric Conditions findLowest:", () => { it("1 is lowest in [10, 8, 4, 1, 8]", () => { - expect(c([10, 8, 4, 1, 8])).toEqual(1) - }) + expect(c([10, 8, 4, 1, 8])).toEqual(1); + }); it("-10 is lowest in [0, 0, -10]", () => { - expect(c([0, 0, -10])).toEqual(-10) - }) + expect(c([0, 0, -10])).toEqual(-10); + }); it("100 is lowest in [100,100,100,100]", () => { - expect(c([100, 100, 100])).toEqual(100) - }) -}) \ No newline at end of file + expect(c([100, 100, 100])).toEqual(100); + }); +}); diff --git a/spec/conditional-flow/string-conditions.spec.js b/spec/conditional-flow/string-conditions.spec.js index aa30f6f6..b6a50907 100644 --- a/spec/conditional-flow/string-conditions.spec.js +++ b/spec/conditional-flow/string-conditions.spec.js @@ -1,104 +1,110 @@ -const { a, b, c, d, e, f } = require('../../src/conditional-flow/string-conditions') +const { + a, + b, + c, + d, + e, + f, +} = require("../../src/conditional-flow/string-conditions"); describe("String Conditions isHello:", () => { it("Returns false with 'goodbye'", () => { - expect(a("goodbye")).toEqual(false) - }) + expect(a("goodbye")).toEqual(false); + }); it("Returns true with 'Hello'", () => { - expect(a("Hello")).toEqual(true) - }) -}) + expect(a("Hello")).toEqual(true); + }); +}); describe("String Conditions isNotHello:", () => { it("Returns true with 'goodbye'", () => { - expect(b("goodbye")).toEqual(true) - }) + expect(b("goodbye")).toEqual(true); + }); it("Returns false with 'Hello'", () => { - expect(b("Hello")).toEqual(false) - }) -}) + expect(b("Hello")).toEqual(false); + }); +}); describe("String Conditions isLongerThan:", () => { it("'Mike' is longer than 'Ed'", () => { - expect(c("Mike", "Ed")).toEqual(true) - }) + expect(c("Mike", "Ed")).toEqual(true); + }); it("'Mike' is not longer than 'Lewis'", () => { - expect(c("Mike", "Lewis")).toEqual(false) - }) + expect(c("Mike", "Lewis")).toEqual(false); + }); it("'Mike' is not longer than 'Mike'", () => { - expect(c("Mike", "Mike")).toEqual(false) - }) -}) + expect(c("Mike", "Mike")).toEqual(false); + }); +}); describe("String Conditions hasOddNumberVowels:", () => { it("'Alex' does not have odd number vowels", () => { - expect(d("Alex")).toEqual(false) - }) + expect(d("Alex")).toEqual(false); + }); it("'Mo' does have odd number vowels", () => { - expect(d("Mo")).toEqual(true) - }) + expect(d("Mo")).toEqual(true); + }); it("'Joanna' does have odd number vowels", () => { - expect(d("Joanna")).toEqual(true) - }) + expect(d("Joanna")).toEqual(true); + }); it("'Maggie Smith' does not have odd number vowels", () => { - expect(d("Maggie Smith")).toEqual(false) - }) -}) + expect(d("Maggie Smith")).toEqual(false); + }); +}); describe("String conditions getMiddleLetter:", () => { it("'Alex' returns 'le'", () => { - expect(e("Alex")).toEqual('le') - }) + expect(e("Alex")).toEqual("le"); + }); it("'Edward' returns 'wa'", () => { - expect(e("Edward")).toEqual('wa') - }) + expect(e("Edward")).toEqual("wa"); + }); it("'Kayla' returns 'y'", () => { - expect(e("Kayla")).toEqual('y') - }) + expect(e("Kayla")).toEqual("y"); + }); it("'Tom' returns 'o'", () => { - expect(e("Tom")).toEqual('o') - }) -}) - -describe('String conditions seasonForMonth:', () => { - it('January is Winter', () => { - expect(f('January')).toEqual('Winter') - }) - it('February is Winter', () => { - expect(f('February')).toEqual('Winter') - }) - it('March is Spring', () => { - expect(f('March')).toEqual('Spring') - }) - it('April is Spring', () => { - expect(f('April')).toEqual('Spring') - }) - it('May is Spring', () => { - expect(f('May')).toEqual('Spring') - }) - it('June is Summer', () => { - expect(f('June')).toEqual('Summer') - }) - it('July is Summer', () => { - expect(f('July')).toEqual('Summer') - }) - it('August is Summer', () => { - expect(f('August')).toEqual('Summer') - }) - it('September is Autumn', () => { - expect(f('September')).toEqual('Autumn') - }) - it('October is Autumn', () => { - expect(f('October')).toEqual('Autumn') - }) - it('November is Autumn', () => { - expect(f('November')).toEqual('Autumn') - }) - it('December is Winter', () => { - expect(f('December')).toEqual('Winter') - }) - it('Marchprilvember is empty', () => { - expect(f('Marchprilvember')).toEqual('') - }) -}) + expect(e("Tom")).toEqual("o"); + }); +}); +describe("String conditions seasonForMonth:", () => { + it("January is Winter", () => { + expect(f("January")).toEqual("Winter"); + }); + it("February is Winter", () => { + expect(f("February")).toEqual("Winter"); + }); + it("March is Spring", () => { + expect(f("March")).toEqual("Spring"); + }); + it("April is Spring", () => { + expect(f("April")).toEqual("Spring"); + }); + it("May is Spring", () => { + expect(f("May")).toEqual("Spring"); + }); + it("June is Summer", () => { + expect(f("June")).toEqual("Summer"); + }); + it("July is Summer", () => { + expect(f("July")).toEqual("Summer"); + }); + it("August is Summer", () => { + expect(f("August")).toEqual("Summer"); + }); + it("September is Autumn", () => { + expect(f("September")).toEqual("Autumn"); + }); + it("October is Autumn", () => { + expect(f("October")).toEqual("Autumn"); + }); + it("November is Autumn", () => { + expect(f("November")).toEqual("Autumn"); + }); + it("December is Winter", () => { + expect(f("December")).toEqual("Winter"); + }); + it("Marchprilvember is empty", () => { + expect(f("Marchprilvember")).toEqual(""); + }); +}); diff --git a/spec/variables/assignment.spec.js b/spec/variables/assignment.spec.js index 265ae766..ec9f59e7 100644 --- a/spec/variables/assignment.spec.js +++ b/spec/variables/assignment.spec.js @@ -1,11 +1,11 @@ -const { a, b } = require('../../src/variables/assignment.js') +const { a, b } = require("../../src/variables/assignment.js"); describe("Variable Assignment:", () => { it("firstNumber is 20", () => { - expect(a).toEqual(20) - }) + expect(a).toEqual(20); + }); it("secondNumber is 42", () => { - expect(b).toEqual(42) - }) -}) + expect(b).toEqual(42); + }); +}); diff --git a/src/conditional-flow/boolean-conditions.js b/src/conditional-flow/boolean-conditions.js index 70b623d4..eeadd13c 100644 --- a/src/conditional-flow/boolean-conditions.js +++ b/src/conditional-flow/boolean-conditions.js @@ -1,12 +1,13 @@ // 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) { - // TODO: write code in this function body to pass the tests +// TODO: write code in this function body to pass the tests +function getResult(didPass) { + return didPass ? "Well done, you passed!" : "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..c26a4d3e 100644 --- a/src/conditional-flow/multiple-conditions.js +++ b/src/conditional-flow/multiple-conditions.js @@ -1,19 +1,15 @@ // 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 - +function isInRange(num, lower, upper) { + return num >= lower && num <= upper; } // 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 - +function isHelloOrGoodbye(val1) { + return val1 === "Hello" || val1 === "Goodbye"; } // This function should return a string that describes the provided age value. The @@ -28,13 +24,16 @@ function isHelloOrGoodbye (val1) { // 5-12 | Child // 13-19 | Teenager // 20+ | Adult -function getAgeDescription (age) { - - // TODO: write code in this function body to pass the tests +function getAgeDescription(age) { + if (age === 0) return "Baby"; + if (age > 0 && age <= 4) return "Toddler"; + if (age > 4 && age <= 12) return "Child"; + if (age > 12 && age <= 19) return "Teenager"; + if (age >= 20) 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..c9f60bb0 100644 --- a/src/conditional-flow/numeric-conditions.js +++ b/src/conditional-flow/numeric-conditions.js @@ -1,28 +1,22 @@ // 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) { - - // TODO: write code in this function body to pass the tests - +function isArrayEmpty(array) { + 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 - +function isGreaterThan(num1, num2) { + return num1 > num2; } // 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 - +function findLowest(nums) { + return Math.min(...nums); } 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..c780f65d 100644 --- a/src/conditional-flow/string-conditions.js +++ b/src/conditional-flow/string-conditions.js @@ -1,41 +1,48 @@ // 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 - +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 - +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 - +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 - +function hasOddNumberVowels(val1) { + const vowels = "aeiouAEIOU"; + count = 0; + for (let j = 0; j < val1.length; j++) { + if (vowels.includes(val1[j])) { + count += 1; + } + } + if (count % 2 !== 0) { + return true; + } else { + return false; + } } +console.log("line 28", hasOddNumberVowels("ioou")); // 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 - +function getMiddleLetter(val1) { + if (val1.length % 2 === 0) { + return val1.charAt(val1.length / 2 - 1) + val1.charAt(val1.length / 2); + } else { + return val1.charAt(Math.floor(val1.length / 2)); + } } // This function should return the name of the season for the provided @@ -47,9 +54,29 @@ 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 +function seasonForMonth(monthName) { + monthName = monthName.toLowerCase(); + + 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 +85,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..3afd78b6 100644 --- a/src/data-types/arrays/accessing-elements.js +++ b/src/data-types/arrays/accessing-elements.js @@ -1,24 +1,24 @@ // do not edit this section -const cities = ['London', 'Shanghai', 'New York', 'Delhi', 'Kuala Lumpur'] +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 = { a: names, b: fourthCity, c: firstCity, - d: lengthOfCitiesArray -} + d: lengthOfCitiesArray, +}; diff --git a/src/data-types/arrays/adding-removing-elements.js b/src/data-types/arrays/adding-removing-elements.js index 69e559e8..f0bfdbcf 100644 --- a/src/data-types/arrays/adding-removing-elements.js +++ b/src/data-types/arrays/adding-removing-elements.js @@ -1,34 +1,34 @@ // do not edit this section -const cities = ['London', 'Shanghai', 'New York', 'Delhi', 'Kuala Lumpur'] -const names = [] -const numbers = [1, 2, 3] -const colours = ['Red', 'Blue', 'Yellow'] -const keys = ['q', 'w', 'e', 'r', 't', 'y'] -const countries = ['Bolivia', 'Jordan', 'Greenland'] -const fruits = ['Apple', 'Orange', 'Pear'] +const cities = ["London", "Shanghai", "New York", "Delhi", "Kuala Lumpur"]; +const names = []; +const numbers = [1, 2, 3]; +const colours = ["Red", "Blue", "Yellow"]; +const keys = ["q", "w", "e", "r", "t", "y"]; +const countries = ["Bolivia", "Jordan", "Greenland"]; +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 = { @@ -39,5 +39,5 @@ module.exports = { e: keys, f: countries, g: fruits, - h: pear -} + h: pear, +}; diff --git a/src/data-types/numbers.js b/src/data-types/numbers.js index 01dd70de..be261689 100644 --- a/src/data-types/numbers.js +++ b/src/data-types/numbers.js @@ -1,27 +1,27 @@ // do not edit these lines -const numOne = 8 -const numTwo = 16 -const numThree = 32 +const numOne = 8; +const numTwo = 16; +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 = { @@ -30,5 +30,5 @@ module.exports = { c: numThreeDividedByNumOne, d: numThreeMinusNumOne, e: sum, - f: numBytes -} + f: numBytes, +}; diff --git a/src/data-types/objects/creating-objects.js b/src/data-types/objects/creating-objects.js index 1939e6b0..bdb26309 100644 --- a/src/data-types/objects/creating-objects.js +++ b/src/data-types/objects/creating-objects.js @@ -1,10 +1,10 @@ // 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 = { person: person, - computer: computer -} + computer: computer, +}; diff --git a/src/data-types/objects/object-keys.js b/src/data-types/objects/object-keys.js index 4999e940..c7306047 100644 --- a/src/data-types/objects/object-keys.js +++ b/src/data-types/objects/object-keys.js @@ -1,30 +1,42 @@ // do not edit this section const book = { - name: 'Clean Code', - author: 'Robert C. Martin', - category: 'Cooking', + name: "Clean Code", + author: "Robert C. Martin", + category: "Cooking", + isbn: { - isbn10: '9780132350884', - asin: '0132350882' + isbn10: "9780132350884", + asin: "0132350882", }, - publisher: 'Prentice Hall', - dimensions: '10x12x2' -} + publisher: "Prentice Hall", + dimensions: "10x12x2", +}; -const isbn13 = '978-0132350884' +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 // 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"] = "Programming"; +const category = book.category; + +book["pages"] = 464; +const pages = book.pages; + +book.isbn["isbn13"] = isbn13; + +delete book["dimensions"]; +delete book.isbn["asin"]; // Do not edit this exported object module.exports = { name: name, isbn10: isbn10, - book: book -} + book: book, +}; diff --git a/src/data-types/objects/objects-and-arrays.js b/src/data-types/objects/objects-and-arrays.js index e4819467..07e482b7 100644 --- a/src/data-types/objects/objects-and-arrays.js +++ b/src/data-types/objects/objects-and-arrays.js @@ -2,34 +2,38 @@ const basket = { items: [ { - name: 'Apple', + name: "Apple", quantity: 10, - price: 1 + price: 1, }, { - name: 'Lemon', + name: "Lemon", quantity: 2, - price: 0.5 - } + price: 0.5, + }, ], - voucherCodes: [ - 'AA-AA-A', - 'BB-BB-B' - ] -} + voucherCodes: ["AA-AA-A", "BB-BB-B"], +}; // 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 // 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]; +basket.items[0].price = 2; // Do not edit this exported object + +basket.items.push({ + name: "Oranges", + price: 0.75, + quantity: 4, +}); module.exports = { basket: basket, numberOfVoucherCodes: numberOfVoucherCodes, - firstVoucherCode: firstVoucherCode -} + firstVoucherCode: firstVoucherCode, +}; diff --git a/src/data-types/strings.js b/src/data-types/strings.js index a8eb8c5b..787861b7 100644 --- a/src/data-types/strings.js +++ b/src/data-types/strings.js @@ -1,26 +1,25 @@ // do not edit these lines -const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -const firstName = 'Jane' -const secondName = 'Smith' +const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +const firstName = "Jane"; +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.toLocaleLowerCase(); // 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 = { a: fullName, b: tenthCharacterOfAlphabet, c: lowerCaseAlphabet, - d: numberOfLettersInAlphabet -} + d: numberOfLettersInAlphabet, +}; diff --git a/src/demo/demo.js b/src/demo/demo.js index 9201bd8a..91d1529f 100644 --- a/src/demo/demo.js +++ b/src/demo/demo.js @@ -1,17 +1,17 @@ // do not edit this section -const numOne = 10 -const numTwo = 2 -let numThree = 0 +const numOne = 10; +const numTwo = 2; +let numThree = 0; // TODO: Update numThree so the tests pass -numThree = 0 +numThree = numOne / numTwo; // 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 = { a: numThree, - b: numOnePlusNumTwo -} + b: numOnePlusNumTwo, +}; diff --git a/src/functions/calling-functions.js b/src/functions/calling-functions.js index f44038d6..566ea27b 100644 --- a/src/functions/calling-functions.js +++ b/src/functions/calling-functions.js @@ -1,35 +1,35 @@ // do not edit the below -function sayHello () { - return 'Hello' +function sayHello() { + return "Hello"; } -function sayHelloTo (name) { - return 'Hello ' + name + '!' +function sayHelloTo(name) { + return "Hello " + name + "!"; } -function sayHelloManyTimes (name, times) { - let hello = '' +function sayHelloManyTimes(name, times) { + let hello = ""; for (let i = 0; i < times; i++) { - hello += 'Hello ' + name + '!' + hello += "Hello " + name + "!"; } - return hello + return hello; } // 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 = { a: hello, b: helloToJane, - c: helloToBob3Times -} + c: helloToBob3Times, +}; diff --git a/src/functions/creating-functions-multiple-args.js b/src/functions/creating-functions-multiple-args.js index a437ae8a..5f9dc4a4 100644 --- a/src/functions/creating-functions-multiple-args.js +++ b/src/functions/creating-functions-multiple-args.js @@ -10,11 +10,33 @@ // // TODO: write code below +function multipleArgs(numLower, numUpper) { + let emptyArray = []; + for (let i = 0; i < numUpper - numLower + 1; i++) { + emptyArray.push(numLower + i); + } + return emptyArray; +} + +console.log(multipleArgs(-13, 1)); +console.log(multipleArgs(10, 13)); +console.log(multipleArgs(-1, 1)); // 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 // determined by the number argument. // + +function stringNumber(initialString, num) { + let finalString = initialString.toUpperCase(); + for (let j = 0; j < num; j++) { + finalString = finalString + "!"; + } + return finalString; +} + +console.log(stringNumber("HeyHo", 7)); + // Example Input and Output: // Input | Output // disaster, 5 | DISASTER!!!!! @@ -24,6 +46,6 @@ // 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: multipleArgs, // change undefined to be the name of the function defined to create the range of numbers (the first todo) + b: stringNumber, // 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..8030af8e 100644 --- a/src/functions/creating-functions.js +++ b/src/functions/creating-functions.js @@ -8,11 +8,19 @@ // // TODO: write code below +const addOne = (x) => { + return x + 1; +}; // Define a function that takes any person's name and returns it with a smiley :)! // Remember to make the name capitalized! // // Example Input and Output: // +const addSmiley = (name) => { + const capatalisedName = name.charAt(0).toUpperCase() + name.slice(1); + return `Hi, ${capatalisedName} :)`; +}; + // Input | Output // edward | Hi, Edward :) // Aiyana | Hi, Aiyana :) @@ -21,6 +29,6 @@ // 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: addSmiley, // 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..11bd605d 100644 --- a/src/loops/for-loop-and-arrays.js +++ b/src/loops/for-loop-and-arrays.js @@ -1,24 +1,44 @@ -const nums = [1, 3, 12, 5, 1, 6, 4, 1, 10] -const letters = ['H', 'e', 'l', 'l', 'o'] -let sum = 0 -let word = '' +const nums = [1, 3, 12, 5, 1, 6, 4, 1, 10]; +const letters = ["H", "e", "l", "l", "o"]; +let sum = 0; +let word = ""; // TODO: Add code below this line to make the tests pass // Use a for loop to set the sum variable to the sum of all the values in nums -sum = 0 -// Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) -const doubledNums = [] +for (let j = 0; j < nums.length; j++) { + sum = sum + nums[j]; +} +// Use a for loop to populate doubledNums with every value from nums array doubled (i.e [2, 6, 24, etc...]) +const doubledNums = []; +for (let j = 0; j < nums.length; j++) { + double = nums[j] * 2; + doubledNums.push(double); +} // Use a for loop to set word equal to all the letters in the letters array -word = '' - +word = ""; +for (let j = 0; j < letters.length; j++) { + word = word + letters[j]; +} // Use a for loop to populate everySecondNum with every second number from the nums array -const everySecondNum = [] +const everySecondNum = []; +for (let j = 1; j < nums.length; j += 2) { + let second = nums[j]; + everySecondNum.push(second); +} // Use a for loop to populate numsReversed with the numbers from nums in reverse order -const numsReversed = [] +const numsReversed = []; + +for (let j = nums.length - 1; j >= 0; j--) { + let num = nums[j]; + numsReversed.push(num); +} + +// console.log(num); +// numsReversed.push(num); // do not change below this line module.exports = { @@ -26,5 +46,5 @@ module.exports = { b: doubledNums, c: word, d: everySecondNum, - e: numsReversed -} + e: numsReversed, +}; diff --git a/src/loops/for-loop-basic.js b/src/loops/for-loop-basic.js index 38a5c971..acc83940 100644 --- a/src/loops/for-loop-basic.js +++ b/src/loops/for-loop-basic.js @@ -1,20 +1,33 @@ -const numsZeroToThree = [] -const numsFiveToTen = [] -const evenNums = [] -const countdown = [] +const numsZeroToThree = []; +const numsFiveToTen = []; +const evenNums = []; +const countdown = []; // TODO: Write a for loop that adds the numbers 0 to 3 to the numsZeroToThree array - +for (i = 0; i < 3 + 1; i++) { + numsZeroToThree.push(i); +} // TODO: Write a for loop that adds the numbers 5 to 10 to the numsFiveToTen array +for (i = 5; i < 10 + 1; 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 (i = 0; i < 7; i++) { + if (i % 2 === 0) { + evenNums.push(i); + } +} // TODO: Write a for loop that adds the numbers 3 to 0 (in that order) to the countdown array +for (i = 3; i >= 0; i--) { + countdown.push(i); +} // do not change below this line module.exports = { a: numsZeroToThree, b: numsFiveToTen, c: evenNums, - d: countdown -} + d: countdown, +}; diff --git a/src/variables/assignment.js b/src/variables/assignment.js index 98f2e31c..7f85253e 100644 --- a/src/variables/assignment.js +++ b/src/variables/assignment.js @@ -1,14 +1,16 @@ // do not edit this -let firstNumber = 10 -firstNumber = 0 +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 = { a: firstNumber, - b: secondNumber -} + b: secondNumber, +}; diff --git a/src/variables/declaration.js b/src/variables/declaration.js index 8afaa55b..eae39645 100644 --- a/src/variables/declaration.js +++ b/src/variables/declaration.js @@ -2,15 +2,20 @@ // // // TODO: Declare the variables firstName and age so that the tests pass - +const firstName = "Jane"; +let age = 35; // do not edit below this line -let firstNameExport = '' -try { firstNameExport = firstName } catch (e) {} +let firstNameExport = ""; +try { + firstNameExport = firstName; +} catch (e) {} -let ageExport = 0 -try { ageExport = age } catch (e) {} +let ageExport = 0; +try { + ageExport = age; +} catch (e) {} module.exports = { firstName: firstNameExport, - age: ageExport -} + age: ageExport, +};