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
6 changes: 5 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/

function getTemperatureReport(cities) {
// TODO
// let report = []
// for (i in cities){
// report.push(`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`)
// }
return cities.map(x=>`The temperature in ${x} is ${temperatureService(x)} degrees`)
}


Expand Down
5 changes: 5 additions & 0 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ function generateRandomNumber() {

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let randNum = generateRandomNumber()
do {
randNum = generateRandomNumber() ;
} while (randNum <= 50);
return randNum
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
19 changes: 15 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
return allArticleTitles.filter(x=>x.length<=65)
}

/*
Expand All @@ -14,7 +14,16 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let myMin;
let myStr = ''
for (i in allArticleTitles){
myMin = myMin || allArticleTitles[i].length
if (allArticleTitles[i].length < myMin){
myMin = allArticleTitles[i].length
myStr = allArticleTitles[i]
}
}
return myStr
}

/*
Expand All @@ -23,15 +32,17 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
return allArticleTitles.filter(x=>x.match(/\d+/g))
}

/*
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 sumOfChar = allArticleTitles.map(x=>x.length)
return Math.round(allArticleTitles.reduce(function(a, b) { return a + b.length }, 0)/allArticleTitles.length)
}


Expand Down
32 changes: 24 additions & 8 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averArr = []
for ( let i of closingPricesForAllStocks){
averArr.push(Number((i.reduce((accumulator, value) => {return accumulator + value})/i.length).toFixed(2)))
}
return averArr
}

/*
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,10 +50,20 @@ 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 calculateChange (prices){
return (prices[prices.length-1]-prices[0]).toFixed(2)
}

function getPriceChanges(closingPricesForAllStocks) {
// let changArr = []
// for ( let i of closingPricesForAllStocks){
// let change = calculateChange(i)
// changArr.push(Number(change))
// }
// return changArr
return closingPricesForAllStocks.map(x=>Number(calculateChange(x)))
}

/*
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,10 +77,13 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let myarr = []
for (let i in closingPricesForAllStocks){
myarr.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`)
}
return myarr
}


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
expect(getAveragePrices(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS)).toEqual(
Expand Down
6 changes: 5 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/

function factorial(input) {
// TODO
let factor = 1
for (let i = 1; i<=input; i++ ){
factor *=i
}
return factor
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
26 changes: 25 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
let maxChil = 0;
let nameChil = ''
let myarr = []
let maxNon = 0;
let nameNon = ''
let maxCook = 0;
let nameCook = ''
for (const i in books){
if (books[i].rating > maxChil && books[i].genre=="children"){
maxChil = books[i].rating
nameChil = books[i].title
}

if (books[i].rating > maxNon && books[i].genre=="non-fiction"){
maxNon = books[i].rating
nameNon = books[i].title
}

if (books[i].rating > maxCook && books[i].genre=="cooking"){
maxCook = books[i].rating
nameCook = books[i].title
}

}
return [nameChil, nameNon, nameCook]
Comment thread
tiritaki marked this conversation as resolved.

@nuclear-pasta nuclear-pasta Dec 12, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you can rewrite this function without the need to declare the genre, and in a single loop.
what if you register an empty object before the loop, and every time there is a new genre, you register that genre as the key of the object, and as value, you register the rating (highest rating but at the first loop will be whatever rating). This will save you from new genre non considered in your code.

}


Expand Down
10 changes: 8 additions & 2 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
*/

function generateFibonacciSequence(n) {
// TODO
}
let fibSec = [0,1]
let summa = 0
for (let i = 0; i<n-2; i++){
summa=fibSec[i]+fibSec[i+1]
fibSec.push(summa)
}
return fibSec
}

/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
Expand Down