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 @@ -11,26 +11,27 @@

// Example 1
let a;
console.log(a);

console.log
// Answer 1:Becuase no value was given to console.log

// Example 2
function sayHello() {
let message = "Hello";
}

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

// Answer: for the reason the functin sayHello does not return any value.
// and when call it , we recieve undifined.

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

sayHelloToUser();
//Answer: becuase the first argumant does not assign a value.( in line 30)


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//Answer: there is no index 3 in the array (in line 35)
20 changes: 18 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@
*/

function evenNumbers(n) {
// TODO
}
let output =''; // '' === false
let counter = 0;

while( counter < n ){
if ( counter == 0 ){
output = "0"
}else{ ''
output = `${output},${(counter * 2)}` //output + "," + (counter * 2)
}
counter++;
}

if( output ){
console.log( output );
}else{
console.log("should output nothing");
}
}

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
Expand Up @@ -14,10 +14,16 @@ const BIRTHDAYS = [
"July 17th",
"September 28th",
"November 15th"
];
];

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

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

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

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


// 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++;

for( let i = 0; i < 26; i++ ) {
console.log(String.fromCharCode(97 + i));
}
// The output shouldn't change.

8 changes: 3 additions & 5 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ 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:

Virginia Woolf is 59 years old
Zadie Smith is 40 years old
Jane Austen is 41 years old
Bell Hooks is 63 years old
Yukiko Motoya is 49 years old
*/
11 changes: 10 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,15 @@ let tubeStations = [
"Tottenham Court Road"
];


for(let words of tubeStations) {
console.log(words)
}
// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
let capitalise = "";
for (let letter of str) {
capitalise += letter.toUpperCase();
console.log(letter.toUpperCase());
}
console.log("_____________________");
console.log(capitalise);
11 changes: 9 additions & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@
- Hint: you can call the temperatureService function from your function
*/

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


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 random;
do{
random = generateRandomNumber()
}while( random < 50 );
return random;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
35 changes: 31 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 articles = [];
for (let item of allArticleTitles ){
if (item.length <= 65 ){
articles.push(item);
}
}
return articles;
}

/*
Expand All @@ -14,7 +20,16 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestCount;
let fewestSentence;
for(let title of allArticleTitles) {
let curentCount = title.split(' ').length;
if( fewestCount === undefined || curentCount < fewestCount) {
fewestCount = curentCount;
fewestSentence = title;
}
}
return fewestSentence;
}

/*
Expand All @@ -23,15 +38,27 @@ 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 article = [];
for( let item of allArticleTitles ){
for( let string of item ){
if ( string >= '0' && string <= '9' ){
article.push( item );
}
}
}
return article;
}

/*
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 total = 0;
for(let title of allArticleTitles) {
total += title.length;
}
return Math.round(total / allArticleTitles.length);
}


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 @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let avg = [];
for(let item of closingPricesForAllStocks ){
let sum = 0;
for(let index of item ){
sum += index;
}
avg.push( Math.round( sum / item.length * 100 ) / 100 );
}
return avg;
}

/*
Expand All @@ -48,7 +56,11 @@ 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
let changes = [];
for( let item of closingPricesForAllStocks ){
changes.push( Math.round( ( item[item.length - 1] - item[0] ) * 100 ) / 100 );
}
return changes;
}

/*
Expand All @@ -64,7 +76,16 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highest = [];

for( let index in stocks ){
let item = closingPricesForAllStocks[index];
item.sort(function(a , b){
return b - a;
});
highest.push(`The highest price of ${stocks[index].toUpperCase()} in the last 5 days was ${item[0].toFixed(2)}`);
}
return highest;
}


Expand Down