Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

khiro-w2-JavaScript #114

Closed
wants to merge 1 commit into from
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
18 changes: 16 additions & 2 deletions 1-JavaScript/Week2/assignment/ex1-giveCompliment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,27 @@ Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-J
Use `console.log` each time to display the return value of the
`giveCompliment` function to the console.
-----------------------------------------------------------------------------*/
export function giveCompliment(/* TODO parameter(s) go here */) {
export function giveCompliment(theName) {
const compliments = [
'great',
'awesome',
'fantastic',
'Excellent',
'nice',
'wonderful',
'clever',
'fun',
'lovely',
];
const randomCompliment =
compliments[Math.floor(Math.random() * compliments.length)];
return `You are ${randomCompliment}, ${theName}`;
// TODO complete this function
}

function main() {
// TODO substitute your own name for "HackYourFuture"
const myName = 'HackYourFuture';
const myName = 'KHIRO';

console.log(giveCompliment(myName));
console.log(giveCompliment(myName));
Expand Down
4 changes: 3 additions & 1 deletion 1-JavaScript/Week2/assignment/ex2-dogYears.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ calculate it!
ages.
-----------------------------------------------------------------------------*/

export function calculateDogAge(/* TODO parameter(s) go here */) {
export function calculateDogAge(dogAge) {
// TODO complete this function
const dogYears = dogAge * 7;
return `your doggie is ${dogYears} old in dog years!`;
}

function main() {
Expand Down
22 changes: 20 additions & 2 deletions 1-JavaScript/Week2/assignment/ex3-tellFortune.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,46 @@ body, this code is now written once only in a separated function.

// This function should take an array as its parameter and return
// a randomly selected element as its return value.
function selectRandomly(/* TODO parameter(s) go here */) {
function selectRandomly(randomSelection) {
return randomSelection[Math.floor(Math.random() * randomSelection.length)];
// TODO complete this function
}

export function tellFortune(/* TODO add parameter(s) here */) {
export function tellFortune(numKids, partnerNames, locations, jobTitles) {
// TODO complete this function
return `You will be a ${selectRandomly(jobTitles)} in ${selectRandomly(locations)} married to ${selectRandomly(partnerNames)} with ${selectRandomly(numKids)} kids.`;
}

function main() {
const numKids = [
1, 2, 3, 4, 5,
// TODO add elements here
];

const partnerNames = [
'lila',
'serhan',
'murat',
'joe',
'mike',
// TODO add elements here
];

const locations = [
'Newyork',
'Istanbul',
'Damascus',
'Amsterdam',
'Harlem',
// TODO add elements here
];

const jobTitles = [
'Doctor',
'Teacher',
'Farmer',
'Pilot',
'Dancer',
// TODO add elements here
];

Expand Down
11 changes: 10 additions & 1 deletion 1-JavaScript/Week2/assignment/ex4-shoppingCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,17 @@ you have more than 3 items in your shopping cart the first item gets taken out.
const shoppingCart = ['bananas', 'milk'];

// ! Function to be tested
function addToShoppingCart(/* parameters go here */) {
function addToShoppingCart(newItem) {
// TODO complete this function

if (newItem === undefined) {
return `You bought ${shoppingCart.join(', ')}!`;
} else if (shoppingCart.length > 2) {
shoppingCart.shift();
}
shoppingCart.push(newItem);

return `You bought ${shoppingCart.join(', ')}!`;
}

// ! Test functions (plain vanilla JavaScript)
Expand Down
7 changes: 6 additions & 1 deletion 1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ it pure. Do the following:
5. Confirm that you function passes the provided unit tests.
------------------------------------------------------------------------------*/
// ! Function under test
function addToShoppingCart(/* TODO parameter(s) go here */) {
function addToShoppingCart(myShoppingCart, groceryItem) {
// TODO complete this function
if (myShoppingCart.length < 3) {
return [...myShoppingCart, groceryItem];
} else {
return [...myShoppingCart.slice(1), groceryItem];
}
}

// ! Test functions (plain vanilla JavaScript)
Expand Down
20 changes: 19 additions & 1 deletion 1-JavaScript/Week2/assignment/ex6-totalCost.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,39 @@ instead!
-----------------------------------------------------------------------------*/
const cartForParty = {
// TODO complete this object
beers: 1.2,
chips: 1.2,
water: 1.2,
cola: 1.2,
bread: 1.2,
};

function calculateTotalPrice(/* TODO parameter(s) go here */) {
function calculateTotalPrice(obj) {
// TODO replace this comment with your code
let totalAmount = 0;
for (let things in obj) {
totalAmount += obj[things];
}
return `Total:€${totalAmount}`;
}

// ! Test functions (plain vanilla JavaScript)
function test1() {
console.log('\nTest 1: calculateTotalPrice should take one parameter');
// TODO replace this comment with your code
console.log(calculateTotalPrice(cartForParty));
}

function test2() {
console.log('\nTest 2: return correct output when passed cartForParty');
// TODO replace this comment with your code
const expectedResult = 'Total:€6';
const calculatedResult = calculateTotalPrice(cartForParty);
if (calculatedResult === expectedResult) {
console.log('Passed the test');
} else {
console.log('Failed');
}
}

function test() {
Expand Down
8 changes: 7 additions & 1 deletion 1-JavaScript/Week2/assignment/ex7-mindPrivacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ const employeeRecords = [
];

// ! Function under test
function filterPrivateData(/* TODO parameter(s) go here */) {
function filterPrivateData(employeeRecords) {
// TODO complete this function
const newArray = [];
for (const puplicInfo of employeeRecords) {
const { name, occupation, email } = puplicInfo;
newArray.push({ name, occupation, email });
}
return newArray;
}

// ! Test functions (plain vanilla JavaScript)
Expand Down