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

// Example 1
let a;
let a; // The variable a is declared but not assign to any value. Trying to print it will bring undefined
console.log(a);


// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello";// The function sayHello() has no return so, console.log(hello)
// will attempt to print empty variable.
}

let hello = sayHello();
Expand All @@ -28,9 +29,9 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();

sayHelloToUser();//The function is called with no parameter, it will attempt to print unrefered variable which will be undefined
// e.g: sayHelloToUser("Barry");

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
let arr = [1,2,3];// The array has tree elements and 0 to 2 indexes.
console.log(arr[3]);// console.log(arr[3]) will attempt to print non existing element (4th element).
8 changes: 7 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
*/

function evenNumbers(n) {
// TODO
let i = 0; // TODO
let evenNum = 0;
while (i < n){
console.log(evenNum);
i +=1;
evenNum +=2;
}
}

evenNumbers(3); // should output 0,2,4
Expand Down
6 changes: 5 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,11 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let index = 0;
while (birthdays[index] !== "July 11th" ){ // TODO
index +=1;
}
return birthdays[index];
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
20 changes: 18 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,25 @@
*/

function evenNumbersSum(n) {
// TODO
let i = 0;
let evenNum = 0;
let sum = 0;
do {
evenNum +=2;
sum +=evenNum;
i += 1 ; // TODO
} while (i < n);
return sum - evenNum;
// let i = 0;
// let evenNum = 0;
// let sum = 0;
// while (i < n-1){
// evenNum +=2;
// sum +=evenNum;
// i += 1 ;
// }
// return sum;
}

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

// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
// while(i < 26)
for (i = 0; i < 26; i++){
console.log(String.fromCharCode(97 + i));
i++;
//i++;
}
// The output shouldn't change.
5 changes: 4 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
for loops can be useful when we already know exactly how many times we want to loop.

Expand Down Expand Up @@ -27,7 +28,9 @@ 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
8 changes: 6 additions & 2 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ let tubeStations = [
"Oxford Street",
"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 capitaliseStr of str){
console.log(capitaliseStr.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 @@ -12,9 +12,17 @@
*/

function getTemperatureReport(cities) {
// TODO
}
if (cities === []) { // TODO
return [];

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 don't even need the check for empty array because it will return empty anyways if its passed as empty array

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you @AltomHussain. yes you are right

}else {
for (let i = 0; i < cities.length; i++){
// citiesTemperatures[i] = temperatureService(cities[i]);
cities[i] = "The temperature in " + cities[i]+" is "+ temperatureService(cities[i])+" degrees";

}
}
return cities;
}

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

Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function generateRandomNumber() {
}

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

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

/*
Expand All @@ -14,28 +20,54 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}
let fewestWords = "";
for (nElememntInArray of allArticleTitles){
let titleLength = 0;
for (characterIntitle of nElememntInArray){
titleLength +=1;
}
if (nElememntInArray.length <= titleLength ) {
fewestWords = nElememntInArray;
}
}

return fewestWords;

}

/*
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.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let headlineOnNumber = [];
for (let i = 0; i < allArticleTitles.length; i++){// TODO
for (let loopOnElement of allArticleTitles[i]){
if ( loopOnElement === "$"){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is anthor simpler way of checking if a string contains number, using rejex check this out:

  let NewArr = [];
  for (let index = 0; index < allArticleTitles.length; index++) {
    if (/\d/.test(allArticleTitles[index]))
      NewArr.push(allArticleTitles[index]);
  }
  return NewArr;
}```


In your case, you are checking `$` sign but that might change I mean you might have a number without `$` so .....


Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yes you are right, I use the sign "$" because I ran out of options and need this code to compile. I know this is now what they asked.
I did not really understand this if (/\d/.test(allArticleTitles[index]))but I should have use the condition below.

        if (loopOnElement >="0"  or  loopOnElement <="9") then push

headlineOnNumber.push(allArticleTitles[i]);
}
}
}
return headlineOnNumber;
}

/*
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 i=0;
let numberOfCharacters = 0;
for (i=0; i<allArticleTitles.length; i++){// TODO
for (let characterCount of allArticleTitles[i]){
numberOfCharacters +=1;
}
}
return Math.round(numberOfCharacters/i)
}



/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
Expand Down