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


// Example 2
// Example 2 message not assigned
function sayHello() {
let message = "Hello";
}
Expand All @@ -23,14 +24,14 @@ let hello = sayHello();
console.log(hello);


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

sayHelloToUser();


// Example 4
// Example 4 array is only 3 objects and starts at 0. no ob at pos 3
let arr = [1,2,3];
console.log(arr[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
const arr = [];
let i = 0;
while(i < n * 2) {
arr.push(i);
i +=2;
}
return arr;
}

evenNumbers(3); // should output 0,2,4
Expand Down
7 changes: 7 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,13 @@ 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: 11 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,17 @@

function evenNumbersSum(n) {
// TODO
let i = 0, j = 0, k =0
if (n != 0) {
do {
i += 2
k += i
j++
} while(j < n-1)
return k
} else {
return 0
}
}

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


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

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

for (let station of tubeStations) {
console.log(station)
}

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

for (let alphabet of str) {
console.log(alphabet.toUpperCase())
}
7 changes: 7 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

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


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
22 changes: 22 additions & 0 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let headline = []
for (article of allArticleTitles){
if (article.length < 65) headline.push(article) ;
}
return headline;
}

/*
Expand All @@ -15,6 +20,11 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let lowestWords = [];
for (let i = 0; i< allArticleTitles.length; i++) {
lowestWords.push(allArticleTitles[i].split(' ').length)
}
return allArticleTitles[lowestWords.indexOf(Math.min(...lowestWords))]
}

/*
Expand All @@ -24,6 +34,13 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let numberArray = [];
for (let article of allArticleTitles) {
if (/[0-9]/.test(article) === true) {
numberArray.push(article);
}
}
return numberArray;
}

/*
Expand All @@ -32,6 +49,11 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let averageCharacter = [];
for (let i = 0; i < allArticleTitles.length; i++) {
averageCharacter += allArticleTitles[i].length
}
return Math.round(averageCharacter / allArticleTitles.length)
}


Expand Down
27 changes: 27 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averagePriceArray = [];
for (let individualArray of closingPricesForAllStocks) {
averagePriceArray.push(individualAverage(individualArray));
}
return averagePriceArray;

}
function individualAverage(array) {
let total = 0;
for (let closing of array) {
total += closing;
}
return Number(total / array.length).toFixed(2);
}

/*
Expand All @@ -49,6 +62,12 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let priceChanges = [];
for (let priceArray of closingPricesForAllStocks) {
let priceChanges = [priceArray.length-1] - priceArray[0];
priceChanges.push(Number(priceChanges.toFixed(2)));
}
return priceChanges;
}

/*
Expand All @@ -65,6 +84,14 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let maxedStock = [];
for (i = 0; i < stocks.length; i++) {
let capitaliseStock = stocks[i].toUpperCase();
let highestPrice = Math.max(...closingPricesForAllStocks[i]);
let decimalPrice = highestPrice.toFixed(2);
maxedStock.push(`The highest price of ${capitaliseStock} in the last five days was ${decimalPrice}`);
}
return maxedStock;
}


Expand Down
7 changes: 7 additions & 0 deletions 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

function factorial(input) {
// TODO
let total = 1;
for (i = 1; i<=input; i++) {
if (input > 1) {
total *= i;
}
}
return total;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
36 changes: 36 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,45 @@
Implement a function which takes the array of books as a parameter, and returns an array of book titles.
Each title in the resulting array should be the highest rated book in its genre.
*/
// first get a list of all genres that are unique
// next would be to get the highest rated in that genre
//final would be to loop thru the book array and get the title with highest rating

function uniqueGenres(books) {
let genres = [];
for (let book of books) {
genres.push (book.genre)
}
return [...new Set(genres)];
}

// highest rated title in each genres
function highestRated(books, genre) {
let ratingArray = [];
for( let book of books) {
if (book.genre === genre) {
ratingArray.push(book.rating);
}
}
return Math.max(...ratingArray);
}



function getHighestRatedInEachGenre(books) {
// TODO
let list = [];
let genres = uniqueGenres(books);
for (let book of books) {
for (let genre of genres) {
if (
book.rating === highestRated(books, genre) && book.genre === genre
) {
list.push(book.title);
}
}
}
return list;
}


Expand Down
12 changes: 12 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@

function generateFibonacciSequence(n) {
// TODO
let newSequence = [];
let amount =0;
for (let i = 0; i<n; i++){
if (newSequence.length >=2) {
amount = newSequence[i -1] + newSequence[i -2];
newSequence.push(amount);
} else {
amount += i;
newSequence.push (amount);
}
}
return newSequence;
}

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