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
9 changes: 7 additions & 2 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@

// Example 1
let a;
console.log(a);
console.log(a);
// a is declared as a variable. But no value has been assigned.


// Example 2
function sayHello() {
let message = "Hello";
}
// sayHello function created. But it has no parameter in it.

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

// hello variable has a value which is sayHello function. But this function has no parameter in it.

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

sayHelloToUser();
// user declared as a parameter. But when calling a function, an argument is needed not to get undefined result.


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// The index is starting from 0 in array. That`s why the last index of array "arr" is [2].
// So there is no value for arr[3]. So, arr[3] will be undefined.
13 changes: 12 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
*/

function evenNumbers(n) {
// TODO
let arr = [];
let i = 0;
while (i < n*2) {
if (i % 2 === 0) {
arr.push(i);
}
i++;
}
console.log(arr);

}

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


12 changes: 9 additions & 3 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/*
Loops can be useful when working with arrays.
In the below example, imagine we've defined an array holding the birthdays of your closest friends.
Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function.
In the below example, imagine we've defined an array holding the
birthdays of your closest friends.
Use a while loop to search through the array until you find the first birthday in July,
then return that birthday from the function.
*/

const BIRTHDAYS = [
Expand All @@ -17,7 +19,11 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
let counter = 0;
while(birthdays[counter].includes("July") != true) {
counter++;
}
return birthdays[counter];
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
22 changes: 17 additions & 5 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
/*
Sometimes when using loops, we'll want to execute the body of the loop at least once. We can make sure this happens by using a do-while loop.
- If the condition in a while loop is initially false, the body of the loop will never execute
- But in a do-while loop, because the condition is checked after the body, we know that it will always execute at least once
Sometimes when using loops, we'll want to execute the body of the loop at least once.
We can make sure this happens by using a do-while loop.
- If the condition in a while loop is initially false,
the body of the loop will never execute
- But in a do-while loop, because the condition is checked after the body,
we know that it will always execute at least once

Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
Using a do-while loop, write a function which returns
the sum of the first n even numbers (starting from 0)
*/

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

console.log(evenNumbersSum(3)); // should output 6
Expand Down
10 changes: 8 additions & 2 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/*
for loops can be useful when we already know exactly how many times we want to loop.
for loops can be useful when we already know exactly how many times
we want to loop.

Change the while loop below into a for loop.
*/


// Change the below code to use a for loop instead of a while loop.
let i = 0;
/*let i = 0;
while(i < 26) {
console.log(String.fromCharCode(97 + i));
i++;
}
*/
for (let index = 0; index < 26; index++) {
console.log(String.fromCharCode(97 + index));

}
// The output shouldn't change.
7 changes: 6 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
for loops can be useful when we already know exactly how many times we want to loop.
for loops can be useful when we already know exactly how many times
we want to loop.

Below we have 2 arrays which have exactly the same number of values.
- The first array is a list of writers
Expand Down Expand Up @@ -27,6 +28,10 @@ const AGES = [
];

// TODO - Write for loop code here
for (let index = 0; index < WRITERS.length; index++) {
console.log(`${WRITERS[index]} is ${AGES[index]} years old`);
}


/*
The output should look something like this:
Expand Down
11 changes: 9 additions & 2 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
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 @@ -10,7 +12,12 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];

for (const str of tubeStations) {
console.log(str);
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (const iterator of str) {
console.log(iterator.toUpperCase());
}
6 changes: 5 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/

function getTemperatureReport(cities) {
// TODO
let cityNameAndDegree = [];
for (const temperature of cities) {
cityNameAndDegree.push(`The temperature in ${temperature} is ${temperatureService(temperature)} degrees`);
}
return cityNameAndDegree;
}


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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A small thing, the exercise says only smaller than 50 not smaller than or equal so yeah

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 for your feedback Altom. I am fixing it.

return num;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
42 changes: 38 additions & 4 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 ) {
if (titleCharacter.length <= 65){
publishedTitle.push(titleCharacter);
}
}
return publishedTitle;
}

/*
Expand All @@ -14,7 +20,19 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let wordLength = 100;
let index = 0;

allArticleTitles.forEach((article, i) => {
let split = article.split(" ");

if (split.length < wordLength) {
wordLength = split.length;
index = i;
}
});

return allArticleTitles[index];
}

/*
Expand All @@ -23,15 +41,31 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let newHeadlinesWithNumbers = [];
for (let i = 0; i < allArticleTitles.length; i++) {
for (let j = 0; j < allArticleTitles[i].length; j++) {
if (allArticleTitles[i][j] === "$") {
newHeadlinesWithNumbers.push(allArticleTitles[i])
}
}
}
return newHeadlinesWithNumbers;
}

/*
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 newNumbers = 0;
for (let i = 0; i < allArticleTitles.length; i++) {
for (let j = 0; j < allArticleTitles[i].length; j++) {
newNumbers++;

}
}
return Math.round(newNumbers / allArticleTitles.length);

}


Expand Down