Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LONDON 10-ANU THAPALIYA-js-exercises-tdd #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion I.pass-tests/01-add-one/add-one.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module.exports = function(numbers) {};
module.exports = function(numbers) {
return numbers.map((num => num + 1));
};
4 changes: 3 additions & 1 deletion I.pass-tests/02-get-word-lengths/get-word-lengths.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var getWordLengths = function(someWords) {};
var getWordLengths = function(someWords) {
return someWords.map((word)=> word.length);
};

module.exports = getWordLengths;
5 changes: 4 additions & 1 deletion I.pass-tests/03-sum-numbers/add-numbers.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
function addNumbers(numbers) {}
function addNumbers(numbers) {
return numbers.reduce((sum, num) => sum + num, 0);

}
module.exports = addNumbers;
4 changes: 2 additions & 2 deletions I.pass-tests/03-sum-numbers/add-numbers.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var addAllnumbers = require("./add-numbers");
var addNumbers = require("./add-numbers");

test("Add all numbers", function() {
var numbers = [9, 23, 10, 3, 8];
var expected = 53;

var output = addAllnumbers(numbers);
var output = addNumbers(numbers);

expect(output).toEqual(expected);
});
4 changes: 3 additions & 1 deletion I.pass-tests/04-find-needle/find-needle.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function findNeedle(words) {}
function findNeedle(words, wordToFind) {
return words.indexOf(wordToFind);
}

module.exports = findNeedle;
12 changes: 11 additions & 1 deletion I.pass-tests/05-factorial/factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
// calculate and return the factorial of int
// note: factorial of 0 is 1

function factorial(int) {}
function factorial(int) {
if(int === 0 || int === 1){
return 1;
}else{
var result = 1;
for(var i = 2; i<=int; i++) {
result *= i;
}
return result;
}
}

module.exports = factorial;
9 changes: 9 additions & 0 deletions I.pass-tests/06-remove-middle/remove-middle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function removeMiddle(arr) {
// find the index of the middle element.
let middleIndex = Math.floor(arr.length / 2);
// Remove the middle element from the array
let removedElement = arr.splice(middleIndex, 1);
// return the removed element
return removedElement;
}
module.exports = removeMiddle;
16 changes: 16 additions & 0 deletions I.pass-tests/07-second-largest/second-largest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function secondLargest(numbers) {
var max = -Infinity;
var secondMax = -Infinity;

for (let i = 0; i < numbers.length; i++){
if (numbers[i] > max){
secondMax = max;
max = numbers[i];
}else if(numbers[i] > secondMax && numbers[i] !== max) {
secondMax = numbers[i];
}
}
return secondMax;
}

module.exports = secondLargest;
14 changes: 14 additions & 0 deletions I.pass-tests/08-get-average/get-average.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// the input is an array of numbers and strings
// return the average of all the numbers
// be sure to exclude the strings
function average(numbers) {
let sum = 0;
let count = 0;

for (let i = 0; i < numbers.length; i++){
if(typeof numbers[i] === "number"){
sum += numbers[i];
count ++;
}
}
var avg = sum / count;
return avg;
}
module.exports = average;
15 changes: 15 additions & 0 deletions I.pass-tests/09-car-sales/car-sales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function sales(carsSold) {
var totals = {};

for (let i = 0; i < carsSold.length; i++){
let car = carsSold[i]
if(!totals[car.make]) {
totals[car.make] = car.price;
}else{
totals[car.make] += car.price;
}
}
return totals
}

module.exports = sales;
19 changes: 19 additions & 0 deletions I.pass-tests/10-paint-ford/paint-cars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function paintShop(cars, color) {
var repaintedCars = [];

for(let i = 0; i < cars.length; i++) {
var car = cars[i];


if(car.color !== color) {
let repaintedCar = {...car, color: color };
repaintedCars.push(repaintedCar);
}else{
repaintedCars.push(car);
}
}

return repaintedCars;
}

module.exports = paintShop;
4 changes: 4 additions & 0 deletions I.pass-tests/11-cities-formatter/cities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function formatCities(cities, transform) {
return cities.map(transform);
}
module.exports = formatCities;
14 changes: 9 additions & 5 deletions I.pass-tests/ES-6-practice/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ function arrow() {
}

//Use the es6 syntax for default parameter
function defaultParameter(name) {
var name = name || "sam";
function defaultParameter(name = "sam") {
// var name = name || "sam";
return name;
}

// Use the spread operator to combine arr1 and arr2
function combineArrays(arr1, arr2) {
return arr1.concat(arr2);
return [...arr1, ...arr2];
// return arr1.concat(arr2);
}

//use destructuring to return the object's cyf property
function destructuring(obj) {
return obj.cyf;
// return obj.cyf;
const { cyf } = obj;
return cyf;
}

// use template literal to return a string with the sum of a and b
function templateString(a, b) {
return "The sum is equal to " + (a + b).toString();
return `The sum is equal to ${a + b}`;
// return "The sum is equal to " + (a + b).toString();
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion I.pass-tests/ES-6-practice/es6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
combineArrays,
destructuring,
templateString
} = require("./index");
} = require("./es6");

test("function arrow is instance of a function", () => {
expect(arrow).toBeInstanceOf(Function);
Expand Down
5 changes: 5 additions & 0 deletions II.write-tests/01-greet-people/greet-people.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const greetPeople = require("./greet-people");

test("print list of names prefixed with Hello", function() {
// Arrange
var teachers = ["Anu", "Junita", "Saliha"];
// Act
var result = greetPeople(teachers);
// Assert
expect(result).toBe("Hello AnuJunitaSaliha")
});
4 changes: 4 additions & 0 deletions II.write-tests/02-remove-vowels/remove-vowels.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import removeVowels from "./remove-vowels";
test("remove vowels from word", function() {
// Arrange
var result = removeVowels("anu");
// Act
var expected = "_n_";
// Assert
expect(result).toBe(expected);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var removeVowels = require("../02-remove-vowels/remove-vowels");

function removeVowelsForWords(words) {
var result = words.map(function(word) {
return removeVowels(word);
return removeVowels(word.toLowerCase());
});

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import removeVowelsForWords from "./remove-vowels-in-array";
test("remove vowels from all words in array", function() {

// Arrange
var words = ["Anu", "Junita", "Saliha"];
// Act
var result = removeVowelsForWords(words);
// Assert
var expected = ["n","Jnt", "Slh"];
expect(result).toEqual(expected);
});
// const removeVowelsForWords = require('./remove-vowels-in-array');

// test("remove vowels from all words in array", function() {
// // Arrange
// var words = ["Irina", "Etza", "Daniel"];

// // Act
// var result = removeVowelsForWords(words);

// // Assert
// var expected = ["rn", "tz", "Dnl"];
// expect(result).toEqual(expected);
// });

// example
// input: ["Irina", "Etza", "Daniel"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
var largerThanTen = require("./numbersGreaterThan10");

test("Get numbers greater than 10", function() {});
test("Get numbers greater than 10", function() {
// Arrange
var numbers = [4, 10, 32, 9, 21];
// Act
var result = largerThanTen(numbers);
// Assert
var expected = [32, 21];
expect(result).toEqual(expected);
});

// input: [4, 10, 32, 9, 21];
// expected output: [32, 21];
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@
// expected output = [6, 8];

// also test that the original array has not changed
var getSecondAndThird = require("./get-second-third");
test("Get second and third elements from array", function() {
// Arrange
var array = [90, 5, 11, 8, 6];
var originalArray = array.slice();
// Act
var result = getSecondAndThird(array);

// Assert
var expected = [6, 8];
expect(result).toEqual(expected);
expect(array).toEqual(originalArray);
});
16 changes: 12 additions & 4 deletions II.write-tests/06-largest-number/largest-number.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// example
// input: [3, 21, 88, 4, 36];
// expected: 88;
var getLargestNumber = require("./largest-number");
test("Get the largest number from the array", function() {
// Arrange
var array = [3, 21, 88, 4, 36];
var originalArray = array.slice();
// Act
var result = getLargestNumber(array);

// also test that the original array hasn't changed
// Assert
var expected = 88;
expect(result).toEqual(expected);
expect(array).toEqual(originalArray);
});
11 changes: 11 additions & 0 deletions II.write-tests/07-get-even-numbers/get-even-numbers.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// example
// input: [22, 13, 73, 82, 4];
// expected: [22, 82, 4];
const getEven = require("./get-even-numbers")

test("Get even numbers", function(){
// Arrange
const numbers = [22, 13, 73, 82, 4];
// Act
const result = getEven(numbers);
// Assert
const expected = [22, 82, 4];
expect(result).toEqual(expected);
});
Loading