Skip to content

Commit

Permalink
refactor: use Array some for simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
mairveh committed Oct 18, 2023
1 parent f6f9baa commit f95de90
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
3 changes: 1 addition & 2 deletions challenges/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export function checkIngredients(menu, ingredient) {
if (menu === undefined) throw new Error('menu is required');
if (!ingredient) throw new Error('ingredient is required');
// Your code here!
const ingredientFound = false
return menu.map(m => m.ingredients.includes(ingredient)).reduce((accumulator, currentValue) => accumulator || currentValue, ingredientFound)
return menu.some((item) => item.ingredients.includes(ingredient))
}

export function duplicateNumbers(arr1, arr2) {
Expand Down
8 changes: 8 additions & 0 deletions challenges/exercise6-optional.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
export const sumDigits = (n) => {
if (n === undefined) throw new Error('n is required');
return Array.from(n.toString()).reduce((accumulator, currentValue) => parseInt(accumulator)+parseInt(currentValue), 0)
};

/**
Expand All @@ -25,6 +26,11 @@ export const createRange = (start, end, step) => {
console.log(
"FYI: Optional step parameter not provided. Remove this check once you've handled the optional step!"
);
const arr = []
for(let i=start; i<=end; i+=step) {
arr.push(i)
}
return arr
};

/**
Expand Down Expand Up @@ -59,6 +65,8 @@ export const createRange = (start, end, step) => {
export const getScreentimeAlertList = (users, date) => {
if (users === undefined) throw new Error('users is required');
if (date === undefined) throw new Error('date is required');
const datedUsers = users.filter((u) => u.screentime)
console.log(datedUsers)
};

/**
Expand Down
62 changes: 62 additions & 0 deletions test/exercise6-optional.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
sumDigits,
createRange,
getScreentimeAlertList,
hexToRGB,
findWinner
} from '../challenges/exercise6-optional'

describe('sumDigits', () => {
test('returns the sum of all the number\'s digits', () => {
expect(sumDigits(375)).toEqual(15);
expect(sumDigits(100)).toEqual(1);
});
});

describe('createRange', () => {
test('creates a range of numbers as an array from start to end incremented by steps', () => {
expect(createRange(3,11,2)).toEqual([3, 5, 7, 9, 11]);
});
});

describe('getScreentimeAlertList', () => {
test('return an array of usernames of users who have used more than 100 minutes of screentime for a given date', () => {
expect(getScreentimeAlertList([
{
username: "beth_1234",
name: "Beth Smith",
screenTime: [
{ date: "2019-05-01", usage: { twitter: 34, instagram: 22, facebook: 40} },
{ date: "2019-05-02", usage: { twitter: 56, instagram: 40, facebook: 31} },
{ date: "2019-05-03", usage: { twitter: 12, instagram: 15, facebook: 19} },
{ date: "2019-05-04", usage: { twitter: 10, instagram: 56, facebook: 61} },
]
},
{
username: "sam_j_1989",
name: "Sam Jones",
screenTime: [
{ date: "2019-06-11", usage: { mapMyRun: 0, whatsApp: 0, facebook: 0, safari: 10} },
{ date: "2019-06-13", usage: { mapMyRun: 0, whatsApp: 0, facebook: 0, safari: 16} },
{ date: "2019-06-14", usage: { mapMyRun: 0, whatsApp: 0, facebook: 0, safari: 31} },
]
}
], '2019-05-04')).toEqual(["beth_1234"]);
});
});

describe('hexToRGB', () => {
test('transform the hex code into an RGB code in the format: rgb(255,17,51)', () => {
expect(hexToRGB('#FF1133')).toEqual('rgb(255,17,51)');
});
});

describe('findWinner', () => {
test('return "X" if player X has won, "0" if the player 0 has won, and null if there is currently no winner', () => {
expect(findWinner([
["X", "0", null],
["X", null, "0"],
["X", null, "0"]
])).toEqual('X');
});
});

0 comments on commit f95de90

Please sign in to comment.