Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

Glasgow Class 6 - Diana Savchuk - JS-1 - Week 3 #263

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

// Example 1
let a;
console.log(a);
console.log(a); //value is not asigned


// Example 2
Expand All @@ -20,17 +20,17 @@ function sayHello() {
}

let hello = sayHello();
console.log(hello);
console.log(hello); //sayHello() function does not have a return statement

Copy link

Choose a reason for hiding this comment

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

Nice One


// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); //sayHelloToUser() function is not given a value when the function is called


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); //array contains only 3 elements and indices like 0,1,2 when we write arr[3] it means that we whant to reach the element number 4 which does not exist here.
4 changes: 2 additions & 2 deletions 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Declare some variables assigned to arrays of values
*/

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
let numbers = [1,2,3,4,5,6,7,8,9,10]; // add numbers from 1 to 10 into this array
let mentors = ['Daniel', 'Irina', 'Rares']; // Create an array with the names of the mentors: Daniel, Irina and Rares

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

function first(arr) {
return; // complete this statement
return arr[0]; // complete this statement
}

function last(arr) {
return; // complete this statement
return arr[arr.length - 1 ]; // complete this statement
}

/*
Expand Down
3 changes: 2 additions & 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,8 @@
*/

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

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

// TODO - Write for loop code here

for(let i = 0; i < WRITERS.length; i++){
console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}
/*
The output should look something like this:

Expand Down
8 changes: 8 additions & 0 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
while (i < BIRTHDAYS.length){
if(BIRTHDAYS[i].includes("July")){
return BIRTHDAYS[i];
}
i++;
}

}

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

function getTemperatureReport(cities) {
// TODO
let forecast = [];
for(let i = 0; i < cities.length; i++){
const currentCity = cities[i]
const temperature = temperatureService(cities[i]);
const report = `The temperature in ${currentCity} is ${temperature} degrees`;
forecast.push(report);
}
return forecast;
}


Expand Down
35 changes: 31 additions & 4 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
const titles = [];
for(let i = 0; i < allArticleTitles.length; i++){
if(allArticleTitles[i].length <= 65){
titles.push(allArticleTitles[i])
}
}
return titles;
}

/*
Expand All @@ -14,7 +20,15 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestSoFar= allArticleTitles[0];

for( let i = 0; i < allArticleTitles.length; i++){
const currentTitle = allArticleTitles[i];
if(currentTitle.split(' ').length < fewestSoFar.split(' ').length){
fewestSoFar = currentTitle;
}
}
return fewestSoFar;
}

/*
Expand All @@ -23,15 +37,28 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
const articleWithNumbers = [];
for(let i = 0; i < allArticleTitles.length; i++){
for(let j = 0; j < allArticleTitles[i].length; j++){
if(Number(allArticleTitles[i][j])){
articleWithNumbers.push(allArticleTitles[i]);
break;
}
}
}
return articleWithNumbers;
}

/*
The Financial Times wants to understand what the average number of characters in an article title is.
Implement the function below to return this number - rounded to the nearest integer.
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let totalNumbers = 0;
for (let i = 0; i < allArticleTitles.length; i++){
totalNumbers += allArticleTitles[i].length;
}
return Math.round(totalNumbers/allArticleTitles.length);
}


Expand Down
61 changes: 55 additions & 6 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/

function getAveragePrices(closingPricesForAllStocks) {
// TODO
const averagePrice = closingPricesForAllStocks.map(calculateAverage);
return averagePrice;
}

function calculateAverage(companyStocks){ //callback function
let sum = 0;
for (let j = 0; j < companyStocks.length; j++) {
sum += companyStocks[j];
}
const average = sum / companyStocks.length;
return Number(average.toFixed(2));
}
/*
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 @@ -46,11 +56,32 @@ function getAveragePrices(closingPricesForAllStocks) {
In this example it would be:
(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) {
// const priceChanges = [];

// for (let i = 0; i < closingPricesForAllStocks.length; i++) {

// const prices = closingPricesForAllStocks[i];
// const priceChange = prices[prices.length - 1] - prices[0];
// priceChanges.push(Number(priceChange.toFixed(2)));
// }

// return priceChanges;
// }

function getPriceChanges(closingPricesForAllStocks) {
// TODO
const priceChanges = closingPricesForAllStocks.map(calculatePriceChange);
return priceChanges;
}

function calculatePriceChange(companyStocks){
for (let i = 0; i < companyStocks.length; i++) {
const priceChange = companyStocks[companyStocks.length - 1] - companyStocks[0];
return Number(priceChange.toFixed(2));
}
}


/*
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
Expand All @@ -64,7 +95,25 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const descriptions = [];

for (let i = 0; i < stocks.length; i++) {
const stockName = stocks[i].toUpperCase();
let highestPrice = 0;

for (let j = 0; j < 5; j++) {
const price = closingPricesForAllStocks[i][closingPricesForAllStocks[i].length - 1 - j];

if (price > highestPrice) {
highestPrice = price;
}
}

const description = `The highest price of ${stockName} in the last 5 days was ${highestPrice}`;
descriptions.push(description);
}

return descriptions;
}


Expand All @@ -88,7 +137,7 @@ test("should return a description of the highest price for each stock", () => {
"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"
"The highest price of TSLA in the last 5 days was 1101.3"
]
);
});