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

London10_Olha-Danylevska_JavaScript-Core1_Coursework_week3 #284

Open
wants to merge 3 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
24 changes: 23 additions & 1 deletion 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@
let a;
console.log(a);

//because "a" did not assign to any value
// let a = 2;
// console.log(a)


// Example 2
function sayHello() {
let message = "Hello";
}

//I think because 'message' is declared but its value is never read.

// function sayHello() {
// let message = "Hello";
// return message
// }

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

Expand All @@ -28,9 +39,20 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}


sayHelloToUser();

// any returned value
// function sayHelloToUser(user) {
// let userName = user
// console.log(`Hello ${userName}`);
// return `Hello ${userName}`
// }



// Example 4
let arr = [1,2,3];
let arr = [1, 2, 3];
console.log(arr[3]);

//non-existing array element
8 changes: 6 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 All @@ -19,3 +19,7 @@ console.log(mentors);
[1,2,3,4,5,6,7,8,9,10]
['Daniel', 'Irina', 'Rares']
*/




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
1 change: 1 addition & 0 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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
6 changes: 6 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const AGES = [

// TODO - Write for loop code here

for (i = 0; i < WRITERS.length; i++) {
console.log(`${WRITERS[i]} is ${AGES[i]} years old`)
}



/*
The output should look something like this:

Expand Down
9 changes: 8 additions & 1 deletion 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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"
17 changes: 15 additions & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@

function getTemperatureReport(cities) {
// TODO
const reports = [];


for (item of cities) {
reports.push(`The temperature in ${item} is ${temperatureService(item)} degrees`)
}

return reports
}


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

function temperatureService(city) {
let temparatureMap = new Map();
let temparatureMap = new Map();

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
Expand All @@ -28,10 +36,15 @@ function temperatureService(city) {
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);

return temparatureMap.get(city);
}

test("test should return array the same length as an argument", () => {
let usersCities = ["London", "Paris", "São Paulo"];
expect(getTemperatureReport(usersCities).length).toEqual(3);
});

test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
Expand Down
42 changes: 39 additions & 3 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
let articlesWhichFit = []
for (item of allArticleTitles) {
if (item.length < 65) {
articlesWhichFit.push(item)
}
}
return articlesWhichFit
// TODO
}

Expand All @@ -14,7 +21,15 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let smallest = allArticleTitles[0]
for (i = 0; i < allArticleTitles.length; i++) {
if (smallest.length > allArticleTitles[i].length) {
smallest = allArticleTitles[i]
}
}

return smallest

}

/*
Expand All @@ -23,19 +38,40 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let numberArray = []
for (item of allArticleTitles) {
for (element of item) {
element = parseInt(element)
if (item.includes(element)) {
numberArray.push(item)
break
}
}
}
return numberArray;
}

/*
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
sumOfStrings = []
for (item of allArticleTitles) {
let itemLength = item.length
sumOfStrings.push(itemLength)
}
const initialValue = 0;
const sumWithInitial = sumOfStrings.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);
return Math.floor(Math.round(sumWithInitial / allArticleTitles.length))
}




/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down
47 changes: 42 additions & 5 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

/* ======= Stock data - DO NOT MODIFY ===== */
const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];
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
Expand All @@ -34,9 +34,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
arrayOfPrices = []
for (item of closingPricesForAllStocks) {
const initialValue = 0;
const sumWithInitial = item.reduce(
(accumulator, currentValue) => accumulator + currentValue,
initialValue
);
let averagePrice = sumWithInitial / item.length
let averagePriceDecimal = Math.round(averagePrice * 100) / 100
arrayOfPrices.push(averagePriceDecimal)
}
return arrayOfPrices
}


/*
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 @@ -48,9 +60,18 @@ function getAveragePrices(closingPricesForAllStocks) {
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let eachArrayOfDifference = []
for (element of closingPricesForAllStocks) {
for (i = 0; i < element.length; i++) {
let lastPrice = element.slice(-1)
let difference = lastPrice - element[i]
let differenceWithDecimal = Math.round(difference * 100) / 100
eachArrayOfDifference.push(differenceWithDecimal)
break
}
}
return eachArrayOfDifference
}

/*
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 +85,23 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arrayOfAnswers = []
let arrayOfStrings = []
for (item of closingPricesForAllStocks) {
for (i = 0; i < item.length; i++)
if (item[0] < item[i]) {
item[0] = item[i]
}
let answer = item[0]
answer = answer.toFixed(2)
console.log("ANSWER", answer)
arrayOfAnswers.push(answer)
}
for (i = 0; i < arrayOfAnswers.length; i++) {
arrayOfStrings.push(`The highest price of ${stocks[i]} in the last 5 days was ${arrayOfAnswers[i]}`)
}

return arrayOfStrings
}


Expand Down
29 changes: 29 additions & 0 deletions 3-extra/1-radio-stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,26 @@
* - Should return this array to use in other functions
*/


// `getAllFrequencies` goes here

function getAllFrequencies() {
let startFrequency = 87
let arrayOfFrequencies = []
while (startFrequency <= 108) {
arrayOfFrequencies.push(startFrequency)
startFrequency = startFrequency + 1
}
return arrayOfFrequencies
}



/**
* Next, let's write a function that gives us only the frequencies that are radio stations.
* Call this function `getStations`.
*

*
* This function should:
* - Get the available frequencies from `getAllFrequencies`
Expand All @@ -26,6 +41,17 @@
*/
// `getStations` goes here


function getStations(allFrequencies) {
let arrayOfRadioStations = []
allFrequencies = getAllFrequencies()
for (i = 0; i < allFrequencies.length; i++) {
if (isRadioStation(allFrequencies[i])) {
arrayOfRadioStations.push(allFrequencies[i])
}
}
return arrayOfRadioStations
}
/*
* ======= TESTS - DO NOT MODIFY =======
* Note: You are not expected to understand everything below this comment!
Expand Down Expand Up @@ -55,6 +81,9 @@ function isRadioStation(frequency) {
return getAvailableStations().includes(frequency);
}




test("getAllFrequencies() returns all frequencies between 87 and 108", () => {
expect(getAllFrequencies()).toEqual([
87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
Expand Down
Loading