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

// Example 1
let a;
console.log(a);
console.log(a);
// We declare the variable 'a' but we haven't assigned a value to it yet.


// Example 2
Expand All @@ -22,6 +23,8 @@ function sayHello() {
let hello = sayHello();
console.log(hello);

// We are not returning anything from the function body and so the default return is undefined.


// Example 3
function sayHelloToUser(user) {
Expand All @@ -30,7 +33,11 @@ function sayHelloToUser(user) {

sayHelloToUser();

// We are not providing any arguments for the function call and so the user is undefined.


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

// arr[3] is the 4th item of the array which has not been assigned a value and so it's undefined.
10 changes: 9 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,17 @@
*/

function evenNumbers(n) {
// TODO
let arr = [];
let i = 0;
while (i%2 === 0 && n > 0 && n > arr.length) {
arr.push(i);
i+=2;
}
return arr.toString();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

hey, Saf in line 11 when you give the condition while loop you can make it simply (n > arr.length) and it will work!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you need to check if i is even here?

console.log(evenNumbers(3));

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
9 changes: 8 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,14 @@ const BIRTHDAYS = [
];

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

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

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

function evenNumbersSum(n) {
// TODO
let i = 0;
let sumTotal = 0;
do {
i++;
if (i % 2 == 0) {
sumTotal += i * 3;
}
} while (i < n);

return sumTotal;
}

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you print out each number as its being added here you're not actually adding the even numbers, you're adding the product of the even numbers of three. Have a double-check of the logic you're using here.

Expand Down
14 changes: 9 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,13 @@


// 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));
}
4 changes: 4 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const AGES = [

// TODO - Write for loop code here

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

/*
The output should look something like this:

Expand Down
10 changes: 9 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ let tubeStations = [
"Tottenham Court Road"
];

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

// 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.
let str = "codeyourfuture";

for (let letterCapitalize of str) {
console.log(letterCapitalize.toUpperCase());
}
10 changes: 7 additions & 3 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,10 +12,14 @@
*/

function getTemperatureReport(cities) {
// TODO
let newArray = [];
for (let i = 0; i < cities.length; i++){
newArray.push("The temperature in " + cities[i] + " is " + temperatureService(cities[i]) + " degrees");

}
return newArray;
}


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

function 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.

Try and get into the habit of using a more descriptive name for your variable than 'newArray'

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 @@ -10,9 +10,15 @@ function generateRandomNumber() {
}

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

} while (num <= 50)
return num;
}


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

test("Returned value should always be greater than 50", () => {
Expand Down
56 changes: 49 additions & 7 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,75 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let newArray = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
newArray.push(allArticleTitles[i]);
}
}
return newArray;
}

/*
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
}
let newString=''
for (let i = 0; i < allArticleTitles.length; i++) {
if (
newString.length < 1 ||
newString.split(" ").length > allArticleTitles[i].split(" ").length
) {
newString = allArticleTitles[i];
}

}

return newString;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Another way of doing this is to initialise newString with the first entry, and then you can just check each subsequent entry with only one condition


/*
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
let newClicks = [];

for (let i = 0; i < allArticleTitles.length; i++) {
for (let letter of allArticleTitles[i]) {
if (letter === "0"||
letter === "1"||
letter === "2"||
letter === "3"||
letter === "4"||
letter === "5"||
letter === "6"||
letter === "7"||
letter === "8"||
letter === "9") {
newClicks.push(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.

maybe you already know there is another easy way to find out number inside array I used that
if (/[0-9]/g.test(sentence)) {
titleWithNum.push(sentence);
}


}
}
return newClicks;
}

/*
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 totalLength = 0;

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

you did it an easier way I changed my code after seeing yours thank you Saf!


Expand Down
27 changes: 24 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let newAverage =[]
let sum = 0;
for(let i = 0; i < STOCKS.length; i++){
for( let j=0; j < CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length; j++){
sum += CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i][j];
}
newAverage.push(
((sum /CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[i].length).toFixed(2)) * 1);
sum = 0;
}
return newAverage;
}


/*
We also want to see what the change in price is from the first day to the last day for each stock.
Implement the below function, which
Expand All @@ -48,7 +60,12 @@ function getAveragePrices(closingPricesForAllStocks) {
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
getPriceChange = [];
for (price of closingPricesForAllStocks) {
let priceChange = Number((price[price.length -1] - price[0]).toFixed(2));
getPriceChange.push(priceChange);
}
return getPriceChange;
}

/*
Expand All @@ -60,11 +77,15 @@ function getPriceChanges(closingPricesForAllStocks) {
- Returns an array of strings describing what the highest price was for each stock.
For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33"
The test will check for this exact string.
The stock ticker should be capitalised.
The stock ticker should be capitalized.
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
highestPriceLast5Days = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
highestPriceLast5Days.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${Math.max(...closingPricesForAllStocks[i]).toFixed(2)}`);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I have seen it on google using Math.max() it really helps to make easier our work, anyway I also used another way to solve it.

return highestPriceLast5Days;
}


Expand Down
9 changes: 8 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
*/

function factorial(input) {
// TODO
if (input === 0 || input === 1)
return 1;
for(let i = input -1; i >= 1 ; i--) {
input *= i;
}
return input;
}

console.log(factorial(3))

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

test("3! should be 6", () => {
Expand Down
10 changes: 9 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO

let newArray = [];

for (let i = 0; i < books.length; i++) {
if(books[i].rating > 4.8) {
newArray.push(books[i].title)
}
}
return newArray;
}


Expand Down
9 changes: 8 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@
*/

function generateFibonacciSequence(n) {
// TODO
let fib = [0, 1];
let newFib = [0, 1];

for(let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
newFib.push(fib[i]);
}
return newFib;
}

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