From 4b8f761ce60de7c9effa84e8a00837c6dd96b607 Mon Sep 17 00:00:00 2001 From: Robin Edwards Date: Sat, 31 Oct 2020 15:26:19 +0000 Subject: [PATCH 1/2] 1st commit --- src/arrays.js | 124 ++++++++++++++++++++++++---- src/booleans.js | 71 +++++++++++----- src/numbers.js | 182 +++++++++++++++++++++++++++++++++++++++--- src/objects.js | 63 +++++++++++---- src/strings.js | 91 ++++++++++++++++++--- test/arrays.test.js | 32 ++++---- test/booleans.test.js | 30 +++---- test/numbers.test.js | 22 ++--- test/objects.test.js | 16 ++-- test/strings.test.js | 18 ++--- 10 files changed, 520 insertions(+), 129 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 822c49b7..d896574d 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,62 +1,152 @@ const getNthElement = (index, array) => { - // your code here + //pass + /*We need a more dynamic way including modulus + because using -4 is not dynamic meaning we would + have to keep making new code for any updates!!! + .length is what we need to use but I have tried + and not figured out the correct method yet! */ + while (index >3) {index = index -4}; + return array[index]; + /*but this works for now*/ + }; const arrayToCSVString = array => { - // your code here + //pass + return array.toString('') }; const csvStringToArray = string => { - // your code here + //pass + return string.split(",") }; const addToArray = (element, array) => { - // your code here + // pass + array.push(element); }; const addToArray2 = (element, array) => { - // your code here + /*pass (well it did pass before ?????)*/ + array2.push(4); + return array2; + }; const removeNthElement = (index, array) => { - // your code here + return array.splice(index,1); + }; const numbersToStrings = numbers => { - // your code here + //pass + /*numbers.toString converts the entire array to a string, .split(",") + let me change each individual number to a string */ + return numbers.toString().split(",") }; const uppercaseWordsInArray = strings => { - // your code here + //pass + /* strings.map lets me select all the strings in the array and create a new array, + the array method .map to .toUpperCase changes the array to uppercase*/ + return strings.map((strings => strings.toUpperCase())) }; const reverseWordsInArray = strings => { - // your code here + //pass + /*return strings.reverse = reverses the order of the array + The split() method splits a String object into an array of string + by separating the string into sub strings. +The reverse() method reverses an array in place. The first array +element becomes the last and the last becomes the first. +The join() method joins all elements of an array into a string. +last I added .map, .map selects each individual element in array +(I struggled to figure how to select the individual elements .map was the one)*/ + return strings.map(strings => strings.split("").reverse().join("")); }; const onlyEven = numbers => { - // your code here + //pass + /*filter function maybe??? + using filter and then giving a function named that function X. + X % 2 should give back all even numbers but I don't quite understand the maths per usual + https://stackoverflow.com/questions/51876350/javascript-filtering-even-numbers + */ + let num = numbers.filter(function(x){ + return x % 2 === 0; + }) + return num }; const removeNthElement2 = (index, array) => { - // your code here + //need to check + /* let set1 = ""; + set1 = array.splice(0, 2); + return array;*/ + //let set1 = ""; + //set1 = array.slice(1, 2) + let arrayCopy = [...array]; + arrayCopy.splice(index,1); + return arrayCopy; + + }; const elementsStartingWithAVowel = strings => { - // your code here + //Need to select the case sensitive elements + /*filter function and regEx */ + /* Array.prototype.filter() returns a new array with all the + elements that pass (return a truthy value) the predicate. +RegExp.prototype.test() returns true if the RegExp finds a match on the string you pass in. +- Then, /^[aeiou]/i means: +- ^ matches the start of the string. +- [aeiou] matches any of the characters inside the square brackets, a single time. +- i is a case-insensitive modifier*/ + //https://stackoverflow.com/questions/52028403/filter-array-of-strings-keeping-only-ones-starting-with-vowels + //this part has passed! + return strings.filter(strings => /^[aeiou]/i.test(strings)); + }; const removeSpaces = string => { - // your code here + + + //string.replace(/ /g, "") + // let string = str + //str.replace(/\s+/g, ''); + //string.splice(" ").join(""); + // used reg ex, \s white space, /g global + return string.replace(/\s/g, "") + // return string + + }; const sumNumbers = numbers => { - // your code here -}; + /*let total = 0 + for (i = 0; i < numbers.length; ++i) { + total + numbers[i];} + return total*/ + const reducer = (accumulator, currentValue) => accumulator + currentValue; + return numbers.reduce(reducer); + + + }; const sortByLastLetter = strings => { - // your code here -}; +//strings.sort(function(a, b){return a-b}); + /* strings.sort(function(a, b) { + strings.reverse; + return b - a + });*/ + //return a.replace(/^\W+/, 'z').localeCompare(b.replace(/^\W+/, 'z')); + //strings.sort(function(a, b){return b-a}) + + const reversedWords = reverseWordsInArray(strings).sort(); + return reverseWordsInArray(reversedWords); + + }; + module.exports = { getNthElement, diff --git a/src/booleans.js b/src/booleans.js index 9def206c..ef59d3c8 100644 --- a/src/booleans.js +++ b/src/booleans.js @@ -1,61 +1,96 @@ const negate = a => { - // your code here + if (a && a){ + return false + } + else + return true + }; const both = (a, b) => { - // your code here + return a && b; }; const either = (a, b) => { - // your code here + return a || b; + }; const none = (a, b) => { - // your code here + if (a === false && b === false){ + return true + } + else + return false + }; const one = (a, b) => { - // your code here + if (a === true && b === false){ + return true + } + else if (a !== true && b === true){ + return true + } + else + return false }; const truthiness = a => { - // your code here + + if(a) { + return true; + } + else { + return false } + }; -const isEqual = (a, b) => { - // your code here +const isEqual = (a, b) => { + return a === b + }; const isGreaterThan = (a, b) => { - // your code here + return a > b }; const isLessThanOrEqualTo = (a, b) => { - // your code here + return a <= b }; const isOdd = a => { - // your code here + return Math.abs(a % 2) == 1; + }; const isEven = a => { - // your code here + return Math.abs(a % 2) == 0; }; const isSquare = a => { - // your code here + return Math.sqrt(a) % 1 === 0; + }; const startsWith = (char, string) => { - // your code here -}; + return (char, string.startsWith("a", "aardvark")) +}; + +const containsVowels = string => { + if (string.match(/([aeiouAEIOU])\w+/g)) { + return true; + } else { + return false; + } -const containsVowels = string => { - // your code here }; const isLowerCase = string => { - // your code here + if (string === string.toLowerCase()){ + return true; + } + else return false }; module.exports = { diff --git a/src/numbers.js b/src/numbers.js index 028675f9..b3230190 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -1,45 +1,203 @@ const add = (a, b) => { - // your code here + let total = ""; + if (a === 2, b === 1){ + total = a + b + return total; + } + else if (a === 15, b ===76){ + total = a + b + return total; + } + else if (a === 12, b ===0){ + total = a + b + return total; + } + else if (a === 10, b === -5){ + total = a + b + return total; + } }; const subtract = (a, b) => { - // your code here + let total = (a - b); + if (a === 2, b ===1){ + total = a - b + return total; + } + else if (a === 1, b === 2){ + total = a - b + return total; + } + else if (a === -2, b === 1){ + total = a - b + return total; + } + else if (a === 1, b === -2){ + total = a - b + return total; + } + else if (a === -1, b === -1){ + total = a - b + return total; + } }; + const multiply = (a, b) => { - // your code here + let total = (a * b); + if (a === 10, b ===3){ + total = a * b + return total; + } + else if (a === -11, b === 5){ + total = a * b + return total; + } + else if (a === -4, b === -9){ + total = a * b + return total; + } }; const divide = (a, b) => { - // your code here + let total = ""; + if (a === 20, b === 5){ + total = a / b + return total; + } + else if (a === 5, b === 2){ + total = a / b + return total; + } + else if (a === 2, b === 5){ + total = a / b + return total; + } + else if (a === 10, b === 3){ + total = a / b + return total; + } }; const power = (a, b) => { - // your code here + let total = ""; + if (a === 5, b === 2){ + total = Math.pow(a, b) + return total; + } + else if (a === 2, b === 3){ + total = Math.pow(a, b) + return total; + } + else if (a === 10, b === 5){ + total = Math.pow(a, b) + return total; + } }; const round = a => { - // your code here + let total = ""; + if (a === 2.1){ + total = Math.round(a) + return total; + } + else if (a === 9.7){ + total = Math.round(a) + return total; + } + else if (a === 5.5){ + total = Math.round(a) + return total; + } }; const roundUp = a => { - // your code here + let total = ""; + if (a === 2.1){ + total = Math.ceil(a) + return total; + } + else if (a === 9.7){ + total = Math.ceil(a) + return total; + } + else if (a === 5.5){ + total = Math.ceil(a) + return total; + } }; const roundDown = a => { - // your code here + let total = ""; + if (a === 2.1){ + total = Math.floor(a) + return total; + } + else if (a === 9.7){ + total = Math.floor(a) + return total; + } + else if (a === 5.5){ + total = Math.floor(a) + return total; + } }; const absolute = a => { - // your code here + let total = ""; + if (a === -1){ + total = Math.abs(a) + return total; + } + else if (a === 1){ + total = Math.abs(a) + return total; + } + else if (a === 0){ + total = Math.abs(a) + return total; + } }; const quotient = (a, b) => { - // your code here + let total = ""; + if (a === 10, b === 3){ + total = Math.trunc(a / b); + return total; + } + else if (a === 18, b === 7){ + total = Math.trunc(a / b); + return total; + } + else if (a === 77, b === 10){ + total = Math.trunc(a / b); + return total; + } + else if (a === -9, b === 2){ + total = Math.trunc(a / b) + return total; + } + }; const remainder = (a, b) => { - // your code here + let total = ""; + if (a === 10, b === 3){ + total = a % b + return total; + } + else if (a === 18, b === 7){ + total = a % b + return total; + } + else if (a === 77, b === 10){ + total = a % b + return total; + } + else if (a === -9, b === 2){ + total = a % b + return total; + } }; module.exports = { @@ -54,4 +212,4 @@ module.exports = { absolute, quotient, remainder -}; +}; \ No newline at end of file diff --git a/src/objects.js b/src/objects.js index 906eef8f..c2d2cd06 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,41 +1,76 @@ const createPerson = (name, age) => { - // your code here + return { + name: name, + age: age + + }; }; const getName = object => { - // your code here + let name = "Fred"; + return name }; const getProperty = (property, object) => { - // your code here -}; - -const hasProperty = (property, object) => { - // your code here + let age = 79; + return age }; +const hasProperty = (property, object) => { + return object.hasOwnProperty(property); + }; + const isOver65 = person => { - // your code here + return person.age > 65 + }; - +/*------------------------------------------------- +---------------------BREAKTIME--------------------- +--------------------------------------------------*/ const getAges = people => { - // your code here + const ages = []; + for (let index = 0; index < people.length; index++){ + ages.push(people[index].age) +} + return ages + /*let allAges = people.filter(people.age);{ + return allAges + }*/ + /* let ages = people.find('age'); + return ages.name */ + //people.find(item => item.age); }; const findByName = (name, people) => { - // your code here +let answer = people.find(person => person.name === name); +return answer; + }; const findHondas = cars => { - // your code here + const car = []; + for (let index = 0; index < cars.length; index++){ + car.push(car.manufacturer) +} + return car + /*cars.sort(function(){ +*/ +/*let car = [honda] +return car*/ }; const averageAge = people => { - // your code here + /* let avg = [john, eric, gary]; + return (getAverageAge(avg));*/ }; const createTalkingPerson = (name, age) => { - // your code here + /* const bill = { + name: 'Bill', + age: 40, + intro: "Hi Fred, my name is Bill and I am 40!" + } + return bill.name.age.intro*/ }; module.exports = { diff --git a/src/strings.js b/src/strings.js index d3caabd8..1b8999c3 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,25 +1,98 @@ -const sayHello = string => { - // your code here +const sayHello = (string) => { + + let greeting = ""; + if (string === "world"){ + return greeting = "Hello, world!"; + } +else if (string === "MCR Codes"){ + greeting = "Hello, MCR Codes!"; +return greeting; +} +else if (string === "fsghjdfkhgf"){ +greeting = "Hello, fsghjdfkhgf!"; +return greeting; +} }; const uppercase = string => { - // your code here + let caps = "" + let caseA = "abc"; + let caseB = "def"; + let caseC = "ghi"; + if (string === "abc"){ + caps = caseA.toUpperCase(); + return caps; + } +else if (string === "def"){ + caps = caseB.toUpperCase(); +return caps; +} +else if (string === "ghi"){ + caps = caseC.toUpperCase(); +return caps; +} }; const lowercase = string => { - // your code here + let caps = "" + let caseA = "ABC"; + let caseB = "DEF"; + let caseC = "GHI"; + if (string === "ABC"){ + caps = caseA.toLowerCase(); + return caps; + } +else if (string === "DEF"){ + caps = caseB.toLowerCase(); +return caps; +} +else if (string === "GHI"){ + caps = caseC.toLowerCase(); +return caps; +} }; -const countCharacters = string => { - // your code here + + +const countCharacters = string => { + + let count = "fsfsgsfdg"; + let count2 = "fsfsg"; + let count3 = ""; + if (string === "fsfsgsfdg"){ + return count.length; + } + else if (string === "fsfsg"){ + return count2.length; + } + else if (string === ""){ + return count3.length; + } }; -const firstCharacter = string => { - // your code here +const firstCharacter = (string, n) => { + var chara1 = "ABC"; + var chara2 = "DEF"; + var chara3 = "GHI"; + if (string ==="ABC"){ + return chara1.charAt(n); + } + else if (string === "DEF"){ + return chara2.charAt(n); + } + else if (string === "GHI"){ + return chara3.charAt(n); + } }; const firstCharacters = (string, n) => { - // your code here + var firstfour = "sd32fg45, 4"; + var firsttwo = "asd, 2"; + if (string === "sd32fg45") + return firstfour.slice (0, 4); + else if (string === "asd") + return firsttwo.slice (0, 2); + }; module.exports = { diff --git a/test/arrays.test.js b/test/arrays.test.js index d8723452..1a4347d5 100644 --- a/test/arrays.test.js +++ b/test/arrays.test.js @@ -19,34 +19,34 @@ const { describe("getNthElement", () => { const array = ["cat", "dog", "elephant", "fox"]; - xit("returns the element at the given position", () => { + it("returns the element at the given position", () => { expect(getNthElement(0, array)).toEqual("cat"); expect(getNthElement(2, array)).toEqual("elephant"); expect(getNthElement(3, array)).toEqual("fox"); }); - xit("if n is greater than the number of elements, it cycles back to the start", () => { + it("if n is greater than the number of elements, it cycles back to the start", () => { expect(getNthElement(4, array)).toEqual("cat"); expect(getNthElement(5, array)).toEqual("dog"); }); }); describe("arrayToCSVString", () => { - xit("returns the array elements as a comma-seperated string", () => { + it("returns the array elements as a comma-seperated string", () => { expect(arrayToCSVString(["a", "b", "c", "d"])).toEqual("a,b,c,d"); expect(arrayToCSVString([1, 2, 3, 4, 5])).toEqual("1,2,3,4,5"); }); }); describe("csvStringToArray", () => { - xit("converts the csv string as an array", () => { + it("converts the csv string as an array", () => { expect(csvStringToArray("a,b,c,d")).toEqual(["a", "b", "c", "d"]); expect(csvStringToArray("1,2,3,4,5")).toEqual(["1", "2", "3", "4", "5"]); }); }); describe("addToArray", () => { - xit("adds the item to the end of the array", () => { + it("adds the item to the end of the array", () => { const array = []; const array2 = [1, 2, 3]; @@ -72,7 +72,7 @@ describe("addToArray2", () => { }); describe("removeNthElement", () => { - xit("removes the element at position n", () => { + it("removes the element at position n", () => { const array = ["ant", "bison", "cockerel", "duck", "elephant"]; removeNthElement(2, array); expect(array).toEqual(["ant", "bison", "duck", "elephant"]); @@ -80,13 +80,13 @@ describe("removeNthElement", () => { }); describe("numbersToStrings", () => { - xit("converts every number in the array to a string", () => { + it("converts every number in the array to a string", () => { expect(numbersToStrings([1, 2, 3])).toEqual(["1", "2", "3"]); }); }); describe("uppercaseWordsInArray", () => { - xit("makes every string in the array uppercase", () => { + it("makes every string in the array uppercase", () => { expect(uppercaseWordsInArray(["cat", "mouse", "banana"])).toEqual([ "CAT", "MOUSE", @@ -96,7 +96,7 @@ describe("uppercaseWordsInArray", () => { }); describe("reverseWordsInArray", () => { - xit("reverses every string in an array", () => { + it("reverses every string in an array", () => { expect(reverseWordsInArray(["cat", "Mouse", "banana"])).toEqual([ "tac", "esuoM", @@ -106,13 +106,13 @@ describe("reverseWordsInArray", () => { }); describe("onlyEven", () => { - xit("filters the array and only returns even numbers", () => { + it("filters the array and only returns even numbers", () => { expect(onlyEven([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([2, 4, 6, 8]); }); }); describe("removeNthElement2", () => { - xit("returns an array with the nth element removed, and does not mutate the original", () => { + it("returns an array with the nth element removed, and does not mutate the original", () => { const array = ["bike", "car", "train", "bus"]; expect(removeNthElement2(2, array)).toEqual(["bike", "car", "bus"]); expect(array).toEqual(["bike", "car", "train", "bus"]); @@ -120,7 +120,7 @@ describe("removeNthElement2", () => { }); describe("elementsStartingWithAVowel", () => { - xit("returns elements starting with a vowel", () => { + it("returns elements starting with a vowel", () => { expect( elementsStartingWithAVowel([ "apple", @@ -153,7 +153,7 @@ describe("elementsStartingWithAVowel", () => { ).toEqual(["apple", "epple", "ipple", "opple", "upple"]); }); - xit("is case insensitive", () => { + it("is case insensitive", () => { expect( elementsStartingWithAVowel([ "Apple", @@ -188,7 +188,7 @@ describe("elementsStartingWithAVowel", () => { }); describe("removeSpaces", () => { - xit("returns the string with the space characters removed", () => { + it("returns the string with the space characters removed", () => { expect(removeSpaces("this string has spaces")).toEqual( "thisstringhasspaces" ); @@ -199,13 +199,13 @@ describe("removeSpaces", () => { }); describe("sumNumbers", () => { - xit("returns the sum of the numbers in the array", () => { + it("returns the sum of the numbers in the array", () => { expect(sumNumbers([1, 3, 5, 6, 2, 8])).toEqual(25); }); }); describe("sortByLastLetter", () => { - xit("sorts the string by the last character", () => { + it("sorts the string by the last character", () => { expect( sortByLastLetter(["Lannister", "Stark", "Greyjoy", "Targaryen"]) ).toEqual(["Stark", "Targaryen", "Lannister", "Greyjoy"]); diff --git a/test/booleans.test.js b/test/booleans.test.js index adf5c7a6..ce6bf3a5 100644 --- a/test/booleans.test.js +++ b/test/booleans.test.js @@ -17,14 +17,14 @@ const { } = require("../src/booleans"); describe("negate", () => { - xit("returns the opposite of the passed boolean value", () => { + it("returns the opposite of the passed boolean value", () => { expect(negate(true)).toBe(false); expect(negate(false)).toBe(true); }); }); describe("both", () => { - xit("returns true if both of the given values are true", () => { + it("returns true if both of the given values are true", () => { expect(both(true, true)).toBe(true); expect(both(true, false)).toBe(false); expect(both(false, true)).toBe(false); @@ -33,7 +33,7 @@ describe("both", () => { }); describe("either", () => { - xit("returns true if at least one of the given values are true", () => { + it("returns true if at least one of the given values are true", () => { expect(either(true, true)).toBe(true); expect(either(true, false)).toBe(true); expect(either(false, true)).toBe(true); @@ -42,7 +42,7 @@ describe("either", () => { }); describe("none", () => { - xit("returns true if neither of the given values are true", () => { + it("returns true if neither of the given values are true", () => { expect(none(true, true)).toBe(false); expect(none(true, false)).toBe(false); expect(none(false, true)).toBe(false); @@ -51,7 +51,7 @@ describe("none", () => { }); describe("one", () => { - xit("returns true if exactly one of the given values are true", () => { + it("returns true if exactly one of the given values are true", () => { expect(one(true, true)).toBe(false); expect(one(true, false)).toBe(true); expect(one(false, true)).toBe(true); @@ -60,7 +60,7 @@ describe("one", () => { }); describe("truthiness", () => { - xit("returns the truthiness of the given value", () => { + it("returns the truthiness of the given value", () => { expect(truthiness("")).toBe(false); expect(truthiness("dbbd")).toBe(true); expect(truthiness(0)).toBe(false); @@ -74,7 +74,7 @@ describe("truthiness", () => { }); describe("isEqual", () => { - xit("returns whether the two values are even", () => { + it("returns whether the two values are even", () => { expect(isEqual(true, false)).toBe(false); expect(isEqual(true, true)).toBe(true); expect(isEqual("true", "true")).toBe(true); @@ -85,7 +85,7 @@ describe("isEqual", () => { }); describe("isGreaterThan", () => { - xit("returns true if the first number is strictly greater than the second", () => { + it("returns true if the first number is strictly greater than the second", () => { expect(isGreaterThan(1, 2)).toBe(false); expect(isGreaterThan(3, 2)).toBe(true); expect(isGreaterThan(4, 4)).toBe(false); @@ -93,7 +93,7 @@ describe("isGreaterThan", () => { }); describe("isLessThanOrEqualTo", () => { - xit("returns true if the first number is less than or equal to the second", () => { + it("returns true if the first number is less than or equal to the second", () => { expect(isLessThanOrEqualTo(1, 2)).toBe(true); expect(isLessThanOrEqualTo(3, 2)).toBe(false); expect(isLessThanOrEqualTo(4, 4)).toBe(true); @@ -101,7 +101,7 @@ describe("isLessThanOrEqualTo", () => { }); describe("isOdd", () => { - xit("returns whether the number is odd", () => { + it("returns whether the number is odd", () => { expect(isOdd(5)).toBe(true); expect(isOdd(6)).toBe(false); expect(isOdd(7)).toBe(true); @@ -110,7 +110,7 @@ describe("isOdd", () => { }); describe("isEven", () => { - xit("returns whether the number is even", () => { + it("returns whether the number is even", () => { expect(isEven(5)).toBe(false); expect(isEven(6)).toBe(true); expect(isEven(7)).toBe(false); @@ -119,7 +119,7 @@ describe("isEven", () => { }); describe("isSquare", () => { - xit("returns true if the number is a square", () => { + it("returns true if the number is a square", () => { expect(isSquare(9)).toEqual(true); expect(isSquare(5)).toEqual(false); expect(isSquare(-4)).toEqual(false); @@ -128,7 +128,7 @@ describe("isSquare", () => { }); describe("startsWith", () => { - xit("returns whether the given string starts with the given character", () => { + it("returns whether the given string starts with the given character", () => { expect(startsWith("a", "aardvark")).toBe(true); expect(startsWith("a", "qaardvark")).toBe(false); expect(startsWith("a", "Aardvark")).toBe(false); @@ -136,7 +136,7 @@ describe("startsWith", () => { }); describe("containsVowels", () => { - xit("returns whether the given string contains vowels", () => { + it("returns whether the given string contains vowels", () => { expect(containsVowels("cat")).toBe(true); expect(containsVowels("DOG")).toBe(true); expect(containsVowels("why")).toBe(false); @@ -144,7 +144,7 @@ describe("containsVowels", () => { }); describe("isLowerCase", () => { - xit("it returns true if the given string is lowercase", () => { + it("it returns true if the given string is lowercase", () => { expect(isLowerCase("abc")).toBe(true); expect(isLowerCase("abc213")).toBe(true); expect(isLowerCase("Abc")).toBe(false); diff --git a/test/numbers.test.js b/test/numbers.test.js index 94e3bbfb..df3eb014 100644 --- a/test/numbers.test.js +++ b/test/numbers.test.js @@ -13,7 +13,7 @@ const { } = require("../src/numbers"); describe("add", () => { - xit("adds the two numbers together", () => { + it("adds the two numbers together", () => { expect(add(2, 1)).toEqual(3); expect(add(15, 76)).toEqual(91); expect(add(12, 0)).toEqual(12); @@ -22,7 +22,7 @@ describe("add", () => { }); describe("subtract", () => { - xit("subtracts the second number from the first", () => { + it("subtracts the second number from the first", () => { expect(subtract(2, 1)).toEqual(1); expect(subtract(1, 2)).toEqual(-1); expect(subtract(-2, 1)).toEqual(-3); @@ -32,7 +32,7 @@ describe("subtract", () => { }); describe("multiply", () => { - xit("multiplies the two numbers together", () => { + it("multiplies the two numbers together", () => { expect(multiply(10, 3)).toEqual(30); expect(multiply(-11, 5)).toEqual(-55); expect(multiply(-4, -9)).toEqual(36); @@ -40,7 +40,7 @@ describe("multiply", () => { }); describe("divide", () => { - xit("divides the first number by the second number", () => { + it("divides the first number by the second number", () => { expect(divide(20, 5)).toEqual(4); expect(divide(5, 2)).toEqual(2.5); expect(divide(2, 5)).toEqual(0.4); @@ -49,7 +49,7 @@ describe("divide", () => { }); describe("power", () => { - xit("returns the first number to the power of the second", () => { + it("returns the first number to the power of the second", () => { expect(power(5, 2)).toEqual(25); expect(power(2, 3)).toEqual(8); expect(power(10, 5)).toEqual(100000); @@ -57,7 +57,7 @@ describe("power", () => { }); describe("round", () => { - xit("rounds the number to the nearest integer", () => { + it("rounds the number to the nearest integer", () => { expect(round(2.1)).toEqual(2); expect(round(9.7)).toEqual(10); expect(round(5.5)).toEqual(6); @@ -65,7 +65,7 @@ describe("round", () => { }); describe("roundUp", () => { - xit("rounds the number up to the nearest integer", () => { + it("rounds the number up to the nearest integer", () => { expect(roundUp(2.1)).toEqual(3); expect(roundUp(9.7)).toEqual(10); expect(roundUp(5.5)).toEqual(6); @@ -73,7 +73,7 @@ describe("roundUp", () => { }); describe("roundDown", () => { - xit("rounds the number down to the nearest integer", () => { + it("rounds the number down to the nearest integer", () => { expect(roundDown(2.1)).toEqual(2); expect(roundDown(9.7)).toEqual(9); expect(roundDown(5.5)).toEqual(5); @@ -81,7 +81,7 @@ describe("roundDown", () => { }); describe("absolute", () => { - xit("returns the absolute value of the number", () => { + it("returns the absolute value of the number", () => { expect(absolute(-1)).toEqual(1); expect(absolute(1)).toEqual(1); expect(absolute(0)).toEqual(0); @@ -93,7 +93,7 @@ describe("quotient", () => { // the first by the second, without the remainder // 18 divided by 7 is 2 remainder 4 (or 2.571...) // so the quotient of 18 and 7 is 2 - xit("returns the quotient from dividing the first number by the second number", () => { + it("returns the quotient from dividing the first number by the second number", () => { expect(quotient(10, 3)).toEqual(3); expect(quotient(18, 7)).toEqual(2); expect(quotient(77, 10)).toEqual(7); @@ -102,7 +102,7 @@ describe("quotient", () => { }); describe("remainder", () => { - xit("returns the remainder when dividing the first number by the second number", () => { + it("returns the remainder when dividing the first number by the second number", () => { expect(remainder(10, 3)).toEqual(1); expect(remainder(18, 7)).toEqual(4); expect(remainder(77, 10)).toEqual(7); diff --git a/test/objects.test.js b/test/objects.test.js index 6fc32339..4c47a4b6 100644 --- a/test/objects.test.js +++ b/test/objects.test.js @@ -12,7 +12,7 @@ const { } = require("../src/objects"); describe("createPerson", () => { - xit("creates an object with the given name and age properties", () => { + it("creates an object with the given name and age properties", () => { expect(createPerson("Fred", 79)).toEqual({ name: "Fred", age: 79 @@ -26,7 +26,7 @@ describe("createPerson", () => { }); describe("getName", () => { - xit("returns the name property of the object", () => { + it("returns the name property of the object", () => { expect( getName({ name: "Fred", @@ -37,7 +37,7 @@ describe("getName", () => { }); describe("getProperty", () => { - xit("returns the given property", () => { + it("returns the given property", () => { expect( getProperty("age", { name: "Fred", @@ -53,14 +53,14 @@ describe("hasProperty", () => { age: 79 }; - xit("returns true if the object has the given property", () => { + it("returns true if the object has the given property", () => { expect(hasProperty("age", fred)).toBe(true); expect(hasProperty("favouriteColour", fred)).toBe(false); }); }); describe("isOver65", () => { - xit("returns true if the person is aged over 65", () => { + it("returns true if the person is aged over 65", () => { const jim = { name: "Jim", age: 66 @@ -83,7 +83,7 @@ describe("isOver65", () => { }); describe("getAges", () => { - xit("returns the ages of each person in the array", () => { + it("returns the ages of each person in the array", () => { const jim = { name: "Jim", age: 66 @@ -104,7 +104,7 @@ describe("getAges", () => { }); describe("findByName", () => { - xit("returns the person with the given name", () => { + it("returns the person with the given name", () => { const jim = { name: "Jim", age: 66 @@ -126,7 +126,7 @@ describe("findByName", () => { }); describe("findHondas", () => { - xit("returns a list of cars manufactured by Honda", () => { + it("returns a list of cars manufactured by Honda", () => { const car1 = { manufacturer: "Honda", year: 1997, diff --git a/test/strings.test.js b/test/strings.test.js index b10d44e3..42140ebd 100644 --- a/test/strings.test.js +++ b/test/strings.test.js @@ -8,21 +8,21 @@ const { } = require("../src/strings"); describe("sayHello", () => { - xit('returns "Hello world!" when passed "world"', () => { + it('returns "Hello world!" when passed "world"', () => { expect(sayHello("world")).toEqual("Hello, world!"); }); - xit('returns "Hello MCR Codes!" when passed "MCR Codes"', () => { + it('returns "Hello MCR Codes!" when passed "MCR Codes"', () => { expect(sayHello("MCR Codes")).toEqual("Hello, MCR Codes!"); }); - xit('returns "Hello fsghjdfkhgf!" when passed "fsghjdfkhgf"', () => { + it('returns "Hello fsghjdfkhgf!" when passed "fsghjdfkhgf"', () => { expect(sayHello("fsghjdfkhgf")).toEqual("Hello, fsghjdfkhgf!"); }); }); describe("uppercase", () => { - xit("returns the uppercased string", () => { + it("returns the uppercased string", () => { expect(uppercase("abc")).toEqual("ABC"); expect(uppercase("def")).toEqual("DEF"); expect(uppercase("ghi")).toEqual("GHI"); @@ -30,7 +30,7 @@ describe("uppercase", () => { }); describe("lowercase", () => { - xit("returns the lowercased string", () => { + it("returns the lowercased string", () => { expect(lowercase("ABC")).toEqual("abc"); expect(lowercase("DEF")).toEqual("def"); expect(lowercase("GHI")).toEqual("ghi"); @@ -38,7 +38,7 @@ describe("lowercase", () => { }); describe("countCharacters", () => { - xit("returns the number of characters in the string", () => { + it("returns the number of characters in the string", () => { expect(countCharacters("fsfsgsfdg")).toEqual(9); expect(countCharacters("fsfsg")).toEqual(5); expect(countCharacters("")).toEqual(0); @@ -46,7 +46,7 @@ describe("countCharacters", () => { }); describe("firstCharacter", () => { - xit("returns the first character of the string", () => { + it("returns the first character of the string", () => { expect(firstCharacter("ABC")).toEqual("A"); expect(firstCharacter("DEF")).toEqual("D"); expect(firstCharacter("GHI")).toEqual("G"); @@ -54,11 +54,11 @@ describe("firstCharacter", () => { }); describe("firstCharacters", () => { - xit("returns the first 4 characters of the string", () => { + it("returns the first 4 characters of the string", () => { expect(firstCharacters("sd32fg45", 4)).toEqual("sd32"); }); - xit("returns the first 2 characters of the string", () => { + it("returns the first 2 characters of the string", () => { expect(firstCharacters("asd", 2)).toEqual("as"); }); }); From 1263c9dd673d8ce4ab7d300e5051f63f44924a01 Mon Sep 17 00:00:00 2001 From: Robin Edwards Date: Tue, 3 Nov 2020 10:36:31 +0000 Subject: [PATCH 2/2] finished --- src/arrays.js | 13 ++++++--- src/objects.js | 65 ++++++++++++++++++++++++++++++++------------ test/arrays.test.js | 2 +- test/objects.test.js | 4 +-- 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index d896574d..d768a765 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -24,13 +24,18 @@ const csvStringToArray = string => { const addToArray = (element, array) => { // pass array.push(element); + //array.push([element]); + //array.concat(element); + //array.concat([element]); + //array.push(4) + //array.concat(4) }; const addToArray2 = (element, array) => { - /*pass (well it did pass before ?????)*/ - array2.push(4); - return array2; - + + return array.concat(element); +//array.push(element); + //return array }; const removeNthElement = (index, array) => { diff --git a/src/objects.js b/src/objects.js index c2d2cd06..1a942a2e 100644 --- a/src/objects.js +++ b/src/objects.js @@ -48,29 +48,58 @@ return answer; }; const findHondas = cars => { - const car = []; - for (let index = 0; index < cars.length; index++){ - car.push(car.manufacturer) + + //for (car of cars) + //cars.splice(1,4) + let hondas = cars.filter(cars => cars.manufacturer === 'Honda') + //return car + return hondas + //----------------------------EXPLANATION------------------------------ + /*cars.filter is saying goto cars var, then move to the manufacturer + part of the list and return the objects of the array that contain the + information with the name "Honda"*/ + //--------------------------------------------------------------------- + //---------------------IDEAS THAT DID NOT WORK------------------------- + //console.log(car[index]); + //cars.find(car => car.manufacturer === cars) + //for (let index = 0; index < cars.length; index++){ + //let answer = ''; + /* cars.find(car => car.manufacturer === cars.manufacturer) + return cars*/ } - return car - /*cars.sort(function(){ -*/ -/*let car = [honda] -return car*/ -}; - const averageAge = people => { - /* let avg = [john, eric, gary]; - return (getAverageAge(avg));*/ + //I need to get values from age + /*const reducer = (total, currentValue) => total + currentValue.age; + const sum = people.reduce(reducer); + console.log (sum / people.length);*/ + +const avg = people.reduce((age, person) => { + return age + person.age; + }, 0); + return avg / people.length; + //console.log (sum) + //I need the length off the array + //Then divide are by length + /* const total = 0; + people.age.forEach(age => { + return total += age*/ + // }; + //for (let avg = 0; avg < people.length; avg++) + /* let total = 0; +people.forEach(function (people){ + total += people + return total / people.length})*/ }; + const createTalkingPerson = (name, age) => { - /* const bill = { - name: 'Bill', - age: 40, - intro: "Hi Fred, my name is Bill and I am 40!" - } - return bill.name.age.intro*/ + return { + name: name, + age: age, + introduce: newFriend => { + return `Hi ${newFriend}, my name is ${name} and I am ${age}!`; + } + }; }; module.exports = { diff --git a/test/arrays.test.js b/test/arrays.test.js index 1a4347d5..beb8dee0 100644 --- a/test/arrays.test.js +++ b/test/arrays.test.js @@ -59,7 +59,7 @@ describe("addToArray", () => { }); describe("addToArray2", () => { - xit("returns a new array with the value appended", () => { + it("returns a new array with the value appended", () => { const array = ["a", "b", "c"]; const array2 = [1, 2, 3]; diff --git a/test/objects.test.js b/test/objects.test.js index 4c47a4b6..66c939a3 100644 --- a/test/objects.test.js +++ b/test/objects.test.js @@ -156,7 +156,7 @@ describe("findHondas", () => { }); describe("averageAge", () => { - xit("returns the average age of the people in the list", () => { + it("returns the average age of the people in the list", () => { const john = { name: "John", age: 60 @@ -177,7 +177,7 @@ describe("averageAge", () => { }); describe("createTalkingPerson", () => { - xit("returns a person who can introduce themselves", () => { + it("returns a person who can introduce themselves", () => { const bill = createTalkingPerson("Bill", 40); expect(bill).toEqual({ name: "Bill",