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
17 changes: 12 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@
For each example, can you explain why we are seeing undefined?
*/

// Example 1

//the variable did not contain any value. for this reason will be undefined
//Example 1
let a;
console.log(a);
console.log(a);


//this fuction did not have return, so when it's called will be undefined
// Example 2
function sayHello() {
let message = "Hello";
function sayHello(a) {
let message = "Hello2";
}

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


// this will return hello and then undefined because we did not declare of the variable user.
// Example 3
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
console.log(`Hello ${user}`);
}

sayHelloToUser();



//this array contain 3 items but the index start from 0 and will display until index[2]=3
// Example 4
let arr = [1,2,3];
console.log(arr[3]);

15 changes: 11 additions & 4 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@
*/

function evenNumbers(n) {
// TODO
let result = []
let i = 0;
while(i < n){
result.push(i * 2)
i++
}
return result.join() //with join() we can display the length of the chain in te same line
}

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
console.log(evenNumbers(3)); // should output 0,2,4
console.log(evenNumbers(0)); // should output nothing
console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18
// console.log(evenNumbers(3))
15 changes: 13 additions & 2 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ const BIRTHDAYS = [
"November 15th"
];

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

//this function will find the first word that start with July and will return it.
console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"

// String.prototype.startsWith()
// The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
15 changes: 13 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@
*/

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

let i = 0;
do {
sum += (i * 2);
i++;
} while(i < n);
return sum

}

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
console.log(evenNumbersSum(10)); // should output 90



20 changes: 15 additions & 5 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
console.log(String.fromCharCode(97 + i));
i++;
}
// let i = 0;
// while(i < 26) {
// console.log(String.fromCharCode(97 + i));
// i++;
// }
// The output shouldn't change.


for(let i = 0; i < 26; i++){
console.log(String.fromCharCode(97 + i))
// return String.fromCharCode(97 + i)
}

/*String.fromCharCode()
The String.fromCharCode() static method returns a string created from the specified sequence of UTF-16 code units.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode*/
15 changes: 14 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ const AGES = [

// TODO - Write for loop code here

/*
for(let i = 0; i < WRITERS.length && AGES.length; i++){
console.log(WRITERS[i] + " is " + AGES[i] + " years old")
}

// ==================other option with interpolation===============

// for(let i = 0; i < WRITERS.length; i++) {
// // Because we want to retrieve the element at the same index for both arrays, we can re-use the same variable i
// // We are also making use of string interpolation here - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
// console.log(`${WRITERS[i]} is ${AGES[i]} years old`);
// }
/*


The output should look something like this:

Virginia Woolf is 59 years old
Expand Down
23 changes: 22 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
A for-of loop is a easy and way of looping through the elements of an array, string or any other "iterable object" (think sequence of elements).
A for-of loop is a easy and way of looping through the elements of an array,
string or any other "iterable object" (think sequence of elements).
*/

// TODO Use a for-of loop to output each of the tube stations below.
Expand All @@ -11,6 +12,26 @@ let tubeStations = [
"Tottenham Court Road"
];

for(let station of tubeStations) {
console.log(station);
}


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

for(let letter of str) {
console.log(letter.toUpperCase());
}

let array = "a1, b2, c4, d5, g7, k8";

for(let letter of array) {
console.log(letter.toUpperCase());
}

let chain ="i am a string";

for(let letter of chain) {
console.log(letter.toUpperCase());
}
12 changes: 10 additions & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@
- Hint: you can call the temperatureService function from your function
*/

// function getTemperatureReport(cities) {
// // TODO
// }
function getTemperatureReport(cities) {
// TODO
let cityWeather = [];
for (let i = 0; i < cities.length; i++) {
cityWeather.push(
`The temperature in ${cities[i]} is ${temperatureService(cities[i])} degrees`
);
}
return cityWeather;
}


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

function temperatureService(city) {
Expand Down
15 changes: 12 additions & 3 deletions 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
/*
In the below example, we want to keep calling generateRandomNumber until we get a value that is > 50.
In the below example, we want to keep calling generateRandomNumber
until we get a value that is > 50.
Implement this using a do-while loop.
*/

// This function shouldn't be changed
function generateRandomNumber() {
console.log("Generating number...");
return Math.round(Math.random() * 100);
return Math.round(Math.random() * 100);
}


function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
// TODO - implement using a do-while loo
let randomNumber;
do {
randomNumber = generateRandomNumber();

} while (randomNumber < 50); //we will run this code, only if we received a value less than 50.
return randomNumber;

}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
90 changes: 88 additions & 2 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let shorTitles = [];
for (const title of allArticleTitles) {
if (title.length <= 65){
shorTitles.push(title);
}
}
return shorTitles;
}


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


// If you want to use the Math.min approach, first extract he lengths of each word and then pass those to the Math.max.

function titleWithFewestWords(allArticleTitles) {
// TODO

}




/*
The editor of the FT has realised 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.
Expand All @@ -34,7 +48,79 @@ function averageNumberOfCharacters(allArticleTitles) {
// TODO
}

/*
Imagine you are working on the Financial Times web site! They have a
list of article titles stored in an array.

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) {
// let arr =[];
// for (let article of allArticleTitles){
// if (article.length <= 65){
// arr.push(article)
// }
// }
// return arr;
// }

// /*
// 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 arr = [];
// for (let i = 0; i < allArticleTitles.length; i++) {
// arr.push(numberOfWords(allArticleTitles[i]));
// }
// return allArticleTitles[arr.indexOf(Math.min(...arr))];
// }

// function numberOfWords(title) {
// console.log(`title: ${title}`);
// let words = title.split(' ');
// console.log(`words: ${words}`);
// return words.length;
// }
// // new link in chat!!!
// /*let
// on The editor of the FT has realised that headlines which have numbers in them get more clicks!n
// Implement the function below to return a new array containing all the headlines which ctain a number.
// (Hint: remember that you can also loop through the characters of a string if you need to)
// */
// function headlinesWithNumbers(allArticleTitles) {
// // TODO
// let arr= [];
// for (let article of allArticleTitles){
// for (let char of article){
// if (char>="0" && char<="9"){
// arr.push (article);
// break;
// }

// }
// }
// return arr
// }

// /*
// 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 sum = 0;
// for (let article of allArticleTitles){
// sum += article.length;

// }

// return Math.round(sum / allArticleTitles.length)
// }
//

/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
Expand Down