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
32 changes: 25 additions & 7 deletions exercises/01-variables/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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
Expand All @@ -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`;
}

/**
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions exercises/02-functions/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
function add(a, b) {
// TODO: Return the sum of a and b

return a + b;
}

/**
Expand All @@ -23,7 +23,7 @@ function add(a, b) {
*/
const multiply = (a, b) => {
// TODO: Return the product of a and b

return a * b;
};

/**
Expand All @@ -35,7 +35,7 @@ const multiply = (a, b) => {
*/
function greetUser(name = "Guest") {
// TODO: Return a greeting message using the name parameter

return `Hello, ${name}!`;
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
};
}

/**
Expand All @@ -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
Expand Down
23 changes: 17 additions & 6 deletions exercises/05-arrays/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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];
}

/**
Expand All @@ -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];
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}

Expand All @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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
};
}

Expand All @@ -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
Expand All @@ -577,6 +626,8 @@ function findTransactionsAboveAmount(amount, month) {
*/
function calculateMonthOverMonthGrowth(currentMonth, previousMonth) {
// TODO: Implement this bonus function


return {
incomeGrowth: 0,
expenseGrowth: 0
Expand Down
14 changes: 7 additions & 7 deletions exercises/calculation-functions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
);
}

Expand Down