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
148 changes: 148 additions & 0 deletions Challenge09/Challenge09.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"use strict";

// Important Note:
// Kindly use forEach loop instead of for in all of your solutions

// Resource:
// forEach: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

// 1) ---------------------
//
// using the (arrInc) function; increase the provided array values by 10
//
// EX:
// [20, 54, 89, 41] ==> [30, 64, 99, 51]
//
// ------------------------

const arrInc = (arr) => {
let result = [];
for (let num of arr) {
result.push(parseInt(num) + 10);
}
return result;
};

// 2) ---------------------
//
// using the (roundDecimals) function; given an array of decimals round every value to the closest value
//
// EX:
// [5.4, 5.5 ,6.7, 6.8] ==> [5, 6, 7, 7]
//
// ------------------------
const roundDecimals = (arr) => {
let result = [];
for (let num of arr) {
result.push(Math.round(parseFloat(num)));
}
return result;
};

// 3) ---------------------
//
// An owner of a factory want to give bounce 100$ for the employee of production department who worked *More than* 8 hours
// and 50$ for those who worked less, given their data increase their salary and return the data back again
//
// EX:
// let data = [
// {
// name: "Robert",
// section: "Transport",
// workHours: 8,
// salaries: "3000$"
// },
// {
// name: "Richard",
// section: "HR",
// workHours: 6,
// salaries: "4000$"
// },
// {
// name: "Christopher",
// section: "Production",
// workHours: 10
// salaries: "4500$"
// },
// {
// name: "Andrew",
// section: "HouseKeeping",
// workHours: 9,
// salaries: "3200$"
// }
// ]
//
// ==>
//
// [
// {
// name: "Robert",
// section: "Transport",
// workHours: 8,
// salaries: "3050$"
// },
// {
// name: "Richard",
// section: "HR",
// workHours: 6,
// salaries: "4050$"
// },
// {
// name: "Christopher",
// section: "Production",
// workHours: 10
// salaries: "4600$"
// },
// {
// name: "Andrew",
// section: "HouseKeeping",
// workHours: 9,
// salaries: "3300$"
// }
// ]
//

// ------------------------
const employeesBonus = (arr) => {
let result = arr;
for (let personObj of result) {
if (personObj.workHours > 8) {
personObj.salaries = `${(
parseInt(personObj.salaries.slice(0, -1)) + 100
).toString()}$`;
} else {
personObj.salaries = `${(
parseInt(personObj.salaries.slice(0, -1)) + 50
).toString()}$`;
}
}
return result;
};

// 4) ---------------------
//
// Harry wants to buy a new mouse and keyboard for his new setup
// He wants to choose one mouse and one keyboard from the list of prices and take the most expensive combination
// but his budget is limited, help him by finding the most expensive *price* for a combination to buy with his budget
//
// EX:
//
// budget = 200$
// mouseArray = [35, 15, 75, 180, 150, 50]
// keyBoardArray = [5, 150, 35, 120, 75, 50, 100]
//
// ==> 200
// ------------------------
const mostExpensive = (budget, mouseArray, keyBoardArray) => {
let allPossibleComb = [];
for (let mNum of mouseArray) {
for (let kNum of keyBoardArray) {
if (mNum + kNum <= budget) {
allPossibleComb.push(mNum + kNum);
}
}
}
return Math.max(...allPossibleComb);
};

module.exports = { arrInc, roundDecimals, employeesBonus, mostExpensive };
126 changes: 126 additions & 0 deletions Challenge09/Challenge09.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"use strict";

const {
arrInc,
roundDecimals,
employeesBonus,
mostExpensive,
} = require("./Challenge09");

const data = [
{
name: "Robert",
section: "Transport",
workHours: 8,
salaries: "3000$",
},
{
name: "Richard",
section: "HR",
workHours: 6,
salaries: "4000$",
},
{
name: "Christopher",
section: "Production",
workHours: 10,
salaries: "4500$",
},
{
name: "Andrew",
section: "HouseKeeping",
workHours: 9,
salaries: "3200$",
},
];

const res = [
{
name: "Robert",
section: "Transport",
workHours: 8,
salaries: "3050$",
},
{
name: "Richard",
section: "HR",
workHours: 6,
salaries: "4050$",
},
{
name: "Christopher",
section: "Production",
workHours: 10,
salaries: "4600$",
},
{
name: "Andrew",
section: "HouseKeeping",
workHours: 9,
salaries: "3300$",
},
];

const data2 = [
{
name: "Jack",
section: "Coding",
workHours: 6,
salaries: "9000$",
},
{
name: "Andrew",
section: "Marketing",
workHours: 9,
salaries: "5000$",
},
];

const res2 = [
{
name: "Jack",
section: "Coding",
workHours: 6,
salaries: "9050$",
},
{
name: "Andrew",
section: "Marketing",
workHours: 9,
salaries: "5100$",
},
];

describe("Challenge09", () => {
test("Array-Increment", () => {
expect(arrInc([5, 15, 3, 10, 32])).toStrictEqual([15, 25, 13, 20, 42]);
expect(arrInc([6, 0, -10, 33])).toStrictEqual([16, 10, 0, 43]);
});
});

describe("Challenge09", () => {
test("Round-Decimals", () => {
expect(roundDecimals([5.4, 5.5, 6.7, 6.8])).toStrictEqual([5, 6, 7, 7]);
expect(roundDecimals([6.1, 2.3, 5, 2.9])).toStrictEqual([6, 2, 5, 3]);
});
});

describe("Challenge09", () => {
test("Employees-Bonus", () => {
expect(employeesBonus(data)).toStrictEqual(res);
expect(employeesBonus(data2)).toStrictEqual(res2);
});
});

describe("Challenge10", () => {
test("Most-Expensive", () => {
expect(
mostExpensive(
200,
[35, 15, 75, 180, 150, 50],
[5, 150, 35, 120, 75, 50, 100]
)
).toStrictEqual(200);
expect(mostExpensive(150, [35, 15, 75], [5, 150, 100])).toStrictEqual(135);
});
});