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: 4 additions & 2 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Example 1
let a;
console.log(a);

//Answer: a is not defined.

// Example 2
function sayHello() {
Expand All @@ -21,16 +21,18 @@ function sayHello() {

let hello = sayHello();
console.log(hello);

//Answer: No value returned by the function.

// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}
//Answer:The variables haven't been declared.

sayHelloToUser();


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//Answer:Zero index, meaning that we only have up to index 2. Index position 3 does not exist.
15 changes: 13 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
/*
while loops can be useful when you want to execute some code as long as some condition is true.

Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string.
Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-separated string.
The list of numbers should start with 0. n is being passed in as a parameter.
*/

function evenNumbers(n) {
// TODO
let array = []
let i = 0;
while (i < n) {

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 have nicely done a while loop!

array.push(i * 2);
i++
}
console.log(array.toString());
}



evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18



8 changes: 7 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let i = 0;
while (i < birthdays.length) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good!

if (birthdays[i].includes("July")){
return birthdays[i];
}
i++
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
8 changes: 7 additions & 1 deletion 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/

function evenNumbersSum(n) {
// TODO
let sum = 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please remember about correct code formatting. It is making your life easier when You are coding and reading code.

let i = 0; //starting from zero
do {
sum = i * (i + 1); //formula for the sum of consecutive even numbers: n(n+1) or JSshort: sum+= i * 2
i++;
} while (i < n);
return sum;
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
8 changes: 5 additions & 3 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
//let i = 0;
//while(i < 26) {
//console.log(String.fromCharCode(97 + i));
//i++;
for (let i = 0; i < 26; 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.

console.log(String.fromCharCode(97 + i));
i++;
}
// The output shouldn't change.
6 changes: 3 additions & 3 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ const AGES = [
63,
49
];

// TODO - Write for loop code here

for (let i = 0; i < WRITERS.length; 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.

Correct indentation :)

console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
}
/*
The output should look something like this:

Expand Down
9 changes: 8 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for (let tubeStation of tubeStations) {
console.log(tubeStation);
}


// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
// TODO Use a for-of loop to capitalize and output each letter in the string separately. //Separately = different line?
let str = "codeyourfuture";

for (let char of str) {
console.log(char.toUpperCase());
}
33 changes: 20 additions & 13 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 @@ -12,24 +12,31 @@
*/

function getTemperatureReport(cities) {
// TODO
}
let citiesTemp = [];
for (city of cities) {
let temp = temperatureService(city);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unused variable - next time, please remove it or use in string interpolation :)

citiesTemp.push(`The temperature in ${city} is ${temperatureService(city)} degrees`);
}
return citiesTemp;
}




/* ======= 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
8 changes: 7 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ function generateRandomNumber() {

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


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

Expand Down
17 changes: 12 additions & 5 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
}
let conformingTitles = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length < 65) {
conformingTitles.push(allArticleTitles[i]);
}
}
return conformingTitles;
}


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

}

/*
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)
*/
Expand Down