Skip to content
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
26 changes: 23 additions & 3 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,29 @@
// 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;
// validate the array
if (!Array.isArray(list)){
return null;
}
// keep only the real number by filtering the array( excluding string, null, undefined )
const numbersOnly = list.filter((item) => typeof item === 'number')
// after filtering return null if nothing is valid
if (numbersOnly.length === 0){
return null ;
}
const sorted =[...numbersOnly].sort((a,b) => a-b)
const middleIndex = Math.floor(sorted.length / 2)


if (sorted.length %2 == 0){
return (sorted[middleIndex-1] + sorted[middleIndex])/2;
}else{
return sorted[middleIndex]
}




}

module.exports = calculateMedian;
13 changes: 12 additions & 1 deletion Sprint-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
function dedupe() {}
function dedupe(arr) {
// initialize an empty array
let dedupedArr =[];
for( element of arr){
if(!dedupedArr.includes(element)){
dedupedArr.push(element);
}

}
return dedupedArr
}
module.exports = dedupe;
12 changes: 11 additions & 1 deletion Sprint-1/implement/dedupe.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const dedupe = require("./dedupe.js");

/*
Dedupe Array

Expand All @@ -16,13 +17,22 @@ E.g. dedupe([1, 2, 1]) returns [1, 2]
// Given an empty array
// 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", ()=>{
expect(dedupe([])).toEqual([])
});

// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
test("Given an array with no duplicates, returns a copy of the original array", ()=>{
expect(dedupe([3,4,6])).toEqual([3,4,6])

})
// Given an array of strings or numbers
// When passed to the dedupe function
// Then it should return a new array with duplicates removed while preserving the
// first occurrence of each element from the original array.
test('Given an array of strings or numbers, returns a new array with duplicates removed while preserving the first occurrence of each element from the original array',()=>{
expect(dedupe(['h',4,'k','h',3,5,4])).toEqual(['h',4,'k',3,5])
}
)
11 changes: 11 additions & 0 deletions Sprint-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function findMax(elements) {
const numbersOnly = elements.filter((item)=> typeof item === 'number' && !Number.isNaN(item));

if (numbersOnly.length ===0 ){
return -Infinity
}
const sorted = [...numbersOnly].sort((a,b) => a-b)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good idea to use sorting

const max = sorted[sorted.length-1]

return max


}

module.exports = findMax;
25 changes: 22 additions & 3 deletions Sprint-1/implement/max.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,47 @@ 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, returns -Infinity", ()=>{
expect(findMax([])).toBe(-Infinity)
});

// Given an array with one number
// When passed to the max function
// Then it should return that number
test('Given an array with one number, returns that number', () =>{
expect(findMax([22])).toBe(22)
} )

// Given an array with both positive and negative numbers
// When passed to the max function
// Then it should return the largest number overall

test('Given an array with both positive and negative numbers, returns the largest number ', ()=>{
expect(findMax([-3,6,-9])).toBe(6)
}
)
// Given an array with just negative numbers
// When passed to the max function
// Then it should return the closest one to zero

test("Given an array with just negative numbers, returns the closest one to zero", ()=>{
expect(findMax([-3,-4,-1])).toBe(-1)
})
// Given an array with decimal numbers
// When passed to the max function
// Then it should return the largest decimal number
test('Given an array with decimal numbers, returns the largest decimal number', ()=>{
expect(findMax([3.5,4.5,6.5])).toBe(6.5)
})

// Given an array with non-number values
// When passed to the max function
// Then it should return the max and ignore non-numeric values
test("Given an array with non-number values, returns the max and ignore non-numeric values", () =>{
expect(findMax(["3",5,'undefined',null,6])).toBe(6)
})

// Given an array with only non-number values
// When passed to the max function
// Then it should return the least surprising value given how it behaves for all other inputs
test("Given an array with only non-number values, returns infinity ", ()=>{
expect(findMax(['Holla!', NaN, true])).toBe(-Infinity)
})
11 changes: 11 additions & 0 deletions Sprint-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
function sum(elements) {
//filter the array to be numerical values only
const numbersOnly = elements.filter((item)=> typeof item === 'number' && !Number.isNaN(item));

if(numbersOnly.length === 0){
return 0
}
let total = 0 ;
for( let i = 0 ; i < numbersOnly.length; i ++ ){
total += numbersOnly[i];
}
return total ;
}

module.exports = sum;
21 changes: 18 additions & 3 deletions Sprint-1/implement/sum.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,39 @@ 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, returns 0", ()=>{
expect(sum([])).toBe(0)
})

// Given an array with just one number
// When passed to the sum function
// Then it should return that number

test('Given an array with just one number, return that number', ()=>{
expect(sum([3])).toBe(3)
})
// Given an array containing negative numbers
// When passed to the sum function
// Then it should still return the correct total sum
test('Given an array with negative numbers, return the correct total', ()=>{
expect(sum([-2,-4,-1])).toBe(-7)
})

// Given an array with decimal/float numbers
// When passed to the sum function
// Then it should return the correct total sum
test('Given an array with decimal/float numbers, returns the correct total sum',()=>{
expect(sum([3.5,6.1,])).toBe(9.6)
} )

// Given an array containing non-number values
// When passed to the sum function
// Then it should ignore the non-numerical values and return the sum of the numerical elements

test('Given an array containing non number values, return the sum of the numerical elements and ignore the non-numerical values',()=>{
expect(sum(['4',"Holla",6,9,1])).toBe(16)
})
// Given an array with only non-number values
// When passed to the sum function
// Then it should return the least surprising value given how it behaves for all other inputs
test('Given an array with only non-number values, return -Infinity',()=>{
expect(sum([null,undefined,NaN,true])).toBe(0)
})
3 changes: 2 additions & 1 deletion Sprint-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"license": "ISC",
"devDependencies": {
"jest": "^29.7.0"
}
},
"type": "commonjs"
}
20 changes: 14 additions & 6 deletions Sprint-1/refactor/includes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// 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];
if (element === target) {
return true;
// function includes(list, target) {
// for (let index = 0; index < list.length; index++) {
// const element = list[index];
// if (element === target) {
// return true;
// }
// }
// return false;
// }
function includes(list, target){
for (element of list){
if(element === target){
return true
}
}
return false;
return false
}

module.exports = includes;
Loading