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
20 changes: 13 additions & 7 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@
*/

// Example 1
let a;
//let a; //. a does not have value
let a = "Hi"
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
}
return "Hello";
} ////. this function have to return to something.

let hello = sayHello();
console.log(hello);
let result = sayHello();
console.log(result); //// should assign a variable


// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); // hello is undefined and recalled with no parameter.
sayHello(Maria); // should recall a value of a variable


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

console.log(arr[3]);// the length of array is 2 because it starts from 0.
//By adding arr[3] = 4; console.log(arr[3]) === true

7 changes: 5 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,11 @@
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 Down
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];
}

function last(arr) {
return; // complete this statement
return arr[arr.length-1];
}

/*
Expand Down
7 changes: 7 additions & 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,13 @@
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration
numbers;
numbers.length;
//numbers[3] = 4; or
numbers[numbers.length] = 4;
numbers[0] = 1;

console.log(numbers.length);

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
7 changes: 7 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,13 @@ const AGES = [

// TODO - Write for loop code here



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


/*
The output should look something like this:

Expand Down
11 changes: 9 additions & 2 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ const BIRTHDAYS = [
"November 15th"
];

function findFirstJulyBDay(birthdays) {
// TODO
function findFirstJulyBDay(BIRTHDAYS) {
let i = 0;
while(i < BIRTHDAYS.length){
if(BIRTHDAYS[i].includes("July")){
return BIRTHDAYS[i];
}
i++;
}
}


console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
34 changes: 19 additions & 15 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.
We also already have a temperatureService function which will take a city as a parameter and return a temperature.

Implement the function below:
- take the array of cities as a parameter
Expand All @@ -11,25 +11,29 @@
- Hint: you can call the temperatureService function from your function
*/

function getTemperatureReport(cities) {
// TODO
}

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.

This looks good 👍
One small suggestion - keep an eye on indentation, as it will make it easier for other developers to read your code.

const statement = [];

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.

Another small point - is statement a good name for this variable? Maybe statements or weatherReports would be a better name as this is an array which contains many statements 😄

for(const city of cities){
const temperature = temperatureService(city)
statement.push(`The temperature in ${city} is ${temperature} degrees`)
}
return statement;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: Well done!

Consider: You could also iterate [i]
function getTemperatureReport(cities) {
const temperatureReport = [];
for(let i = 0; i < cities.length; i++) {
const temperature = temperatureService(cities[i]);
temperatureReport.push(The temperature in ${cities[i]} is ${temperature} degrees);
}
return temperatureReport;


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

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

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
temparatureMap.set('Barcelona', 17);
temparatureMap.set('Dubai', 27);
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);
let temperatureMap = new Map();

temperatureMap.set('London', 10);
temperatureMap.set('Paris', 12);
temperatureMap.set('Barcelona', 17);
temperatureMap.set('Dubai', 27);
temperatureMap.set('Mumbai', 29);
temperatureMap.set('São Paulo', 23);
temperatureMap.set('Lagos', 33);

return temparatureMap.get(city);
return temperatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
Expand Down
143 changes: 126 additions & 17 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,145 @@
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
}

/*
// function potentialHeadlines(allArticleTitles) {
// // Create an empty array to store the potential headlines
// const potentialHeadlines = [];

// // Loop through each article title in the input array
// for (let i = 0; i < allArticleTitles.length; i++) {
// const articleTitle = allArticleTitles[i];

// // Check if the title is 65 characters or less
// if (articleTitle.length <= 65) {
// // If it is, add it to the potential headlines array
// potentialHeadlines.push(articleTitle);
// }
// }

// // Return the array of potential headlines
// return potentialHeadlines;
// }


// Using filter function with loops

// function filterPotentialHeadlines(allArticleTitles){
// let potentialHeadlines = [];
// for(let title of allArticleTitles){
// if(title.length <= 65){
// potentialHeadlines.push(title);
// }
// }
// return potentialHeadlines;
// }

// const allArticleTitles = [ "Streaming wars drive media groups to spend more than $100bn on new content",
// "Amazon Prime Video India country head: streaming is driving a TV revolution",
// "Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
// "British companies look to muscle in on US retail investing boom",
// "Labor to take firm step towards oblivion on New Year's Day",
// "Audit profession unattractive to new recruits, says PwC boss",
// "Chinese social media users blast Elan Musk over near miss in space",
// "Companies raise over $12tn in 'blockbuster' year for global capital markets",
// "The three questions that dominate investment",
// "Brussels urges Chile's incoming president to endorse EU trade deal",]
// console.log(filterPotentialHeadlines(allArticleTitles));


// Using the filter function without loops


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 perfect, well done 😄

function isPotentialHeadline(articleTitle){
return articleTitle.length <= 65;
}

function filterPotentialHeadlines(articleTitles){
// Here filter takes the function as a parameter
return articleTitles.filter(isPotentialHeadline);
}

const articleTitles = [ "Streaming wars drive media groups to spend more than $100bn on new content",
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Labor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elan Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",]

console.log(filterPotentialHeadlines(articleTitles));





/*
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)
(you can assume words will always be separated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
function titleWithFewestWords(allArticleTitles) {

let fewestWords = allArticleTitles[0];

for (let i = 0; i < allArticleTitles.length; i++) {
if (fewestWords.length > allArticleTitles[i].length) {
fewestWords = allArticleTitles[i];
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: Same code solution as mine


return fewestWords;
}


/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
The editor of the FT has realized that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/

function headlinesWithNumbers(allArticleTitles) {
// TODO
}
const headlinesWithNumbers = [];

for (let i = 0; i < allArticleTitles.length; i++) {
const articleTitle = allArticleTitles[i];

for (let j = 0; j < articleTitle.length; j++) {
const character = articleTitle.charAt(j);

if (!isNaN(parseInt(character))) {
headlinesWithNumbers.push(articleTitle);
}
}
}

return headlinesWithNumbers;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: Interesting. I see you used .charAt and NaN. I have not seen any other use it.

My solution:
let newArr = [];
for (let title of allArticleTitles) {
if (/[0-9]/.test(title) === true) {
newArr.push(title);
}
}

return newArr;
}



/*
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) {

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 solution!

// TODO
}
let totalChars = 0;

for (let i = 0; i < allArticleTitles.length; i++) {
totalChars += allArticleTitles[i].length;
}

const averageChars = Math.round(totalChars / allArticleTitles.length);

return averageChars;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Comment: I see you use just 1 global variable (let). Mine has 2. Well done







/* ======= List of Articles - DO NOT MODIFY ===== */
Expand All @@ -42,9 +151,9 @@ const ARTICLE_TITLES = [
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Labor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elon Musk over near miss in space",
"Chinese social media users blast Elan Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
Expand All @@ -53,16 +162,16 @@ const ARTICLE_TITLES = [
/* ======= TESTS - DO NOT MODIFY ===== */

test("should only return potential headlines", () => {
expect(new Set(potentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([
expect(new Set(filterPotentialHeadlines(ARTICLE_TITLES))).toEqual(new Set([
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Labor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"The three questions that dominate investment"
]));
});

test("should return an empty array for empty input", () => {
expect(potentialHeadlines([])).toEqual([]);
expect(filterPotentialHeadlines([])).toEqual([]);
});

test("should return the title with the fewest words", () => {
Expand Down
Loading