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

Glasgow_6-Joseph_Mwanza-JavaScript-Core-1-CourseWork-Week3 #256

Open
wants to merge 2 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
29 changes: 28 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;

Copy link

Choose a reason for hiding this comment

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

This is nice.

// Add the statement to the array
temperatureStatements.push(temperatureStatement);
});

return temperatureStatements;
}
getTemperatureReport(kitwe)
// TODO



/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
48 changes: 46 additions & 2 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@
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!
Implement the function below, which returns the title with the fewest words.
(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
}

Expand All @@ -23,6 +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) {

const headlinesWithNums = [];

for (let title of allArticleTitles) {
if (/\d/.test(title)) {
headlinesWithNums.push(title);
}
}

return headlinesWithNums;


// TODO
}

Expand All @@ -31,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++;

Choose a reason for hiding this comment

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

clean code! i like it

}

const avgChars = Math.round(totalChars / numTitles);
return avgChars;


// TODO
}

Expand Down