From b3c917a9109913d46607ca121bddc2f5cecb1780 Mon Sep 17 00:00:00 2001 From: khiro963 Date: Tue, 3 Dec 2024 15:39:35 +0100 Subject: [PATCH] assigment w2 js and the solutions --- .../Week2/assignment/ex1-giveCompliment.js | 18 +++++++++++++-- 1-JavaScript/Week2/assignment/ex2-dogYears.js | 4 +++- .../Week2/assignment/ex3-tellFortune.js | 22 +++++++++++++++++-- .../Week2/assignment/ex4-shoppingCart.js | 11 +++++++++- .../Week2/assignment/ex5-shoppingCartPure.js | 7 +++++- .../Week2/assignment/ex6-totalCost.js | 20 ++++++++++++++++- .../Week2/assignment/ex7-mindPrivacy.js | 8 ++++++- 7 files changed, 81 insertions(+), 9 deletions(-) diff --git a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js index 93806cfa..82b5223a 100644 --- a/1-JavaScript/Week2/assignment/ex1-giveCompliment.js +++ b/1-JavaScript/Week2/assignment/ex1-giveCompliment.js @@ -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)); diff --git a/1-JavaScript/Week2/assignment/ex2-dogYears.js b/1-JavaScript/Week2/assignment/ex2-dogYears.js index c88d88dd..023cfa68 100644 --- a/1-JavaScript/Week2/assignment/ex2-dogYears.js +++ b/1-JavaScript/Week2/assignment/ex2-dogYears.js @@ -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() { diff --git a/1-JavaScript/Week2/assignment/ex3-tellFortune.js b/1-JavaScript/Week2/assignment/ex3-tellFortune.js index c80aae95..081313bc 100644 --- a/1-JavaScript/Week2/assignment/ex3-tellFortune.js +++ b/1-JavaScript/Week2/assignment/ex3-tellFortune.js @@ -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 ]; diff --git a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js index a3f15a2a..f9965046 100644 --- a/1-JavaScript/Week2/assignment/ex4-shoppingCart.js +++ b/1-JavaScript/Week2/assignment/ex4-shoppingCart.js @@ -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) diff --git a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js index fc5e02f2..b0337d16 100644 --- a/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js +++ b/1-JavaScript/Week2/assignment/ex5-shoppingCartPure.js @@ -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) diff --git a/1-JavaScript/Week2/assignment/ex6-totalCost.js b/1-JavaScript/Week2/assignment/ex6-totalCost.js index eba643bc..f1cec3eb 100644 --- a/1-JavaScript/Week2/assignment/ex6-totalCost.js +++ b/1-JavaScript/Week2/assignment/ex6-totalCost.js @@ -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() { diff --git a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js index ae686dea..304993ee 100644 --- a/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js +++ b/1-JavaScript/Week2/assignment/ex7-mindPrivacy.js @@ -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)