From 69bd1b5e3bf52f0956450bac5bb84efb39064a69 Mon Sep 17 00:00:00 2001 From: Joemwa <109852314+Joemwa@users.noreply.github.com> Date: Wed, 8 Mar 2023 19:19:37 +0000 Subject: [PATCH 1/2] Done two questions --- 2-mandatory/1-weather-report.js | 29 ++++++++++++++++++++++++++++- 2-mandatory/2-financial-times.js | 11 +++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/2-mandatory/1-weather-report.js b/2-mandatory/1-weather-report.js index dcc2bdb0..c35761d2 100644 --- a/2-mandatory/1-weather-report.js +++ b/2-mandatory/1-weather-report.js @@ -12,8 +12,35 @@ */ function getTemperatureReport(cities) { - // TODO + const temperatureStatements = []; + + cities.forEach(city => { + let temperature = 0; + switch (city) { + case 'Ndola': + temperature = 20; + break; + case 'Kitwe': + temperature = 30; + break; + case 'London': + temperature = 40; + break; + default: + temperature = 0; + break; + // Construct a statement about the temperature of the city + const temperatureStatement = `${city} is currently ${temperature} degrees Celsius.`; + + // Add the statement to the array + temperatureStatements.push(temperatureStatement); + }); + + return temperatureStatements; } + getTemperatureReport(kitwe) + // TODO + /* ======= TESTS - DO NOT MODIFY ===== */ diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2ce6fb73..2a2f7516 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -4,9 +4,11 @@ The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less. Implement the function below, which will return a new array containing only article titles which will fit. */ + function potentialHeadlines(allArticleTitles) { - // TODO -} + return allArticleTitles.filter(title => title.length <= 65); + //TODO + } /* The editor of the FT likes short headlines with only a few words! @@ -23,6 +25,11 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { + let result = []; + + for (let title of allArticleTitles) { + + } // TODO } From 6db1eb7d069a63df572d5e8fc1f9175f6cbdd86a Mon Sep 17 00:00:00 2001 From: Joemwa <109852314+Joemwa@users.noreply.github.com> Date: Wed, 8 Mar 2023 22:31:47 +0000 Subject: [PATCH 2/2] Made attempt but code not passing the tests --- 2-mandatory/2-financial-times.js | 43 +++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/2-mandatory/2-financial-times.js b/2-mandatory/2-financial-times.js index 2a2f7516..bc361b10 100644 --- a/2-mandatory/2-financial-times.js +++ b/2-mandatory/2-financial-times.js @@ -16,6 +16,23 @@ function potentialHeadlines(allArticleTitles) { (you can assume words will always be seperated by a space) */ function titleWithFewestWords(allArticleTitles) { + + let minWords = Infinity; + let minTitle = ""; + + for (let i = 0; i < allArticleTitles.length; i++) { + const titleWords = allArticleTitles[i].split(" "); + const numWords = titleWords.length; + if (numWords < minWords) { + minWords = numWords; + minTitle = allArticleTitles[i]; + } + } + + return minTitle; + + + // TODO } @@ -25,11 +42,18 @@ function titleWithFewestWords(allArticleTitles) { (Hint: remember that you can also loop through the characters of a string if you need to) */ function headlinesWithNumbers(allArticleTitles) { - let result = []; - + + const headlinesWithNums = []; + for (let title of allArticleTitles) { - + if (/\d/.test(title)) { + headlinesWithNums.push(title); + } } + + return headlinesWithNums; + + // TODO } @@ -38,6 +62,19 @@ function headlinesWithNumbers(allArticleTitles) { Implement the function below to return this number - rounded to the nearest integer. */ function averageNumberOfCharacters(allArticleTitles) { + + let totalChars = 0; + let numTitles = 0; + + for (let title of allArticleTitles) { + totalChars += title.length; + numTitles++; + } + + const avgChars = Math.round(totalChars / numTitles); + return avgChars; + + // TODO }