Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
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
12 changes: 9 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,34 @@
For each example, can you explain why we are seeing undefined?
*/

// Example 1
let a;
// Example 1
//we are declaring a variable "a" without initializing it. When we try to log the value of "a" to the console, we get undefined because we haven't assigned a value to it yet.
let a= 3
console.log(a);


// Example 2
//we have a function called "sayHello" which initializes a local variable called "message" and doesn't return anything. When we call "sayHello" and assign the result to the "hello" variable, the value of "hello" becomes undefined because the function doesn't return anything.
function sayHello() {
let message = "Hello";
return message
}

let hello = sayHello();
console.log(hello);


// Example 3
//we have a function called "sayHelloToUser" that expects a parameter called "user". When we call "sayHelloToUser" without passing any arguments, the "user" parameter is undefined, which causes the function to log "Hello undefined" to the console.
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser('baki');


// Example 4
// we have an array called "arr" with three elements. When we try to access the fourth element using the index 3, we get undefined because the array doesn't have a fourth element. In JavaScript, when you try to access an index that doesn't exist in an array, the value returned is undefined.
let arr = [1,2,3];
arr.push(4)
console.log(arr[3]);
6 changes: 5 additions & 1 deletion 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*/

let numbers = []; // add numbers from 1 to 10 into this array
let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares

for (let i = 1; i<=10 ; i++){
numbers.push(i);
}
let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares
mentors = ['Daniel', 'Irina', 'Rares']
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
8 changes: 6 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
*/

function first(arr) {
return; // complete this statement

return arr[0];
}

function last(arr) {
return; // complete this statement

return arr[arr.length - 1];

}


/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
2 changes: 1 addition & 1 deletion 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration

numbers.push(4)
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
9 changes: 7 additions & 2 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ const AGES = [

// TODO - Write for loop code here

/*
The output should look something like this:
for(let i ; i<WRITERS.length;i++){
console.log(`${WRITERS[i]} is ${AGES[i]} years old.`)
//This code uses a for loop to iterate over the WRITERS array.
//The loop variable i is used as an index to access the corresponding element in the AGES array.
//The template literal syntax is used to create a string that combines the writer's name and age, which is then logged to the console.
}
/*The output should look something like this:

Virginia Woolf is 59 years old
Zadie Smith is 40 years old
Expand Down
11 changes: 9 additions & 2 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ const BIRTHDAYS = [
"November 15th"
];

function findFirstJulyBDay(birthdays) {
// TODO
function findFirstJulyBDay() {
let i = 0;
while (i < BIRTHDAYS.length) {
if (BIRTHDAYS[i].startsWith("July")) {
return BIRTHDAYS[i];
}
i++; // Increment i to avoid infinite loop
}
return null; // It will return null until it finds July in the array
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
18 changes: 18 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution 👍

function getTemperatureReport(cities) {
// TODO
let temperatureReport = []; // First, we initialize an empty array temperatureReport to hold the statements about the temperature of each city.

// Next, we use a for loop to loop through each city in the cities array.
for (let i=0; i <cities.length; i++){
let city = cities[i];
//we get the temperature for the current city by calling the temperatureService() function with the city as the argument.
let temparature = temperatureService(city);
// we create a statement about the temperature of the current city by using string interpolation to combine the city name and temperature into a single string
let statement = `The temperature in ${city} is ${temparature} degrees`;
console.log(cities)
console.log(temparature)
console.log(statement)
// we add the statement to the temperatureReport array using the push() method.
temperatureReport.push(statement);

}

return temperatureReport
}


Expand Down
29 changes: 29 additions & 0 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks perfect!

function potentialHeadlines(allArticleTitles) {
// TODO
// to create a new array containing only article titles that have 65 characters or less.
return allArticleTitles.filter(title => title.length <= 65);

}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well done Baki

/*
Expand All @@ -15,6 +18,13 @@ function potentialHeadlines(allArticleTitles) {
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to solve this one!
The next step is to think about different arrays that might be passed into this function (in the allArticleTitles parameter). What will your code do if allArticleTitles only has one title in the array?

function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestWordsTitle = allArticleTitles[0];
for (let i = 1; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].split(" ").length < fewestWordsTitle.split(" ").length) {
fewestWordsTitle = allArticleTitles[i];
}
}
return fewestWordsTitle;
}

/*
Expand All @@ -24,6 +34,17 @@ function titleWithFewestWords(allArticleTitles) {
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this works!
If you get time, you can experiment with different ways of checking if there's a number in a string - and then have a think about whether one approach might be better than another.

function headlinesWithNumbers(allArticleTitles) {
// TODO
let headlinesWithNums = [];
//Return an array containing all the headlines which contain a number
for (let i = 0; i < allArticleTitles.length; i++) {
for (let j = 0; j < allArticleTitles[i].length; j++) {
if (!isNaN(parseInt(allArticleTitles[i][j]))) {
headlinesWithNums.push(allArticleTitles[i]);
break;
}
}
}
return headlinesWithNums;
}

/*
Expand All @@ -32,6 +53,14 @@ function headlinesWithNumbers(allArticleTitles) {
*/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

function averageNumberOfCharacters(allArticleTitles) {
// TODO
// return the average number of characters in an article title

let totalChars = 0;
for (let i = 0; i < allArticleTitles.length; i++) {
totalChars += allArticleTitles[i].length;
}
let avgChars = Math.round(totalChars / allArticleTitles.length);
return avgChars;
}


Expand Down
91 changes: 63 additions & 28 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.20, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.30, 2869.45], // GOOGL
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
[179.19, 180.33, 176.28, 175.64, 172.99], // AAPL
[340.69, 342.45, 334.69, 333.2, 327.29], // MSFT
[3384.44, 3393.39, 3421.37, 3420.74, 3408.34], // AMZN
[2951.88, 2958.13, 2938.33, 2928.3, 2869.45], // GOOGL
[1101.3, 1093.94, 1067.0, 1008.87, 938.53], // TSLA
];

/*
Expand All @@ -34,9 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
}
let output = [];

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
const priceList = closingPricesForAllStocks[i];
const totalPrice = priceList.reduce(function (a, b) {
return a + b;
}, 0);
const average = totalPrice / priceList.length;
const averageFixed = average.toFixed(2);

output.push(parseFloat(averageFixed));
}

return output;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Baki
I sew you done nice work here with big effort and I want add here you can use for loop in two time instead to use two function

check here

function getAveragePrices(closingPricesForAllStocks) {
    let result = [];
    for (let i = 0; i < closingPricesForAllStocks.length; i++) {
    let total = 0;
    for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
        total += closingPricesForAllStocks[i][j];
    }
    result.push(parseFloat((total / 5).toFixed(2)));
    }
    return result;
}

this is my review for this week
Thanks Baki

/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
Expand All @@ -47,11 +59,22 @@ function getAveragePrices(closingPricesForAllStocks) {
(Apple's price on the 5th day) - (Apple's price on the 1st day) = 172.99 - 179.19 = -6.2
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
function getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS) {
let changePrice = [];
for (let i = 0; i < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS.length; i++) {
let priceList = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i];
const diffirent = priceList.reduce(function (a, b) {
a = priceList[0];
b = priceList[priceList.length - 1];
return b - a;
}, 0);
changePrice.push(parseFloat(diffirent.toFixed(2)));
}

return changePrice;
}

/*
/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
- Takes 2 parameters:
Expand All @@ -63,32 +86,44 @@ function getPriceChanges(closingPricesForAllStocks) {
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
function highestPriceDescriptions(
CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS,
STOCKS
) {
const descriptions = [];
for (let i = 0; i < STOCKS.length; i++) {
const stockName = STOCKS[i];
const prices = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i];
const highestPrice = Math.max(...prices);
const formattedPrice = highestPrice.toFixed(2);
descriptions.push(
`The highest price of ${stockName.toUpperCase()} in the last 5 days was ${formattedPrice}`
);
}
return descriptions;
}


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[176.89, 335.66, 3405.66, 2929.22, 1041.93]
);
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
176.89, 335.66, 3405.66, 2929.22, 1041.93,
]);
});

test("should return the price change for each stock", () => {
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
[-6.2, -13.4, 23.9, -82.43, -162.77]
);
expect(getPriceChanges(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual([
-6.2, -13.4, 23.9, -82.43, -162.77,
]);
});

test("should return a description of the highest price for each stock", () => {
expect(highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)).toEqual(
[
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30"
]
);
expect(
highestPriceDescriptions(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS, STOCKS)
).toEqual([
"The highest price of AAPL in the last 5 days was 180.33",
"The highest price of MSFT in the last 5 days was 342.45",
"The highest price of AMZN in the last 5 days was 3421.37",
"The highest price of GOOGL in the last 5 days was 2958.13",
"The highest price of TSLA in the last 5 days was 1101.30",
]);
});
15 changes: 13 additions & 2 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@

function generateFibonacciSequence(n) {
// TODO

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job on this one!

const sequence = [0, 1];

// Generate the remaining numbers in the sequence
for (let i = 2; i < n; i++) {
sequence.push(sequence[i - 1] + sequence[i - 2]);
}

// Return the generated sequence
return sequence;
}

/* ======= TESTS - DO NOT MODIFY ===== */

/* ======= TESTS - DO NOT MODIFY =====
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(10)).toEqual(
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Expand All @@ -34,4 +45,4 @@ test("should return the first 15 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(15)).toEqual(
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
);
});
});*/