Skip to content
Closed
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
38 changes: 35 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,41 @@
// or 'list' has mixed values (the function is expected to sort only numbers).

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
let newArray = [];
if(!Array.isArray(list))
{
return null;
}

for (const item of list)
{
if (typeof item === 'number')
{
newArray.push(item);
}

}
if (newArray.length === 0)
{
return null;
}
newArray = newArray.sort((a, b) => a-b);
if(newArray.length %2 === 0)
{
const rightIndex = newArray.length/2
const leftIndex = rightIndex - 1;
const median = (newArray[rightIndex] + newArray[leftIndex])/2;
return median;

}
else{

const middleIndex = Math.floor(newArray.length/2);
return newArray[middleIndex]
}
//const middleIndex = Math.floor(newArray.length / 2);
//const median = list.splice(middleIndex, 1)[0];
//return newArray[middleIndex];
}

module.exports = calculateMedian;
18 changes: 17 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
function dedupe() {}
function dedupe(list) {
if (list.length=== 0)
{
return [];
}
let result = [];
for (let item of list)
{
if(!result.includes(item))
{
result.push(item);
}
}
return result;

}
module.exports = dedupe;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2]
// When passed to the dedupe function
// Then it should return an empty array
test.todo("given an empty array, it returns an empty array");

test("given an empty array, it returns an empty array", ()=>{
const list = [];
expect(dedupe(list)).toEqual([]);
});
test
("array with no duplicate should return copy of original", ()=>{
let list = [1,2,4,6,9,8];
expect(dedupe(list)).toEqual([1,2,4,6,9,8]);
});
test("shoiuld remove the duplicate value and preserving the first occurebce",()=>{
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5,1,2,3,8]);
})
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
Expand Down
15 changes: 15 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function findMax(elements) {
if(elements.length === 0)
{
return -Infinity;
}
let max = -Infinity ;
for (const item of elements)
{
if (typeof item === 'number')
{
max = Math.max(max , item);
}


}
return max;
}

module.exports = findMax;
29 changes: 28 additions & 1 deletion Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,34 @@ const findMax = require("./max.js");
// When passed to the max function
// Then it should return -Infinity
// Delete this test.todo and replace it with a test.
test.todo("given an empty array, returns -Infinity");
test("given an empty array should return -Infinity", () => {
const list = [];
expect(findMax(list)).toEqual(-Infinity);
});
test("Array with one number should return that number", () => {
const list =[7];
expect(findMax(list)).toEqual(7);
});
test("An array with both positive and negative number should return the largest overall", () =>{
const list = [-10, 5, 30, -3];
expect(findMax(list)).toEqual(30);
});
test("Array with just negative number should return the closest on to zero", () =>{
const list = [-10, -3, -20,-7,-4]
expect(findMax(list)).toEqual(-3);
});
test("An array with decimal number should return largest decimal number", ()=>{
const list = [1.2, 3.5, 2.8]
expect(findMax(list)).toEqual(3.5);
});
test("An array with non-numeric values should return the max and ignore non-numeric", () =>{
const list = ['hey', 10, 'hi', 60, 50];
expect(findMax(list)).toEqual(60);
});
test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> {
const list = ['a', 'b', 'hello'];
expect(findMax(list)).toEqual(-Infinity);
})

// Given an array with one number
// When passed to the max function
Expand Down
15 changes: 15 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
function sum(elements) {
if (elements.length === 0)
{
return 0;
}
let total = 0 ;
for (const item of elements)
{
if (typeof item === 'number')
{
total = total + item;
}


}
return total;
}

module.exports = sum;
29 changes: 28 additions & 1 deletion Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,34 @@ const sum = require("./sum.js");
// Given an empty array
// When passed to the sum function
// Then it should return 0
test.todo("given an empty array, returns 0")
test("given an empty array should return -Infinity", () => {
const list = [];
expect(sum(list)).toEqual(0);
});
test("Array with one number should return that number", () => {
const list =[7];
expect(sum(list)).toEqual(7);
});
test("An array with both positive and negative number should return the largest overall", () =>{
const list = [-10, 5, 30, -3];
expect(sum(list)).toEqual(22);
});
test("Array with just negative number should return the closest on to zero", () =>{
const list = [-10, -3, -20,-7,-4]
expect(sum(list)).toEqual(-44);
});
test("An array with decimal number should return largest decimal number", ()=>{
const list = [1.2, 3.5, 2.8]
expect(sum(list)).toEqual(7.5);
});
test("An array with non-numeric values should return the max and ignore non-numeric", () =>{
const list = ['hey', 10, 'hi', 60, 50];
expect(sum(list)).toEqual(120);
});
test("An array with non numeric value should return the least surprising value given how it behaves for all other inputs", ()=> {
const list = ['a', 'b', 'hello'];
expect(sum(list)).toEqual(0);
})

// Given an array with just one number
// When passed to the sum function
Expand Down
3 changes: 1 addition & 2 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Refactor the implementation of includes to use a for...of loop

function includes(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];
for (const element of list) {
if (element === target) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);
13 changes: 9 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// The program throws an error because a for...of loop only works with iterable values like arrays or strings.
// The author variable is an object, which is not iterable by default, so JavaScript cannot loop through it.
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

Expand All @@ -10,7 +11,11 @@ const author = {
age: 40,
alive: true,
};

for (const value of author) {
console.log(value);
const values = Object.values(author);
for(const item of values)
{
console.log(item);
}



14 changes: 10 additions & 4 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

//The title and serves values print correctly because the properties are accessed directly using
// recipe.title and recipe.serves.
// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
// How can you fix it?
Expand All @@ -10,6 +11,11 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);
for(const ingredients of recipe.ingredients)
{
console.log(`*${ingredients}`)
}

//The program prints the recipe title and number of servings.
// Then it loops through the ingredients array and logs each ingredient on a new line so they are displayed individually instead of printing the whole object.
20 changes: 19 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function contains() {}
function contains(object, propertyName) {
if(typeof object !== 'object' || Array.isArray(object) || object=== null)
{
throw new Error("It is a invalid object")
}
const newArray = Object.keys(object);
for(let item of newArray)
{
if (item === propertyName)
{
return true
}



}
return false;
}


module.exports = contains;
32 changes: 32 additions & 0 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

test("An empty object should return false",()=>{
const object = {};
const propertyName = "a";
expect(contains(object, propertyName)).toEqual(false);
});


test("returns true when the object contains an existing property name", () => {
const object = { a: 1, b: 2, c: 3 };
const propertyName = "a";
expect(contains(object, propertyName)).toEqual(true);
});
test("returns false when the object not contains an existing property name", () => {
const object = { a: 1, b: 2, c: 3 };
const propertyName = "d";
expect(contains(object, propertyName)).toEqual(false);
});
test("Should throw an error for an invalid parameters", () => {
const object = [1,2,3,4,5,6,7];
const propertyName = "a";
expect(() => contains(object, propertyName)).toThrow();
});
test("Should throw an error for an invalid parameters", () => {
const object = null;
const propertyName = "a";
expect(() => contains(object, propertyName)).toThrow();
});
test("Should throw an error for an invalid parameters", () => {
const object = "hi";
const propertyName = "a";
expect(() => contains(object, propertyName)).toThrow();
});
// Acceptance criteria:

// Given a contains function
Expand Down
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const countryCurrencyLookup = {};

for (let item of countryCurrencyPairs) {
const [key, value] = [item[0], item[1]];
countryCurrencyLookup[key] = value;
}
return countryCurrencyLookup;
}

module.exports = createLookup;
9 changes: 9 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");

test("An array of arrays representing country code and currency code should return an object where the keys are country code values are currency code",()=>{
const countryCurrencyPairs = [['IND', 'INR'], ['US', 'USD'], ['UK', 'GBR'], ['CA','CAD'] ];
expect(createLookup(countryCurrencyPairs)).toEqual({
IND: "INR",
US: "USD",
UK: "GBR",
CA: "CAD",
});
})
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
6 changes: 4 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ function parseQueryString(queryString) {
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
for (const item of keyValuePairs) {
const pair = item.split("=");
const key = pair.shift();
const value = pair.join("=");
queryParams[key] = value;
}

Expand Down
15 changes: 15 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});
test("An empty string should return an empty object",()=>{
expect(parseQueryString("")).toEqual({});
});
test("parses querystring parameters with an empty value",()=>{
expect(parseQueryString("name=")).toEqual({name :""});
});
test("handles querystring parameters that have no = sign",()=>{
expect(parseQueryString("flag")).toEqual({flag:""})
});
test("parses querystring values containing multiple = characters",()=>{
expect(parseQueryString("token=a=b=c=d")).toEqual({token:"a=b=c=d"});
});
test("handles multiple querystring parameters when one value contains =",()=>{
expect(parseQueryString("a=1&equation=x=y+1")).toEqual({a:"1", equation:"x=y+1"});
});
Loading
Loading