From 211f0c31c413e561606b5844b93667f29eeddc50 Mon Sep 17 00:00:00 2001 From: Mahmoud Jeilani Date: Tue, 6 Jul 2021 00:47:24 +0100 Subject: [PATCH] Branch on main fork --- src/arrays.js | 40 ++++++++++++++++++++++++++++----- src/booleans.js | 52 +++++++++++++++++++++++++++++++++++++++---- src/numbers.js | 53 ++++++++++++++++++++++++++------------------ src/objects.js | 28 ++++++++++++++++++----- src/strings.js | 11 +++++++-- test/arrays.test.js | 34 ++++++++++++++-------------- test/objects.test.js | 20 ++++++++--------- 7 files changed, 173 insertions(+), 65 deletions(-) diff --git a/src/arrays.js b/src/arrays.js index 822c49b7..8489ebb1 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,61 +1,91 @@ const getNthElement = (index, array) => { - // your code here + + let arrayIndex = index; + let arrayLenght = array.length; + if (arrayIndex < arrayLenght) { + return array[arrayIndex]; + } + return Array[arrayLenght -(arrayIndex)]; + }; 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 + return array.concat(element); }; 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/numbers.js b/src/numbers.js index 028675f9..63412eb9 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -1,57 +1,68 @@ const add = (a, b) => { // your code here + return a + b; }; const subtract = (a, b) => { - // your code here +// your code here + return a - b; }; const multiply = (a, b) => { - // your code here +// your code here + return a * b; }; const divide = (a, b) => { - // your code here +// your code here + return a / b; }; const power = (a, b) => { - // your code here +// your code here + return a ** b; }; const round = a => { - // your code here +// your code here + return Math.round(a); }; const roundUp = a => { - // your code here +// your code here + return Math.ceil(a); }; const roundDown = a => { - // your code here +// your code here + return Math.floor(a); }; const absolute = a => { - // your code here +// your code here + return Math.abs(a); }; const quotient = (a, b) => { - // your code here +// your code here + return Math.trunc(a / b); }; const remainder = (a, b) => { - // your code here +// your code here + return a % b; }; module.exports = { - add, - subtract, - multiply, - divide, - power, - round, - roundUp, - roundDown, - absolute, - quotient, - remainder +add, +subtract, +multiply, +divide, +power, +round, +roundUp, +roundDown, +absolute, +quotient, +remainder }; diff --git a/src/objects.js b/src/objects.js index 906eef8f..46629475 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,43 +1,59 @@ -const createPerson = (name, age) => { - // your code here -}; + + 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/arrays.test.js b/test/arrays.test.js index d8723452..beb8dee0 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]; @@ -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]; @@ -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/objects.test.js b/test/objects.test.js index 6fc32339..4e5daf18 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, @@ -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",