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

//Answer: Because a has not been assigned any value

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


let hello = sayHello();
console.log(hello);
//Answer: Because the function is not returning anything


// Example 3
Expand All @@ -29,8 +32,10 @@ function sayHelloToUser(user) {
}

sayHelloToUser();
//Answer: Because no arguement has been passed inside the function when calling it


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//Answer: Because the arr has 3 elements in it. So arr[3] means 4th element which is not present in the arr
21 changes: 16 additions & 5 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;
let evenNumberArray = []
while (evenNumberArray.length < n) {
if (i%2 === 0) {
let strNum = i.toString()
evenNumberArray.push(strNum);
}

i++
}

return evenNumberArray.join(',')

}

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
console.log(evenNumbers(3)); // should output 0,2,4(
console.log(evenNumbers(0)); // should output nothing
console.log(evenNumbers(10)); // should output 0,2,4,6,8,10,12,14,16,18
6 changes: 5 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,11 @@ const BIRTHDAYS = [
];

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

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

function evenNumbersSum(n) {
// TODO
let currentNum = 0;
let evenNumArr = [];

do {
if (currentNum%2 === 0 ) {
evenNumArr.push(currentNum)

}
currentNum += 1;

} while (evenNumArr.length < n)
return evenNumArr.reduce((prevNum, num) => {return prevNum+num})
}

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


// 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.
5 changes: 5 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const AGES = [

// TODO - Write for loop code here


for (let writerIndex = 0 ; writerIndex<WRITERS.length; writerIndex++) {
console.log(`${WRITERS[writerIndex]} is ${AGES[writerIndex]} 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 @@ -10,7 +10,14 @@ 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 letter of str) {
let capLetter = letter.toLocaleUpperCase()
console.log(capLetter)
}
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 tempDetailArr = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice code structure, easy to read. Great work Monika

for (let cityName of cities) {
const cityTemp = temperatureService(cityName)
const tempDetail = `The temperature in ${cityName} is ${cityTemp} degrees`
tempDetailArr.push(tempDetail)
}
return tempDetailArr
}


Expand Down
8 changes: 7 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,13 @@ function generateRandomNumber() {
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let randNumber = ''
do {
generateRandomNumber()
randNumber = generateRandomNumber()

} while (randNumber < 50)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if the random number is 50? Should the loop be carrying on? Will it?

return randNumber
}

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this 6 come from? Is there something else that we can compare to that would work even if all the titles are longer than 6 words?

return title
}
}


}

/*
Expand All @@ -23,15 +36,28 @@ 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 titleWithNumArr = []
for (let title of allArticleTitles) {
let reg = /\d/;
if (reg.test(title)) {
titleWithNumArr.push(title)
}
}
return titleWithNumArr;
}

/*
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 numOfCharArr = []
for (let title of allArticleTitles) {
numOfCharArr.push(Number(title.length))
}
const totalOfChar = numOfCharArr.reduce((prevTitleChar, currTitleChar) => {return prevTitleChar + currTitleChar});
const avgNumOfChar = (totalOfChar/numOfCharArr.length).toFixed()
return Number(avgNumOfChar)
}


Expand Down
38 changes: 35 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let avgPricesArr = [];

for (let prices of closingPricesForAllStocks) {
let totalPrices = prices.reduce((prevPrice, currPrice) => { return (prevPrice + currPrice)})
let avgPrices = (totalPrices/5).toFixed(2)
avgPricesArr.push(Number(avgPrices))
}
return avgPricesArr
}

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


for (let price of closingPricesForAllStocks) {

let priceDiff = (price[price.length - 1] - price[0]).toFixed(2)
priceDiffArr.push(Number(priceDiff))
}
return priceDiffArr
}

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



function compareFunction(a, b) {
return b-a
}


for (let currStock = 0; currStock < closingPricesForAllStocks.length; currStock++) {
const sortedPrice = closingPricesForAllStocks[currStock].sort(compareFunction)
const highestPrice = sortedPrice[0].toFixed(2);
const highestPriceInt = Number(highestPrice).toFixed(2)
const highestPriceDescription = `The highest price of ${stocks[currStock].toUpperCase()} in the last 5 days was ${highestPriceInt}`
highestPriceDescriptionArr.push(highestPriceDescription)

}
return highestPriceDescriptionArr
}


Expand Down
9 changes: 8 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
*/

function factorial(input) {
// TODO
let numReducedByOneArr = []
while (input !== 1) {
let newNum = input
numReducedByOneArr.push(newNum)
input--
}
const multipleOfAll = numReducedByOneArr.reduce((prev, curr) => {return prev * curr })
return multipleOfAll
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
45 changes: 44 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,51 @@
Each title in the resulting array should be the highest rated book in its genre.
*/

let higherRatedBook = []
let higherRate = 0;
let genreArray = []

// function to collect all the unique genres in the object and add it to the genreArray
function createGenreArray(availableBooks) {

for (let everyBook of availableBooks) {
if (!genreArray.includes(everyBook.genre)) {
genreArray.push(everyBook.genre)
}
}
return genreArray
}

function getHighestRatedInEachGenre(books) {
// TODO
const allAvailableGenres = createGenreArray(books)



// let highestRating;
let highestRatedBooksArr = [];

for (let i = 0; i<allAvailableGenres.length; i++) {
let higherRating = 0 //to catch the highest rated book in a genre
let highestRatedTitle = "" //to catch the title of the highest rated book in a genre
for (let book of books) {

if (allAvailableGenres[i] === book.genre) {

if (higherRating < book.rating) {
higherRating = book.rating; // higher rating changes every time it finds the higher rating in a genre
highestRatedTitle = book.title // similarly, title changes for the higher rated book in a genre


}


}

}
highestRatedBooksArr.push(highestRatedTitle)

}
return highestRatedBooksArr
}


Expand Down
10 changes: 9 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
*/

function generateFibonacciSequence(n) {
// TODO
let fibonacciNumArr = [0, 1];

for (let i=2; i<n; i++) {
fibonacciNumArr[i] = fibonacciNumArr[i-2] + fibonacciNumArr[i-1]



}
return fibonacciNumArr
}

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