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

Osman Elsahib- TDD #68

Open
wants to merge 4 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((number)=> number +1)
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one

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)
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here 👍


module.exports = getWordLengths;
7 changes: 6 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,7 @@
function addNumbers(numbers) {}
function addNumbers(numbers) {
return numbers.reduce(function (a, b) {
return a + b;
}, 0);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect


module.exports = addNumbers;
9 changes: 8 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,10 @@
function findNeedle(words) {}
function findNeedle(words) {
let result;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you look at the test, you have two arguments passed, expecting two parameters.

const lookForWords = ["needle", "plant"]
words.forEach((element, index) => {
lookForWords.includes(element) ? result = index : "Not found"
});
return result
}

module.exports = findNeedle;
10 changes: 9 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,14 @@
// calculate and return the factorial of int
// note: factorial of 0 is 1

function factorial(int) {}
function factorial(int) {
// let result = 1, i = int;
if (int === 0 || int === 1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while int is not reserved anymore in JS 5, it is reserved word in previous versions afaik
it's also reserved in many other languages. so while parameter is coming as int, it can be useful to save it in another variable

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this will save you the trouble of the first conditions, as you can initialize it with 1

return 1;
for (var i = int - 1; i >= 1; i--) {
int *= i;
}
return int;
}

module.exports = factorial;
7 changes: 7 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,7 @@
function removeMiddle(array) {
if (array.length >= 3) {
let index = (array.length - 1) / 2
return array.splice(index, 1)
} else { return "Can't work with such short array" }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the creativity, but why can't work with short arrays
also what if it's not odd number ?

}
module.exports = removeMiddle;
8 changes: 8 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,8 @@
function secondLargest(array) {
let actualMax = Math.max(...array)
let maxIndex = array.indexOf(actualMax)
array.splice(maxIndex, 1)
return Math.max(...array)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work, and works fine
the only a couple of issues, the complexity is high here
You needed to loop through the array twice to get the max
also what if max number is duplicated ?


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 getAverage(array) {
let numArray = [];
array.forEach(element => {
if (typeof element == "number") {
numArray.push(element)
}
});
let sum = numArray.reduce(function (a, b) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nicely done
you can make it fast using one loop also to do both checking if it's number and adding it

return a + b;
}, 0);
return sum / numArray.length

}
module.exports = getAverage;
24 changes: 24 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,24 @@
function carSales(array) {
let result = {}, ford = 0, honda = 0, rover = 0, toyota = 0;
array.forEach(element => {
if (element.make === "Ford") {
ford = ford + element.price
} else if (element.make === "Honda") {
honda = honda + element.price
} else if (element.make === "Land Rover") {
rover = rover + element.price
debugger
} else {
toyota = toyota + element.price
}

});
result.Ford = ford
result.Honda = honda
result["Land Rover"] = rover
result.Toyota = toyota

return result
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good work
you can add more test to make it fail
and move from hard-coding the car modules to program them in


}
module.exports = carSales;
7 changes: 7 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,7 @@
function paintCars(array, color) {
// I didn't understand what is required to be done so I hardcoded the index
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a tricky one
they expect you to change the color of the Ford model (or first car) to the color they choose

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remember it should not change the original object colour
if you stuck let me know for more hints

newarray = [...array]
newarray[0] = { ...newarray[0], colour: color }
return newarray
}
module.exports = paintCars;
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 cities(array, func) {
return array.map(item => func(item))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct
also when writing map, you don't need to create it as array function if you're passign a function
something like
array.map(func)

}
module.exports = cities;
17 changes: 8 additions & 9 deletions I.pass-tests/ES-6-practice/es6.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
// Turn this function into an arrow function
function arrow() {
return "es6 is awesome!";
}
const arrow = () => { return "es6 is awesome!" };


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

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

//use destructuring to return the object's cyf property
function destructuring(obj) {
return obj.cyf;
function destructuring({ cyf }) {
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).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
8 changes: 4 additions & 4 deletions II.write-tests/01-greet-people/greet-people.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function greetPeople(people) {
var greeting = "Hello ";

people.forEach(function(person) {
greeting = greeting + person;
var myarray = []
people.forEach(function (person) {
myarray.push(greeting + person);
});

return greeting;
return myarray;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work, while forEach is pushing to another array (not quite its role)
something like map will help you better

}

module.exports = greetPeople;
Expand Down
9 changes: 8 additions & 1 deletion II.write-tests/01-greet-people/greet-people.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
test("print list of names prefixed with Hello", function() {
const greetPeople = require("./greet-people.js")

test("print list of names prefixed with Hello", function () {
// Arrange
const mentors = ['Irina', 'Ashleigh', 'Etza'];
const expected = ['Hello Irina', 'Hello Ashleigh', 'Hello Etza'];
// Act
const result = greetPeople(mentors)

// Assert
expect(result).toEqual(expected)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work

});
16 changes: 8 additions & 8 deletions II.write-tests/02-remove-vowels/remove-vowels.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ function removeVowels(word) {

var result = [];

characters.forEach(function(character) {
characters.forEach(function (character) {
if (
character === "a" ||
character === "o" ||
character === "i" ||
character === "e" ||
character === "u"
character.toLowerCase() === "a" ||
character.toLowerCase() === "o" ||
character.toLowerCase() === "i" ||
character.toLowerCase() === "e" ||
character.toLowerCase() === "u"
) {
result.push(character);
result.push("");
} else {
result.push("_");
result.push(character);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea for those tests, is to add more tests to make your functions work better. So try to do that and we can review them later on

});

Expand Down
7 changes: 6 additions & 1 deletion II.write-tests/02-remove-vowels/remove-vowels.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
test("remove vowels from word", function() {
const removeVowels = require("./remove-vowels.js")

test("remove vowels from word", function () {
// Arrange
const expected = "sml"
// Act
const result = removeVowels('samuel');
// Assert
expect(result).toEqual(expected)
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
test("remove vowels from all words in array", function() {
const removeVowelsForWords = require("./remove-vowels-in-array")

test("remove vowels from all words in array", function () {
// Arrange
const expected = ["rn", "tz", "Dnl"]
const input = ["Irina", "Etza", "Daniel"]
// Act
const result = removeVowelsForWords(input)
// Assert
expect(result).toEqual(expected)
});

// example
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
var largerThanTen = require("./numbersGreaterThan10");

test("Get numbers greater than 10", function() {});
test("Get numbers greater than 10", function () {
const input = [4, 10, 32, 9, 21],
expected = [32, 21],
result = largerThanTen(input)

expect(result).toEqual(expected)
});


// input: [4, 10, 32, 9, 21];
// expected output: [32, 21];
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const getSecondThird = require("./get-second-third")

test("Move the second to the third position", function () {
const input = [90, 5, 11, 8, 6],
expected = [6, 8],
result = getSecondThird(input)

expect(result).toEqual(expected)
});
// example
// input = [90, 5, 11, 8, 6];
// expected output = [6, 8];
Expand Down
7 changes: 7 additions & 0 deletions II.write-tests/06-largest-number/largest-number.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const largestNumber = require("./largest-number")
test("Return the largest Number", function () {
const input = [3, 21, 88, 4, 36],
expected = 88,
result = largestNumber(input)
expect(result).toEqual(expected)
})
// example
// input: [3, 21, 88, 4, 36];
// expected: 88;
Expand Down
9 changes: 9 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,12 @@
const getEvenNumbers = require("./get-even-numbers")

test("the even number", function () {
const input = [22, 13, 73, 82, 4],
expected = [22, 82, 4],
result = getEvenNumbers(input)
expect(result).toEqual(expected)
})

// example
// input: [22, 13, 73, 82, 4];
// expected: [22, 82, 4];
8 changes: 4 additions & 4 deletions II.write-tests/09-test-async/async-1.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const getRepos = require("./async-1");

test("gets a list of repositories names", function() {
test("gets a list of repositories names", function () {
// arrange
var url = "https://api.github.com/users/kabaros/repos";
// act
return getRepos(url).then(function(result) {
return getRepos(url).then(function (result) {
// assert
expect(result).toContain("js-exercises");
expect(result).toContain("dom-ajax-repo");
expect(result).toContain("master-reference");
expect(result).toContain("sword");
});
});
Loading