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
25 changes: 25 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"configurations": [

{
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"name": "Launch Extension",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm",
"request": "launch",
"type": "extensionHost"
},
{
"type": "node",
"name": "Run Current File",
"request": "launch",
"program": "${workspaceFolder}/2-mandatory/3-stocks.js",
"stopOnEntry": true

}
]
}
18 changes: 17 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
*/

function getTemperatureReport(cities) {

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.

Good job 👍

// TODO
let temparatureReport = [];
for (let item of cities) {
temparatureReport.push("The temperature in " + item + " is " + temperatureService(item) + " degrees");

// console.log(`The temperature in ${cities[i]} is ${temperatureService(cities[i])}`);
}
return temparatureReport;
}


Expand Down Expand Up @@ -46,6 +52,16 @@ test("should return a temperature report for the user's cities", () => {
]);
});

test("should return the same array length as input's length", () => {
let usersCities = [
"London",
"Paris",
"São Paulo"
]

expect(getTemperatureReport(usersCities).length).toEqual(3);
});

test("should return a temperature report for the user's cities (alternate input)", () => {
let usersCities = [
"Barcelona",
Expand Down
39 changes: 35 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) {

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 one 😄
As an extra exercise, could you try re-writing this with the filter array method?

// TODO
// let shortTitles = [];
// for (let item of allArticleTitles){
// if (item.length <= 65) {
// shortTitles.push(item);
// }
// }
return allArticleTitles.filter(article => article.length <=65);
}

/*
Expand All @@ -14,7 +20,17 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let shortestHeadline;
let fewestNumber;
for (let item of allArticleTitles) {
let numberOfWords = item.split(' ').length;

if (fewestNumber === undefined || numberOfWords < fewestNumber) {
shortestHeadline = item;
fewestNumber = numberOfWords;
}
}
return shortestHeadline;
}

/*
Expand All @@ -23,15 +39,30 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
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.

Very nice and simple solution.

// TODO
let linesWithNumbers = [];
for (let item of allArticleTitles) {
if (/\d/.test(item)) {
linesWithNumbers.push(item);
}
}
return linesWithNumbers;
}

/*
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
// number of characters in each article
// number of characters/number of articles (allArticleTitles.length)

let charactersAmount = 0;
for (let item of allArticleTitles) {
let charSum = item.length;
charactersAmount +=charSum;
}
let average = charactersAmount / allArticleTitles.length;
return Math.round(average);
}


Expand Down
40 changes: 37 additions & 3 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {

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.

This looks good to me.
One small point: try to keep an eye on indentation, as it will improve readability for other developers.

// TODO
let averagePrices = [];
for (let item of closingPricesForAllStocks) {
let sum=0;
let average = 0;
for (let i of item) {
sum +=i;
}
average = sum/item.length;
averagePrices.push(Number(average.toFixed(2)));
}

return averagePrices;
}

/*
Expand All @@ -48,7 +59,13 @@ 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) {

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.

This looks good as well.
One small comment: Maybe item is not the best variable name here, something like prices will give the reader of the code a bit more information.

// TODO
let priceChanges = [];
for (let item of closingPricesForAllStocks) {
let changed = item[item.length-1] - item[0];
priceChanges.push(Number(changed.toFixed(2)));
}

return priceChanges;
}

/*
Expand All @@ -64,7 +81,24 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {

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.

Good work on this one 👍
In case you're interested, JavaScript also gives us something you might find useful here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

// TODO

let priceDescrip = [];
let biggestPrice = 0;
let biggestPriceArray = [];
for (let item of closingPricesForAllStocks) {
let biggestPrice = 0;
for (let i of item) {
if (i > biggestPrice) {
biggestPrice = i;
}
}
biggestPriceArray.push(biggestPrice.toFixed(2));
}
for (let i=0; i<stocks.length; i++)
{
priceDescrip.push("The highest price of " + stocks[i].toUpperCase() + " in the last 5 days was " + biggestPriceArray[i]);
}
return priceDescrip;
}


Expand Down
19 changes: 19 additions & 0 deletions 3-extra/1-radio-stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
*/

// `getAllFrequencies` goes here
function getAllFrequencies() {
const allFrequencies = [];
let i = 87;
while (i <=108) {
allFrequencies.push(i);
i++;
}
return allFrequencies
}

/**
* Next, let's write a function that gives us only the frequencies that are radio stations.
Expand All @@ -25,6 +34,16 @@
* - Return only the frequencies that are radio stations.
*/
// `getStations` goes here
function getStations () {

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 👍
For practice, can you re-write this using the filter array method?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done :)

let allFrequencies = getAllFrequencies();
// const stations = [];
// for (let item of allFrequencies){
// if (isRadioStation(item)) {
// stations.push(item);
// }
// }
return allFrequencies.filter(isRadioStation);
}

/*
* ======= TESTS - DO NOT MODIFY =======
Expand Down
20 changes: 18 additions & 2 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
}
// this will be an object where the property will be a genre, and the value will be an object representing the book
let highestRated = {};

for(let book of books) {
// if this is the first time we're seeing this genre OR the rating we've seen is not as high as the current book
if(highestRated[book.genre] === undefined || highestRated[book.genre].rating < book.rating) {
// then this book is now the highest rated in the genre so far
highestRated[book.genre] = book;
}
}

// Here we just want to get the highest rated books (the values of the object)
// and then get the title for each one
return Object.values(highestRated)
.map(book => book.title);
}




/* ======= Book data - DO NOT MODIFY ===== */
const BOOKS = [
{
Expand Down
9 changes: 9 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@

function generateFibonacciSequence(n) {
// TODO
const numberArray = [0, 1];
let i = 2;
while (numberArray.length<n){
let newNumber = numberArray[i-1] + numberArray[i-2];
numberArray.push(newNumber);
i++;
}

return numberArray;
}

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