Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Ebtesam Ahmed | Module-JS2 | Week 1 #238

Open
wants to merge 2 commits into
base: main
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
12 changes: 9 additions & 3 deletions week-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
// If you're in the week-1 directory, you can run npm test -- fix to run the tests in the fix directory

function calculateMedian(list) {
const middleIndex = Math.floor(list.length / 2);
const median = list.splice(middleIndex, 1)[0];
return median;
const sortedList = [...list].sort((a, b) => a - b);

const middleIndex = Math.floor(sortedList.length / 2);

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

module.exports = calculateMedian;
10 changes: 9 additions & 1 deletion week-1/implement/dedupe.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
function dedupe() {}
function dedupe(numbers) {

return [ ...new Set(numbers)]



}

console.log(dedupe([1,1,13]));
24 changes: 24 additions & 0 deletions week-1/implement/max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Fix this implementation
// Start by running the tests for this function
// If you're in the week-1 directory, you can run npm test -- fix to run the tests in the fix directory

function calculateMedian(list) {

const sortedList = [...list].sort((a, b) => a - b);


const middleIndex = Math.floor(sortedList.length / 2);


if (sortedList.length % 2 === 0) {

return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2;
} else {

return sortedList[middleIndex];
}
}

module.exports = calculateMedian;


12 changes: 12 additions & 0 deletions week-1/implement/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function sum (numbers){
let total = 0;
for ( let i = 0; i < numbers.length; i++ ){
if ( typeof numbers[i] === "number"){
total += numbers[i];
}
}

return total
}

console.log(sum([6,8,8,"w"]))
9 changes: 6 additions & 3 deletions week-1/refactor/find.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Refactor the implementation of find to use a for...of loop

function find(list, target) {
for (let index = 0; index < list.length; index++) {
const element = list[index];



for ( const element of list) {

if (element === target) {
return index;
return indexof(element);
}
}
return -1;
Expand Down