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
8 changes: 5 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
By now, you would have already seen "undefined", either in an error message or being output from your program.
By now, you would have already seen "undefined", either in an error message or being output from your program.
But what does it mean? undefined represents the absence of a value.

In some cases, undefined will be used by a programmer intentionally, and they will write code to handle it.
Expand All @@ -12,6 +12,7 @@
// Example 1
let a;
console.log(a);
//"a" is not set to any value


// Example 2
Expand All @@ -21,16 +22,17 @@ function sayHello() {

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

//the function doesn't return any value

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

//is not declared the variables
sayHelloToUser();


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//index start from 0, this mean in "(arr[])" should be 2, not 3
7 changes: 7 additions & 0 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

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

evenNumbers(3); // should output 0,2,4
Expand Down
9 changes: 9 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ const BIRTHDAYS = [
];

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

// TODO
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"

11 changes: 9 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
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
function evenNumbersSum(number) {
let sum = 0;
let i = 0;
do {
sum = i * (i + 1);
i++;
} while (i < number);
return sum;
}


console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
6 changes: 3 additions & 3 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) {
for (let i = 0;
i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;

}
// The output shouldn't change.
4 changes: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const AGES = [
49
];

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

/*
The output should look something like this:
Expand Down
4 changes: 4 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ let tubeStations = [

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

for (let char of str) {
console.log(char.toUpperCase());
}
8 changes: 7 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
*/

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

}


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

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

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

Expand Down
7 changes: 6 additions & 1 deletion 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let arr = [];
for (const element of allArticleTitles) {
if (element.length <= 65) {
arr.push(element);
}
}return arr;
}

/*
Expand Down
35 changes: 32 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let total = 0;
let average = 0;
arrayOfAverages = [];
for (const subArray of closingPricesForAllStocks) {
for (const element of subArray) {
total = total + element;
}
average = total / subArray.length;
total = 0;
arrayOfAverages.push(Math.round(average *100)/100);
}
return arrayOfAverages;
}

/*
Expand All @@ -48,7 +59,15 @@ 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 priceDifference = 0;
let arrayOfChanges = [];
for (const subArray of closingPricesForAllStocks) {

priceDifference = Math.round((subArray[subArray.length -1] - subArray[0]) * 100) /100;
arrayOfChanges.push(priceDifference);
}

return arrayOfChanges;
}

/*
Expand All @@ -64,7 +83,17 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPriceEach = 0;
let arrayOfPrices = [];
let i = 0;
for (const subArray of closingPricesForAllStocks) {

highestPriceEach = Math.max(...subArray).toFixed(2);
arrayOfPrices.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPriceEach}`);
i++

}
return arrayOfPrices;
}


Expand Down