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: 4 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
For each example, can you explain why we are seeing undefined?
*/

// Example 1
// Example 1- variable "a" has not been declared.
let a;
console.log(a);


// Example 2
// Example 2- no value inside function parantheses so it doesnt returned any value.
function sayHello() {
let message = "Hello";
}
Expand All @@ -23,14 +23,14 @@ let hello = sayHello();
console.log(hello);


// Example 3
// Example 3- the function has been called with empty parameter
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();


// Example 4
// Example 4- the index [3] does not exist in Array list.
let arr = [1,2,3];
console.log(arr[3]);
11 changes: 11 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,17 @@

function evenNumbers(n) {
// TODO
let i = 0;
let numbersList = [];
while (numbersList.length < n) {
if (i % 2 === 0) {
numbersList.push(i);
}
i++;
}
console.log(numbersList.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 @@ -18,6 +18,15 @@ const BIRTHDAYS = [

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

}

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

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

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


// 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++;
}
Expand Down
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 @@ -27,7 +27,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
3 changes: 3 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,6 @@ let tubeStations = [

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for (let i = 0; i < str.length; i ++) {
console.log(str[i].toUpperCase())
}
6 changes: 6 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

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


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
38 changes: 38 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let headlines = [];

for (let eachTitle of allArticleTitles) {
if (eachTitle.length <= 65) {
headlines.push(eachTitle);
}
}

return headlines;
}

/*
Expand All @@ -15,6 +24,19 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let leastWordsTillNow;
let headlineWithLeastWords;

for (let eachHeadline of allArticleTitles) {
let numberOfWords = eachHeadline.split(" ").length;

if (leastWordsTillNow === undefined || leastWordsTillNow > numberOfWords) {
leastWordsTillNow = numberOfWords;
headlineWithLeastWords = eachHeadline;
}
}

return headlineWithLeastWords;
}

/*
Expand All @@ -24,6 +46,15 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let headlineTitlesWithNumbers = [];

for (let eachHeadline of allArticleTitles) {
if (headlineTitleHasNumbers(eachHeadline)) {
headlineTitlesWithNumbers.push(eachHeadline);
}
}

return headlineTitlesWithNumbers;
}

/*
Expand All @@ -32,6 +63,13 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let characterTotal = 0;

for (let eachTitle of allArticleTitles) {
characterTotal += eachTitle.length;
}

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


Expand Down
29 changes: 28 additions & 1 deletion 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let average = [];
let stockSum = 0;
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
stockSum = 0;
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
stockSum = stockSum + closingPricesForAllStocks[i][j];
}
average.push(
Number((stockSum / closingPricesForAllStocks[i].length).toFixed(2))
);
}
return average;
}

/*
Expand All @@ -49,7 +61,16 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
}
let priceChangeArr = [];

for (price of closingPricesForAllStocks) {
let priceChange = Number((price[price.length - 1] - price[0]).toFixed(2));
priceChangeArr.push(priceChange);
}
return priceChangeArr;
}



/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Expand All @@ -65,6 +86,12 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
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)}`);
}
return highestPriceLast5Days;

}


Expand Down