diff --git a/exercises/01-variables/exercise.js b/exercises/01-variables/exercise.js index e49dbda..7ef113c 100644 --- a/exercises/01-variables/exercise.js +++ b/exercises/01-variables/exercise.js @@ -11,7 +11,13 @@ */ function createPersonInfo() { // TODO: Create and return an object with the specified properties - + const person = { + name : "Alex Johnson", + age : 28, + city : "New York", + isEmployed : true + }; + return person; } /** @@ -23,7 +29,10 @@ function createPersonInfo() { */ function calculateAge(birthYear) { // TODO: Calculate age by subtracting birth year from 2024 - + let x = 2024; + let y = birthYear; + let number = x-y; + return number; } /** @@ -35,7 +44,8 @@ function calculateAge(birthYear) { */ function formatFullName(firstName, lastName) { // TODO: Combine first and last name with a space between them - + name = `${firstName} +" " + ${lastName};` + return name; } /** @@ -46,9 +56,15 @@ function formatFullName(firstName, lastName) { */ function checkAdult(age) { // TODO: Return true if age is 18 or greater, false otherwise - -} + let Adult = age>=18; + if (Adult){ + return true; + } + else{ + return false; + } +} /** * Task 5: Convert Temperature * Convert temperature from Celsius to Fahrenheit @@ -58,7 +74,9 @@ function checkAdult(age) { */ function convertTemperature(celsius) { // TODO: Convert Celsius to Fahrenheit using the formula - + let C = celsius; + let Fahrenheit = (C * 9/5) + 32 + return `the temperature in farenheit is ${Fahrenheit}F`; } /** @@ -70,7 +88,7 @@ function convertTemperature(celsius) { */ function createGreeting(name, timeOfDay) { // TODO: Create a greeting message using template literals or string concatenation - + return `Good ${timeOfDay}, ${name}`; } // DO NOT MODIFY: Export functions for testing diff --git a/exercises/02-functions/exercise.js b/exercises/02-functions/exercise.js index 04c5eb2..c4f179e 100644 --- a/exercises/02-functions/exercise.js +++ b/exercises/02-functions/exercise.js @@ -10,7 +10,7 @@ */ function add(a, b) { // TODO: Return the sum of a and b - + return a + b; } /** @@ -23,7 +23,7 @@ function add(a, b) { */ const multiply = (a, b) => { // TODO: Return the product of a and b - + return a * b; }; /** @@ -35,7 +35,7 @@ const multiply = (a, b) => { */ function greetUser(name = "Guest") { // TODO: Return a greeting message using the name parameter - + return `Hello, ${name}!`; } /** @@ -48,7 +48,7 @@ function greetUser(name = "Guest") { function calculateTotal(price, taxRate) { // TODO: Calculate and return the total price including tax // Formula: price + (price * taxRate) - + return price + (price * taxRate); } /** @@ -61,7 +61,12 @@ function calculateTotal(price, taxRate) { function createCounter() { // TODO: Create a counter variable and return a function that increments it // Hint: Use closure to maintain the counter state - + let counter = 0; + + return function increase(){ + counter++; + return counter; + }; } /** @@ -75,7 +80,7 @@ function createCounter() { function processNumbers(numbers, callback) { // TODO: Create a new array by applying the callback to each number // Hint: Use a loop or array method like map() - + return numbers.map(callback); } // DO NOT MODIFY: Export functions for testing diff --git a/exercises/05-arrays/exercise.js b/exercises/05-arrays/exercise.js index ff2eb19..9ea8638 100644 --- a/exercises/05-arrays/exercise.js +++ b/exercises/05-arrays/exercise.js @@ -8,7 +8,8 @@ */ function createNumberArray() { // TODO: Create and return an array with numbers 1 through 5 - + let numbers = [1, 2, 3, 4, 5]; + return numbers; } /** @@ -22,7 +23,7 @@ function createNumberArray() { function addToEnd(array, element) { // TODO: Create a new array with the element added to the end // Hint: Use spread operator [...array, element] or concat() - + let newNumbers = [...numbers, element]; } /** @@ -35,7 +36,7 @@ function addToEnd(array, element) { function removeFromStart(array) { // TODO: Return the first element of the array // Hint: Use array[0] or array indexing - + return numbers[0]; } /** @@ -47,7 +48,7 @@ function removeFromStart(array) { function findLargest(numbers) { // TODO: Find and return the largest number // Hint: Use Math.max() with spread operator or a loop - + return Math.max(...numbers); } /** @@ -59,7 +60,13 @@ function findLargest(numbers) { function filterEvenNumbers(numbers) { // TODO: Filter the array to only include even numbers // Hint: Use filter() method and modulo operator (%) - + const evenNumber = []; + for (const number of numbers) { + if (number % 2 == 0) { + evens.push(number); + } + } + return evenNumber; } /** @@ -71,7 +78,11 @@ function filterEvenNumbers(numbers) { function sumArray(numbers) { // TODO: Calculate the sum of all numbers in the array // Hint: Use reduce() method or a loop - + let sum = 0; + for (const number of numbers) { + sum += number; + } + return sum; } // DO NOT MODIFY: Export functions for testing diff --git a/exercises/calculation-function.js b/exercises/calculation-functions-exercise.js similarity index 89% rename from exercises/calculation-function.js rename to exercises/calculation-functions-exercise.js index 06c02d5..608e42a 100644 --- a/exercises/calculation-function.js +++ b/exercises/calculation-functions-exercise.js @@ -337,10 +337,15 @@ function calculateTotalIncome(month) { // 2. Check if transaction.type === 'income' // 3. Check if transaction is in the specified month // 4. Add transaction.amount to total - + for (const transaction of sampleTransactions) { + if (transaction.type === 'income' && isTransactionInMonth(transaction, month)) { + total += transaction.amount; + } + } return total; } + /** * TODO 2: Calculate total expenses for a specific month * @@ -362,7 +367,11 @@ function calculateTotalExpenses(month) { let total = 0; // Your code here - + for (const transaction of sampleTransactions){ + if (transaction.type === 'expense' && isTransactionInMonth(transaction, month)){ + total += transaction.amount; + } + } return total; } @@ -385,10 +394,13 @@ function calculateTotalExpenses(month) { function calculateNetBalance(month) { // TODO: Implement this function // Hint: This should be a simple calculation using the two functions above - - return 0; // Replace with your calculation + + const income = calculateTotalIncome(month); + const expenses = calculateTotalExpenses(month); + return income - expenses; } + /** * TODO 4: Calculate spending by category for a specific month * @@ -423,10 +435,20 @@ function calculateSpendingByCategory(month) { // 1. Loop through sampleTransactions // 2. Check if transaction is expense and in specified month // 3. Add amount to categoryTotals[transaction.category] - + + for (const transaction of sampleTransactions) { + if (transaction.type === 'expense' && isTransactionInMonth(transaction, month)) { + const category = transaction.category; + if (!categoryTotals[category]) { + categoryTotals[category] = 0; + } + categoryTotals[category] += transaction.amount; + } + } return categoryTotals; } + /** * TODO 5: Calculate average transaction amount by type * @@ -455,7 +477,14 @@ function calculateAverageTransaction(type, month) { // 2. Check if transaction matches type and month // 3. Add to total and increment count // 4. Return total / count (handle division by zero) - + + for (const transaction of sampleTransactions) { + if (transaction.type === type && isTransactionInMonth(transaction, month)) { + total += transaction.amount; + count++; + } + } + return count > 0 ? total / count : 0; } @@ -485,7 +514,15 @@ function findLargestExpense(month) { // 2. Check if transaction is expense and in specified month // 3. Compare amount with largestAmount // 4. Update largestTransaction and largestAmount if bigger - + + for (const transaction of sampleTransactions) { + if (transaction.type === 'expense' && isTransactionInMonth(transaction, month)) { + if (transaction.amount > largestAmount) { + largestAmount = transaction.amount; + largestTransaction = transaction; + } + } + } return largestTransaction; } @@ -515,8 +552,10 @@ function calculateSavingsRate(month) { // 1. Calculate savings (income - expenses) // 2. Calculate percentage (savings / income * 100) // 3. Handle case where income is 0 - - return 0; // Replace with your calculation + + if (income === 0) return 0; + const savings = income - expenses; + return parseFloat(((savings / income) * 100).toFixed(2)); } /** @@ -544,13 +583,19 @@ function calculateSavingsRate(month) { function getMonthSummary(month) { // TODO: Implement this function // Use the functions you've already implemented - + + const totalIncome = calculateTotalIncome(month); + const totalExpenses = calculateTotalExpenses(month); + const netBalance = totalIncome - totalExpenses; + const savingsRate = calculateSavingsRate(month); + const transactionCount = sampleTransactions.filter(t => isTransactionInMonth(t, month)).length; + return { - totalIncome: 0, - totalExpenses: 0, - netBalance: 0, - savingsRate: 0, - transactionCount: 0 + totalIncome, + totalExpenses, + netBalance, + savingsRate, + transactionCount }; } @@ -566,9 +611,13 @@ function getMonthSummary(month) { */ function findTransactionsAboveAmount(amount, month) { // TODO: Implement this bonus function - return []; + + return sampleTransactions.filter(transaction => + isTransactionInMonth(transaction, month) && transaction.amount > amount + ); } + /** * BONUS 2: Calculate month-over-month growth * @param {string} currentMonth - Current month in YYYY-MM format @@ -577,6 +626,8 @@ function findTransactionsAboveAmount(amount, month) { */ function calculateMonthOverMonthGrowth(currentMonth, previousMonth) { // TODO: Implement this bonus function + + return { incomeGrowth: 0, expenseGrowth: 0 diff --git a/exercises/calculation-functions-test.js b/exercises/calculation-functions-test.js index 2c5d8d1..dd90ea9 100644 --- a/exercises/calculation-functions-test.js +++ b/exercises/calculation-functions-test.js @@ -382,21 +382,21 @@ function testFindLargestExpense() { 'December 2024 (no data) should return null' ); - // Test March 2024 - should be education ($400) + // Test March 2024 - should be housing ($1200) const mar2024LargestExpense = { - id: '28', + id: '23', type: 'expense', - amount: 400, - category: 'education', - description: 'Online Course', - date: '2024-03-18' + amount: 1200, + category: 'housing', + description: 'Monthly Rent', + date: '2024-03-01' }; test( 'findLargestExpense-Mar2024', findLargestExpense('2024-03'), mar2024LargestExpense, - 'March 2024 largest expense should be education ($400)' + 'March 2024 largest expense should be housing ($1200)' ); }