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
10 changes: 5 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
// Example 1
let a;
console.log(a);

//a has not been assigned any value.

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

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

// function has not return value.

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

sayHelloToUser();

// function does not pass the parameter value.

// Example 4
let arr = [1,2,3];
console.log(arr[3]);
// index 3 does not exist.
12 changes: 11 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
*/

function evenNumbers(n) {
// TODO

let i = 0;

while (n > 0) {

if(i % 2 == 0){
console.log(i);
n = n - 1;
}
i = i + 1;
}
}

evenNumbers(3); // should output 0,2,4
Expand Down
13 changes: 12 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,18 @@ const BIRTHDAYS = [
];

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

if (birthdays[i].startsWith("July")) {
console.log(birthdays[i]);
break;
}

i++;
}
}


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

function evenNumbersSum(n) {
// TODO
let i = 0;
let sumofnumbers = 0;
do {
if(i % 2 == 0) {
sumofnumbers = sumofnumbers + i;

n = n - 1;
}

i = i + 1;


} while (n > 0)
console.log(sumofnumbers);
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
12 changes: 9 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,15 @@


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


for (i = 0 ;i < 26 ; i++) {
console.log(String.fromCharCode(97 + i));
i++;

}
// 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
Expand Up @@ -26,7 +26,12 @@ const AGES = [
49
];

// TODO - Write for loop code here
const combined=[]

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
7 changes: 7 additions & 0 deletions 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ let tubeStations = [
"Tottenham Court Road"
];

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


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

function getTemperatureReport(cities) {
// TODO
let tempArray = [];
for (i=0; i < cities.length; i++ ) {
let deg = temperatureService(cities[i]);
tempArray.push(`The temperature in ${cities[i]} is ${deg} degrees`);
}
return tempArray;
}


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
56 changes: 51 additions & 5 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
function potentialHeadlines(allArticleTitles)
{
let articleArray = [];
{
for (const currentArticle of allArticleTitles) {
if (currentArticle.length <= 65) {
articleArray.push(currentArticle);
}
}
}return articleArray
}

/*
Expand All @@ -14,7 +22,25 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let articleWithFewestWords = 0;
let numberOfWords = 0;

for (const currentArticle of allArticleTitles) {

numberOfWordsCurrent = currentArticle.length;

if ( numberOfWords == 0 ) {
numberOfWords = numberOfWordsCurrent;
articleWithFewestWords = currentArticle;
}

if (numberOfWordsCurrent < numberOfWords) {
numberOfWords = numberOfWordsCurrent;
articleWithFewestWords = currentArticle;
}
}

return articleWithFewestWords;
}

/*
Expand All @@ -23,15 +49,35 @@ 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 headlineWithNumbers = [];
let digits = /\d+/g;
let headlines = potentialHeadlines(allArticleTitles);

for (const headline in headlines) {

if (headline.match(digits)) {
headlineWithNumbers.push(headline);
}
}

return headlineWithNumbers;

}

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

for (let i = 0; i < allArticleTitles.length; i++) {
lengthOfAllArticles += allArticleTitles[i].length;
}

let averageChar = lengthOfAllArticles / allArticleTitles.length;
return Math.round(averageChar);
}


Expand Down
43 changes: 40 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averagePriceArr = [];
for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let totalStock = 0;
let averageStockSum = 0;
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
totalStock += closingPricesForAllStocks[i][j];
}
averageStockSum = totalStock / closingPricesForAllStocks[i].length;
let value = averageStockSum.toFixed(2);
averagePriceArr.push(parseFloat(value));
}

return averagePriceArr;
}

/*
Expand All @@ -48,7 +60,18 @@ 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 priceChangeValue = [];

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let difference = closingPricesForAllStocks[i].length - 1;
let totalDifference =
closingPricesForAllStocks[i][difference] -
closingPricesForAllStocks[i][0];

priceChangeValue.push(parseFloat(totalDifference.toFixed(2)));
}

return priceChangeValue;
}

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

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let total = 0;
for (let j = 0; j < closingPricesForAllStocks[i].length; j++) {
if (closingPricesForAllStocks[i][j] > total) {
total = closingPricesForAllStocks[i][j];
}
}
highestPrices.push(
`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${total.toFixed(2)}`
);
}

return highestPrices;
}


Expand Down