diff --git a/README.md b/README.md index d6da44d..5a33968 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # JavaScript Basics +This was lovingly coppied from [MCRcodes/javascript-basics](https://github.com/MCRcodes/javascript-basics) but modified for use in our 2022 bootcamp. All credit goes to [MCRCodes](https://github.com/MCRcodes) for the amazing work in this repo The repository contains test cases and empty function definitions which need to be populated to solve the different challenges set in this weeks walkthrough. 🚨*You should not update the tests!* 🚨 diff --git a/src/__tests__/arrays.test.js b/src/__tests__/arrays.test.js index d00a556..ed0ac6f 100644 --- a/src/__tests__/arrays.test.js +++ b/src/__tests__/arrays.test.js @@ -121,7 +121,7 @@ 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]); expect(onlyEven([8, 9, 10, 11, 12, 13, 14, 15])).toEqual([8, 10, 12, 14]); }); diff --git a/src/__tests__/booleans.test.js b/src/__tests__/booleans.test.js index 734201d..890770a 100644 --- a/src/__tests__/booleans.test.js +++ b/src/__tests__/booleans.test.js @@ -17,14 +17,14 @@ const { } = require('../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 booleans are true', () => { + it('returns true if both of the given booleans 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 booleans are true', () => { +it('returns true if at least one of the given booleans 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 booleans are true', () => { + it('returns true if neither of the given booleans 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 booleans are true', () => { + it('returns true if exactly one of the given booleans 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); @@ -86,7 +86,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); @@ -98,7 +98,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); @@ -108,7 +108,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); @@ -117,7 +117,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); @@ -126,7 +126,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(4)).toEqual(true); expect(isSquare(5)).toEqual(false); @@ -136,7 +136,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('c', 'aardvark')).toBe(false); expect(startsWith('b', 'baardvark')).toBe(true); @@ -146,7 +146,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); @@ -154,7 +154,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/src/__tests__/class.test.js b/src/__tests__/class.test.js index 64fc057..7fdc1d3 100644 --- a/src/__tests__/class.test.js +++ b/src/__tests__/class.test.js @@ -3,24 +3,24 @@ const { } = require('../class'); describe('sayHello', () => { - xit('returns "Hello world!" when passed "world"', () => { + it('returns "Hello world!" when passed "world"', () => { const obj = new testingClass('world'); expect(obj.sayHello()).toEqual('Hello, world!'); }); - xit('returns "Hello MCR Codes!" when passed "MCR Codes"', () => { + it('returns "Hello MCR Codes!" when passed "MCR Codes"', () => { const obj = new testingClass('MCR Codes'); expect(obj.sayHello()).toEqual('Hello, MCR Codes!'); }); - xit('returns "Hello fsghjdfkhgf!" when passed "fsghjdfkhgf"', () => { + it('returns "Hello fsghjdfkhgf!" when passed "fsghjdfkhgf"', () => { const obj = new testingClass('fsghjdfkhgf'); expect(obj.sayHello()).toEqual('Hello, fsghjdfkhgf!'); }); }); describe('uppercase', () => { - xit('returns the uppercased string', () => { + it('returns the uppercased string', () => { const obj1 = new testingClass('abc'); expect(obj1.uppercase()).toEqual('ABC'); const obj2 = new testingClass('def'); @@ -31,7 +31,7 @@ describe('uppercase', () => { }); describe('lowercase', () => { - xit('returns the lowercased string', () => { + it('returns the lowercased string', () => { const obj1 = new testingClass('ABC'); expect(obj1.lowercase()).toEqual('abc'); const obj2 = new testingClass('dEf'); @@ -42,7 +42,7 @@ describe('lowercase', () => { }); describe('countCharacters', () => { - xit('returns the number of characters in the string', () => { + it('returns the number of characters in the string', () => { const obj1 = new testingClass('fsfsgsfdg'); const obj2 = new testingClass('fsfsg'); const obj3 = new testingClass(''); @@ -54,7 +54,7 @@ describe('countCharacters', () => { }); describe('firstCharacter', () => { - xit('returns the first character of the string', () => { + it('returns the first character of the string', () => { const obj1 = new testingClass('ABC'); const obj2 = new testingClass('dEf'); const obj3 = new testingClass('GHi'); @@ -65,13 +65,13 @@ describe('firstCharacter', () => { }); describe('firstCharacters', () => { - xit('returns the first 4 characters of the string', () => { + it('returns the first 4 characters of the string', () => { const obj1 = new testingClass('sd32fg45'); expect(obj1.firstCharacters(4)).toEqual('sd32'); expect(obj1.firstCharacters(2)).toEqual('sd'); }); - xit('returns the first 2 characters of the string', () => { + it('returns the first 2 characters of the string', () => { const obj1 = new testingClass('asd'); expect(obj1.firstCharacters(2)).toEqual('as'); }); diff --git a/src/__tests__/objects.test.js b/src/__tests__/objects.test.js index 77abc37..1755397 100644 --- a/src/__tests__/objects.test.js +++ b/src/__tests__/objects.test.js @@ -12,7 +12,7 @@ const { } = require('../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', @@ -43,7 +43,7 @@ describe('getName', () => { }); describe('getProperty', () => { - xit('returns the given property', () => { + it('returns the given property', () => { expect( getProperty('age', { name: 'Fred', @@ -70,7 +70,7 @@ describe('hasProperty', () => { age: 23 }; - 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('name', tom)).toBe(true); expect(hasProperty('favouriteColour', fred)).toBe(false); @@ -79,7 +79,7 @@ describe('hasProperty', () => { }); 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 @@ -102,7 +102,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 @@ -125,7 +125,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 @@ -147,7 +147,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, @@ -179,7 +179,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 @@ -207,7 +207,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); const catherine = createTalkingPerson('Catherine', 21); expect(bill).toEqual({ diff --git a/src/__tests__/strings.test.js b/src/__tests__/strings.test.js index e2c3c88..b19fd73 100644 --- a/src/__tests__/strings.test.js +++ b/src/__tests__/strings.test.js @@ -8,21 +8,21 @@ const { } = require('../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'); }); }); diff --git a/src/arrays.js b/src/arrays.js index 822c49b..f6f7255 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,41 +1,55 @@ const getNthElement = (index, array) => { - // your code here + while (index >= array.length) { + index=index-array.length; + } + 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 +const addToArray = (element, array) => { + array.push(element) }; -const addToArray2 = (element, array) => { - // your code here +const addToArray2 = (element, array) => { + let array2 = [...array]; + array2.push(element) + return array2; }; -const removeNthElement = (index, array) => { - // your code here +const removeNthElement = (index, array) => { + array.splice(index, 1) + return array; }; const numbersToStrings = numbers => { - // your code here + let output = [] + for (i = 0; i < numbers.length; i++){ + const number = numbers[i] + output.push(number.toString()) + } + return output; }; const uppercaseWordsInArray = strings => { - // your code here + return }; const reverseWordsInArray = strings => { - // your code here + return }; const onlyEven = numbers => { - // your code here + const even = numbers.filter(number => { + return number % 2 ===0; + }) + return even }; const removeNthElement2 = (index, array) => { @@ -47,7 +61,8 @@ const elementsStartingWithAVowel = strings => { }; const removeSpaces = string => { - // your code here + return string.replace(/\s/g,'') + }; const sumNumbers = numbers => { diff --git a/src/booleans.js b/src/booleans.js index 5ff6cb5..0eda88c 100644 --- a/src/booleans.js +++ b/src/booleans.js @@ -1,61 +1,97 @@ function negate(a) { - // your code here + return !a }; function both(a, b) { - // your code here + return a && b }; function either(a, b) { - // your code here + return a || b }; function none(a, b) { - // your code here + return (a || b) == false }; function one(a, b) { - // your code here + if (a && b) { + return false + }; + return a || b }; function truthiness(a) { - // your code here + if (a) { + return true; + }else{ + return false; + } }; function isEqual(a, b) { - // your code here + return a === b }; function isGreaterThan(a, b) { - // your code here + return a > b }; function isLessThanOrEqualTo(a, b) { - // your code here + return a <= b }; function isOdd(a) { - // your code here + if (a % 2 === 0 ){ + return false; + } else { + return true; + } }; function isEven(a) { - // your code here + if (a%2 === 0){ + return true +} else { + return false +} }; function isSquare(a) { - // your code here + if(Math.sqrt(a) % 1 == 0){ + return true + }else { + return false + } }; function startsWith(char, string) { - // your code here + if(string.charAt(0) == char){ + return true + } else { + return false + } }; function containsVowels(string) { - // your code here + const checker = ["a", "e", "i", "o", "u" ] + string = string.toLowerCase() + for ( i = 0; i < checker.length; i ++) { + const letter = checker[i] + if (string.includes(letter)){ + return true + } + } + return false }; function isLowerCase(string) { - // your code here + if (string === string.toLowerCase()){ + return true + } else { + return false + } + }; module.exports = { diff --git a/src/class.js b/src/class.js index 70d541b..7eadf5d 100644 --- a/src/class.js +++ b/src/class.js @@ -3,27 +3,27 @@ class testingClass { this._string = string; } sayHello = () => { - // your code here + return `Hello, ${this._string}!` }; uppercase = () => { - // your code here + return this._string.toUpperCase() }; lowercase = () => { - // your code here + return this._string.toLowerCase() }; countCharacters = () => { - // your code here + return this._string.length }; firstCharacter = () => { - // your code here + return this._string.charAt(0) }; firstCharacters = (n) => { - // your code here + return this._string.substr(0, n) }; } diff --git a/src/numbers.js b/src/numbers.js index 9d192c9..804e1c3 100644 --- a/src/numbers.js +++ b/src/numbers.js @@ -1,37 +1,37 @@ function add (a, b) { - // your code here + return a+b } function subtract (a, b) { - // your code here + return a - b } function multiply (a, b) { - // your code here + return a * b } function divide (a, b) { - // your code here + return a / b } function power (a, b) { - // your code here + return Math.pow(a, b) } function round (a) { - // your code here + return Math.round(a) } function roundUp (a) { - // your code here + return Math.ceil(a) } function roundDown (a) { - // your code here + return Math.floor(a) } function absolute (a) { - // your code here + return Math.abs(a) } module.exports = { diff --git a/src/objects.js b/src/objects.js index 906eef8..93286ca 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,43 +1,102 @@ const createPerson = (name, age) => { - // your code here + const person = { + name: name, + age: age, + } + return person }; const getName = object => { - // your code here + return object.name }; const getProperty = (property, object) => { - // your code here + if(property == "name"){ + return object.name + } else if (property == "age"){ + return object.age + } }; const hasProperty = (property, object) => { - // your code here + if(property == "name"){ + return true + } else if (property == "age"){ + return true + } else { + return false + } }; const isOver65 = person => { - // your code here + if (person.age > 65){ + return true + } else { + return false + } }; const getAges = people => { - // your code here + let output = [] + for (i = 0; i < people.length; i++){ + const person = people[i] + const age = person.age + output.push(age) + } + return output }; const findByName = (name, people) => { - // your code here + for (i = 0; i < people.length; i++){ + const person = people[i] + if (person.name == name){ + return person + } + + } }; const findHondas = cars => { - // your code here + let output= [] + for (i = 0; i < cars.length; i++){ + const car = cars[i] + if (car.manufacturer == "Honda"){ + output.push(car) + } + + }; + return output }; const averageAge = people => { - // your code here + let NoPeople = 0 + let TotalAge = 0 + for (i = 0; i < people.length; i++){ + const person = people[i] + TotalAge = TotalAge + person.age + NoPeople = NoPeople + 1 + } + return (TotalAge/NoPeople) }; const createTalkingPerson = (name, age) => { - // your code here + let person = { + introduce: (hi) => { + return "Hi " + hi + ", my name is " + name + " and I am " + age + "!" + }, + name: name, + age: age + } + return person }; + +//{ + //a: variable, + //b: function() {}, + //c: variable + //} + module.exports = { createPerson, getName, diff --git a/src/strings.js b/src/strings.js index ce02aff..78ca395 100644 --- a/src/strings.js +++ b/src/strings.js @@ -1,25 +1,25 @@ -function sayHello (string) { - // your code here +function sayHello(string) { + return "Hello, " + string + "!" }; -function uppercase (string) { - // your code here +function uppercase(string) { + return string.toUpperCase() }; -function lowercase (string) { - // your code here +function lowercase(string) { + return string.toLowerCase() }; -function countCharacters (string) { - // your code here +function countCharacters(string) { + return string.length }; -function firstCharacter (string) { - // your code here +function firstCharacter(string) { + return string[0] }; -function firstCharacters (string, n) { - // your code here +function firstCharacters(string, n) { + return string.substring(0,n) }; module.exports = {