From ab60b67c2637a3275c0349d005fe344313e065f0 Mon Sep 17 00:00:00 2001 From: Mahmoud Jeilani Date: Sat, 3 Jul 2021 21:28:44 +0100 Subject: [PATCH 1/2] completed most of the test --- test/arrays.test.js | 7 +- test/booleans.test.js | 34 ++++---- test/dist/arrays.test.dev.js | 121 ++++++++++++++++++++++++++ test/dist/objects.test.dev.js | 159 ++++++++++++++++++++++++++++++++++ test/numbers.test.js | 20 ++--- test/objects.test.js | 16 ++-- test/strings.test.js | 16 ++-- 7 files changed, 327 insertions(+), 46 deletions(-) create mode 100644 test/dist/arrays.test.dev.js create mode 100644 test/dist/objects.test.dev.js diff --git a/test/arrays.test.js b/test/arrays.test.js index d8723452..586a88e1 100644 --- a/test/arrays.test.js +++ b/test/arrays.test.js @@ -21,6 +21,7 @@ describe("getNthElement", () => { xit("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"); }); @@ -59,7 +60,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]; @@ -112,7 +113,7 @@ describe("onlyEven", () => { }); describe("removeNthElement2", () => { - xit("returns an array with the nth element removed, and does not mutate the original", () => { + it.only("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"]); @@ -205,7 +206,7 @@ describe("sumNumbers", () => { }); 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 c0ae87b1..3e04106e 100644 --- a/test/booleans.test.js +++ b/test/booleans.test.js @@ -24,7 +24,7 @@ describe("negate", () => { }); 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 equal", () => { + it("returns whether the two values are equal", () => { 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,15 +128,15 @@ describe("isSquare", () => { }); describe("startsWith", () => { - xit("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); + it("returns whether the given string starts with the given character", () => { + expect(startsWith("aardvark", "a")).toBe(true); + expect(startsWith("qaardvark", "a")).toBe(false); + expect(startsWith("Aardvark", "a")).toBe(false); }); }); 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/dist/arrays.test.dev.js b/test/dist/arrays.test.dev.js new file mode 100644 index 00000000..81499b9c --- /dev/null +++ b/test/dist/arrays.test.dev.js @@ -0,0 +1,121 @@ +"use strict"; + +var _require = require("../src/arrays"), + getNthElement = _require.getNthElement, + arrayToCSVString = _require.arrayToCSVString, + csvStringToArray = _require.csvStringToArray, + addToArray = _require.addToArray, + addToArray2 = _require.addToArray2, + removeNthElement = _require.removeNthElement, + numbersToStrings = _require.numbersToStrings, + uppercaseWordsInArray = _require.uppercaseWordsInArray, + reverseWordsInArray = _require.reverseWordsInArray, + onlyEven = _require.onlyEven, + removeNthElement2 = _require.removeNthElement2, + elementsStartingWithAVowel = _require.elementsStartingWithAVowel, + removeSpaces = _require.removeSpaces, + sumNumbers = _require.sumNumbers, + sortByLastLetter = _require.sortByLastLetter; + +describe("getNthElement", function () { + var array = ["cat", "dog", "elephant", "fox"]; + it("returns the element at the given position", function () { + 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", function () { + expect(getNthElement(4, array)).toEqual("cat"); + expect(getNthElement(5, array)).toEqual("dog"); + }); +}); +describe("arrayToCSVString", function () { + xit("returns the array elements as a comma-seperated string", function () { + 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", function () { + xit("converts the csv string as an array", function () { + 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", function () { + xit("adds the item to the end of the array", function () { + var array = []; + var array2 = [1, 2, 3]; + expect(addToArray("a", array)).toEqual(undefined); + expect(addToArray(4, array2)).toEqual(undefined); + expect(array).toEqual(["a"]); + expect(array2).toEqual([1, 2, 3, 4]); + }); +}); +describe("addToArray2", function () { + xit("returns a new array with the value appended", function () { + var array = ["a", "b", "c"]; + var array2 = [1, 2, 3]; + expect(addToArray2("d", array)).toEqual(["a", "b", "c", "d"]); + expect(array).toEqual(["a", "b", "c"]); + expect(addToArray2(4, array2)).toEqual([1, 2, 3, 4]); + expect(array2).toEqual([1, 2, 3]); + }); +}); +describe("removeNthElement", function () { + xit("removes the element at position n", function () { + var array = ["ant", "bison", "cockerel", "duck", "elephant"]; + removeNthElement(2, array); + expect(array).toEqual(["ant", "bison", "duck", "elephant"]); + }); +}); +describe("numbersToStrings", function () { + xit("converts every number in the array to a string", function () { + expect(numbersToStrings([1, 2, 3])).toEqual(["1", "2", "3"]); + }); +}); +describe("uppercaseWordsInArray", function () { + xit("makes every string in the array uppercase", function () { + expect(uppercaseWordsInArray(["cat", "mouse", "banana"])).toEqual(["CAT", "MOUSE", "BANANA"]); + }); +}); +describe("reverseWordsInArray", function () { + xit("reverses every string in an array", function () { + expect(reverseWordsInArray(["cat", "Mouse", "banana"])).toEqual(["tac", "esuoM", "ananab"]); + }); +}); +describe("onlyEven", function () { + xit("filters the array and only returns even numbers", function () { + expect(onlyEven([1, 2, 3, 4, 5, 6, 7, 8])).toEqual([2, 4, 6, 8]); + }); +}); +describe("removeNthElement2", function () { + xit("returns an array with the nth element removed, and does not mutate the original", function () { + var array = ["bike", "car", "train", "bus"]; + expect(removeNthElement2(2, array)).toEqual(["bike", "car", "bus"]); + expect(array).toEqual(["bike", "car", "train", "bus"]); + }); +}); +describe("elementsStartingWithAVowel", function () { + xit("returns elements starting with a vowel", function () { + expect(elementsStartingWithAVowel(["apple", "bapple", "capple", "dapple", "epple", "fepple", "gepple", "hepple", "ipple", "jipple", "kipple", "lipple", "mipple", "nipple", "opple", "popple", "qopple", "ropple", "sopple", "topple", "upple", "vupple", "wupple", "xupple", "yupple", "zupple"])).toEqual(["apple", "epple", "ipple", "opple", "upple"]); + }); + xit("is case insensitive", function () { + expect(elementsStartingWithAVowel(["Apple", "Bapple", "Capple", "Dapple", "Epple", "Fepple", "Gepple", "Hepple", "Ipple", "Jipple", "Kipple", "Lipple", "Mipple", "Nipple", "Opple", "Popple", "Qopple", "Ropple", "Sopple", "Topple", "Upple", "Vupple", "Wupple", "Xupple", "Yupple", "Zupple"])).toEqual(["Apple", "Epple", "Ipple", "Opple", "Upple"]); + }); +}); +describe("removeSpaces", function () { + xit("returns the string with the space characters removed", function () { + expect(removeSpaces("this string has spaces")).toEqual("thisstringhasspaces"); + expect(removeSpaces(" this one has sneaky spaces ")).toEqual("thisonehassneakyspaces"); + }); +}); +describe("sumNumbers", function () { + xit("returns the sum of the numbers in the array", function () { + expect(sumNumbers([1, 3, 5, 6, 2, 8])).toEqual(25); + }); +}); +describe("sortByLastLetter", function () { + xit("sorts the string by the last character", function () { + expect(sortByLastLetter(["Lannister", "Stark", "Greyjoy", "Targaryen"])).toEqual(["Stark", "Targaryen", "Lannister", "Greyjoy"]); + }); +}); \ No newline at end of file diff --git a/test/dist/objects.test.dev.js b/test/dist/objects.test.dev.js new file mode 100644 index 00000000..ac9f7482 --- /dev/null +++ b/test/dist/objects.test.dev.js @@ -0,0 +1,159 @@ +"use strict"; + +var _require = require("../src/objects"), + createPerson = _require.createPerson, + getName = _require.getName, + getProperty = _require.getProperty, + hasProperty = _require.hasProperty, + isOver65 = _require.isOver65, + getAges = _require.getAges, + findByName = _require.findByName, + findHondas = _require.findHondas, + averageAge = _require.averageAge, + createTalkingPerson = _require.createTalkingPerson; + +describe("createPerson", function () { + xit("creates an object with the given name and age properties", function () { + expect(createPerson("Fred", 79)).toEqual({ + name: "Fred", + age: 79 + }); + expect(createPerson("Dilys", 50)).toEqual({ + name: "Dilys", + age: 50 + }); + }); +}); +describe("getName", function () { + xit("returns the name property of the object", function () { + expect(getName({ + name: "Fred", + age: 79 + })).toEqual("Fred"); + }); +}); +describe("getProperty", function () { + xit("returns the given property", function () { + expect(getProperty("age", { + name: "Fred", + age: 79 + })).toEqual(79); + }); +}); +describe("hasProperty", function () { + var fred = { + name: "Fred", + age: 79 + }; + it("returns true if the object has the given property", function () { + expect(hasProperty("age", fred)).toBe(true); + expect(hasProperty("favouriteColour", fred)).toBe(false); + }); +}); +describe("isOver65", function () { + xit("returns true if the person is aged over 65", function () { + var jim = { + name: "Jim", + age: 66 + }; + var dilys = { + name: "Dilys", + age: 50 + }; + var marjorie = { + name: "Marjorie", + age: 65 + }; + expect(isOver65(jim)).toBe(true); + expect(isOver65(dilys)).toBe(false); + expect(isOver65(marjorie)).toBe(false); + }); +}); +describe("getAges", function () { + xit("returns the ages of each person in the array", function () { + var jim = { + name: "Jim", + age: 66 + }; + var dilys = { + name: "Dilys", + age: 50 + }; + var marjorie = { + name: "Marjorie", + age: 65 + }; + expect(getAges([jim, dilys, marjorie])).toEqual([66, 50, 65]); + }); +}); +describe("findByName", function () { + it.only("returns the person with the given name", function () { + var jim = { + name: "Jim", + age: 66 + }; + var dilys = { + name: "Dilys", + age: 50 + }; + var marjorie = { + name: "Marjorie", + age: 65 + }; + expect(findByName("Jim", [jim, dilys, marjorie])).toBe(jim); + expect(findByName("Marjorie", [jim, dilys, marjorie])).toBe(marjorie); + }); +}); +describe("findHondas", function () { + xit("returns a list of cars manufactured by Honda", function () { + var car1 = { + manufacturer: "Honda", + year: 1997, + colour: "blue" + }; + var car2 = { + manufacturer: "Fiat", + year: 2010, + colour: "green" + }; + var car3 = { + manufacturer: "Toyota", + year: 2017, + colour: "blue" + }; + var car4 = { + manufacturer: "Honda", + year: 2001, + colour: "red" + }; + expect(findHondas([car1, car2, car3, car4])).toEqual([car1, car4]); + }); +}); +describe("averageAge", function () { + xit("returns the average age of the people in the list", function () { + var john = { + name: "John", + age: 60 + }; + var eric = { + name: "Eric", + age: 50 + }; + var gary = { + name: "Gary", + age: 25 + }; + expect(averageAge([john, eric, gary])).toBe(45); + }); +}); +describe("createTalkingPerson", function () { + xit("returns a person who can introduce themselves", function () { + var bill = createTalkingPerson("Bill", 40); + expect(bill).toEqual({ + name: "Bill", + age: 40, + introduce: expect.any(Function) + }); + expect(bill.introduce("Fred")).toEqual("Hi Fred, my name is Bill and I am 40!"); + }); +}); \ No newline at end of file diff --git a/test/numbers.test.js b/test/numbers.test.js index 94e3bbfb..760866fe 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); @@ -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..303573d1 100644 --- a/test/objects.test.js +++ b/test/objects.test.js @@ -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, @@ -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 diff --git a/test/strings.test.js b/test/strings.test.js index b10d44e3..7792e8c9 100644 --- a/test/strings.test.js +++ b/test/strings.test.js @@ -8,7 +8,7 @@ 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!"); }); @@ -16,13 +16,13 @@ describe("sayHello", () => { 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 a04926f9f954c4f03f9cc0c30d68f0d16e034187 Mon Sep 17 00:00:00 2001 From: Mahmoud Jeilani Date: Mon, 5 Jul 2021 18:58:11 +0100 Subject: [PATCH 2/2] added objects --- .gitignore | 1 + dist/testfile.dev.js | 34 ++++++++++ src/arrays.js | 36 ++++++++-- src/booleans.js | 52 ++++++++++++-- src/dist/booleans.dev.js | 143 +++++++++++++++++++++++++++++++++++++++ src/numbers.js | 15 +++- src/objects.js | 28 ++++++-- src/strings.js | 11 ++- test/objects.test.js | 2 +- 9 files changed, 304 insertions(+), 18 deletions(-) create mode 100644 dist/testfile.dev.js create mode 100644 src/dist/booleans.dev.js diff --git a/.gitignore b/.gitignore index 1f283afb..f797787f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ src/solutions.js package-lock.json +testfile.js diff --git a/dist/testfile.dev.js b/dist/testfile.dev.js new file mode 100644 index 00000000..95a5be79 --- /dev/null +++ b/dist/testfile.dev.js @@ -0,0 +1,34 @@ +"use strict"; + +// function iteration(n){ +// const emptyArray =[]; +// for( var i = 1; i <=n; i++ ){ +// // console.log(i); +// emptyArray.push(i) +// } +// console.log(emptyArray); +// } +// iteration(20); +var jim = { + name: "Jim", + age: 60 +}; +var dilys = { + name: "Dilys", + age: 50 +}; +var marjorie = { + name: "Marjorie", + age: 65 +}; + +var getAges = function getAges(people) { + // your code here + if (people.age > 65) { + return true; + } + + return false; +}; + +console.log(getAges(jim)); \ No newline at end of file diff --git a/src/arrays.js b/src/arrays.js index 822c49b7..2f04d586 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,61 +1,87 @@ const getNthElement = (index, array) => { - // your code here + return array[index]; }; const arrayToCSVString = array => { // your code here + return array.join(",") + }; const csvStringToArray = string => { // your code here + return string.split(","); + }; const addToArray = (element, array) => { - // your code here + array.push(element); }; const addToArray2 = (element, array) => { // your code here + const newArray = array.push(element) + console.log(newArray); }; const removeNthElement = (index, array) => { // your code here + return array.splice(index, 1); }; const numbersToStrings = numbers => { // your code here + + return numbers.toString().split(',') }; const uppercaseWordsInArray = strings => { // your code here + return strings.map(name => name.toUpperCase()); }; const reverseWordsInArray = strings => { // your code here + return strings.map(item => item.split('').reverse().join('')); + + }; const onlyEven = numbers => { // your code here + return numbers.filter(item => item % 2 === 0); }; const removeNthElement2 = (index, array) => { - // your code here -}; + // your code here + + +}; const elementsStartingWithAVowel = strings => { // your code here + const vowels = ['a','e','i','o','u']; + return vowels.map(function(vowel) { + return strings.find(function(string) { + return string.toLowerCase().charAt(0) === vowel; + }); + }); }; const removeSpaces = string => { // your code here + return string.replace(/\s+/g, '') }; const sumNumbers = numbers => { - // your code here + // your code here + return numbers.reduce((acc, curr) => acc + curr); }; const sortByLastLetter = strings => { // your code here + + return strings.sort((a, b) => a.charCodeAt(a.length - 1) - b.charCodeAt(b.length - 1)); }; module.exports = { diff --git a/src/booleans.js b/src/booleans.js index 9def206c..7e86d419 100644 --- a/src/booleans.js +++ b/src/booleans.js @@ -1,61 +1,100 @@ +/* eslint-disable func-names */ const negate = a => { // your code here + return !a; }; 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 + return !a && !b; }; const one = (a, b) => { // your code here + if (a || b || a) { + return true; + } + return false; }; const truthiness = a => { - // your code here + // your code her + if (a) { + return true; + } + return false; }; const isEqual = (a, b) => { // your code here + 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 + if (a % 2 !== 0) { + return true; + } + return false; }; const isEven = a => { // your code here + if (a % 2 == 0) { + return true; + // eslint-disable-next-line no-else-return + } else { + return false; + } }; const isSquare = a => { // your code here + return Math.sqrt(a) === Math.round(Math.sqrt(a)); }; -const startsWith = (char, string) => { +const startsWith = (string, char) => { // your code here + if (string.startsWith(char)) { + return true; + } + return false; }; -const containsVowels = string => { - // your code here +const containsVowels = function(string) { + const vowels = ['a','e','i','o','u']; +// var stringLowercase = string.toLowerCase() + for(i = 0; i < vowels.length; i++){ + if (vowels.includes(string.toLowerCase()[i])){ + return true; + } + } + return false; }; const isLowerCase = string => { // your code here + return string == string.toLowerCase() && string != string.toUpperCase(); }; module.exports = { @@ -75,3 +114,8 @@ module.exports = { containsVowels, isLowerCase }; + +// program to count the number of vowels in a string + +// defining vowels + diff --git a/src/dist/booleans.dev.js b/src/dist/booleans.dev.js new file mode 100644 index 00000000..9b7f2796 --- /dev/null +++ b/src/dist/booleans.dev.js @@ -0,0 +1,143 @@ +"use strict"; + +/* eslint-disable func-names */ +var negate = function negate(a) { + // your code here + return !a; +}; + +var both = function both(a, b) { + // your code here + return a && b; +}; + +var either = function either(a, b) { + // your code here + return a || b; +}; + +var none = function none(a, b) { + // your code here + return !a && !b; +}; + +var one = function one(a, b) { + // your code here + if (a || b || a) { + return true; + } + + return false; +}; + +var truthiness = function truthiness(a) { + // your code her + if (a) { + return true; + } + + return false; +}; + +var isEqual = function isEqual(a, b) { + // your code here + return a === b; +}; + +var isGreaterThan = function isGreaterThan(a, b) { + // your code here + return a > b; +}; + +var isLessThanOrEqualTo = function isLessThanOrEqualTo(a, b) { + // your code here + return a <= b; +}; + +var isOdd = function isOdd(a) { + // your code here + if (a % 2 !== 0) { + return true; + } + + return false; +}; + +var isEven = function isEven(a) { + // your code here + if (a % 2 == 0) { + return true; // eslint-disable-next-line no-else-return + } else { + return false; + } +}; + +var isSquare = function isSquare(a) { + // your code here + return Math.sqrt(a) === Math.round(Math.sqrt(a)); +}; + +var startsWith = function startsWith(string, _char) { + // your code here + if (string.startsWith(_char)) { + return true; + } + + return false; +}; + +var containsVowels = function containsVowels(string) { + var vowels = ["a", "e", "i", "o", "u"]; // convert vowels to same casing + + var _iteratorNormalCompletion = true; + var _didIteratorError = false; + var _iteratorError = undefined; + + try { + for (var _iterator = string.toLowerCase()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { + var letterInString = _step.value; + + if (vowels.includes(letterInString)) { + return true; + } + + return false; + } // your code here + + } catch (err) { + _didIteratorError = true; + _iteratorError = err; + } finally { + try { + if (!_iteratorNormalCompletion && _iterator["return"] != null) { + _iterator["return"](); + } + } finally { + if (_didIteratorError) { + throw _iteratorError; + } + } + } +}; + +var isLowerCase = function isLowerCase(string) {// your code here +}; + +module.exports = { + negate: negate, + both: both, + either: either, + none: none, + one: one, + truthiness: truthiness, + isEqual: isEqual, + isGreaterThan: isGreaterThan, + isLessThanOrEqualTo: isLessThanOrEqualTo, + isOdd: isOdd, + isEven: isEven, + isSquare: isSquare, + startsWith: startsWith, + containsVowels: containsVowels, + isLowerCase: isLowerCase +}; // program to count the number of vowels in a string +// defining vowels \ No newline at end of file diff --git a/src/numbers.js b/src/numbers.js index 028675f9..add1e427 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -1,45 +1,58 @@ const add = (a, b) => { - // your code here + // your code here + return a+b; }; const subtract = (a, b) => { // your code here + return a-b }; const multiply = (a, b) => { // your code here + return a*b }; const divide = (a, b) => { // your code here + return a/b }; const power = (a, b) => { // your code here + return a**b }; const round = a => { // your code here + return Math.round(a) }; const roundUp = a => { // your code here + return Math.ceil(a) + }; const roundDown = a => { // your code here + return Math.floor(a) }; const absolute = a => { // your code here + return Math.abs(a) }; const quotient = (a, b) => { // your code here + return Math.trunc(a/b) + }; const remainder = (a, b) => { // your code here + return a % b }; module.exports = { diff --git a/src/objects.js b/src/objects.js index 906eef8f..c6551ef9 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,43 +1,61 @@ -const createPerson = (name, age) => { - // your code here +const { TestScheduler } = require("jest-cli"); + + function createPerson(name, age) { + // your code here + + return { + name: name, + age: age + } + }; const getName = object => { // your code here + return object.name; }; const getProperty = (property, object) => { // your code here + + return object[property]; }; const hasProperty = (property, object) => { // your code here + return object.hasOwnProperty(property); }; const isOver65 = person => { // your code here +return person.age > 65; + }; const getAges = people => { // your code here + return people.map((ages)=> ages.age) }; const findByName = (name, people) => { // your code here + return people.find((item) => item.name === name) }; const findHondas = cars => { - // your code here + // your code here + return cars.filter(manafac => manafac.manufacturer === "Honda"); }; const averageAge = people => { - // your code here + // eslint-disable-next-line func-names + return people.reduce((acc, curr) => acc + curr.age, 0) / people.length; }; const createTalkingPerson = (name, age) => { // your code here }; - + module.exports = { createPerson, getName, diff --git a/src/strings.js b/src/strings.js index d3caabd8..dbad5062 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,25 +1,32 @@ -const sayHello = string => { +const sayHello = (string) => { // your code here + return 'Hello,' + ' ' + string + '!' }; -const uppercase = string => { +const uppercase = (string) => { + // your code here + return string.toUpperCase() }; const lowercase = string => { // your code here + return string.toLowerCase() }; const countCharacters = string => { // your code here + return string.length; }; const firstCharacter = string => { // your code here + return string.charAt(0) }; const firstCharacters = (string, n) => { // your code here + return string.substring(0,n) }; module.exports = { diff --git a/test/objects.test.js b/test/objects.test.js index 303573d1..fb5dbe35 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.only ("creates an object with the given name and age properties", () => { expect(createPerson("Fred", 79)).toEqual({ name: "Fred", age: 79