Skip to content
Merged
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
12 changes: 8 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
// Example 1
let a;
console.log(a);

// here a hasn't assigned to any value.

// Example 2
function sayHello() {
let message = "Hello";
let message = "Hello";
// this function doesn't return any value.
}


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


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

sayHelloToUser();

// user hasn't been defined here when we called the function

// Example 4
let arr = [1,2,3];
console.log(arr[3]);

// There are only 3 parameters in this array so there isn't any index number of 3, indexes start from 0 to one less than the array length which is two here
9 changes: 8 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/*
while loops can be useful when you want to execute some code as long as some condition is true.

Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-seperated string.
Using a while loop, complete the function below so it logs (using console.log) the first n even numbers as a comma-separated string.
The list of numbers should start with 0. n is being passed in as a parameter.
*/

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
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"
8 changes: 8 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,14 @@

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

console.log(evenNumbersSum(3)); // should output 6
Expand Down
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 (i = 0; i < 26; i++) {
console.log(String.fromCharCode(97 + i));
i++;

}
// The output shouldn't change.
4 changes: 4 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,10 @@ const AGES = [

// TODO - Write for loop code here

for ( i=0 ; i < 5 ; i++){
console.log(`${WRITERS[i]} is ${AGES[i]} years old`)
}

/*
The output should look something like this:

Expand Down
9 changes: 8 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,14 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for ( const element of tubeStations){
console.log(element)
}


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

for (const letter of str) {
console.log(letter.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 @@ -2,7 +2,7 @@
Imagine we're making a weather app!

We have a list of cities that the user wants to track.
We also already have a temperatureService function which will take a city as a parameter and return a temparature.
We also already have a temperatureService function which will take a city as a parameter and return a temperature.

Implement the function below:
- take the array of cities as a parameter
Expand All @@ -13,6 +13,12 @@

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


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

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

// function potentialHeadlines(allArticleTitles){
// let acceptedTitles=[]
// for (element of allArticleTitles){
// ( element.length <= 65 )? acceptedTitles.push(element):next
// }
// return acceptedTitles
// }

/*
The editor of the FT likes short headlines with only a few words!
Implement the function below, which returns the title with the fewest words.
(you can assume words will always be seperated by a space)
Implement the function below, which returns the title with the fewest words.e
(you can assume words will always be separated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}
let minValue
let indexOfShortestTitle
let wordCountOFTitles=[]
let i=0
do {
let wordCount= allArticleTitles[i].trim().split(" ").length
wordCountOFTitles.push(wordCount)
i++
}
while(i < allArticleTitles.length)

minValue= Math.min(...wordCountOFTitles)
indexOfShortestTitle=wordCountOFTitles.indexOf(minValue)

return allArticleTitles[indexOfShortestTitle]
}
/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
The editor of the FT has realized that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let titleWithNum=[]
for (const title of allArticleTitles){
if (/\d/.test(title)){
titleWithNum.push(title)
}
}
return titleWithNum
}

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


Expand Down
47 changes: 45 additions & 2 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,35 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/
// function getAveragePrices(closingPricesForAllStocks) {
// // TODO
// let averageArray=[]
// for (const array of closingPricesForAllStocks){
// let sum=0
// let average=0
// for (const value of array){
// sum+=value
// }
// average= parseFloat((sum / closingPricesForAllStocks[i].length).toFixed(2))
// averageArray.push(average)
// }
// return averageArray
// }

function getAveragePrices(closingPricesForAllStocks) {
// TODO
let aveArray=[]
for (i=0 ; i< closingPricesForAllStocks.length;i++){
let sum=0
let average=0
let j=0
do{
sum+= closingPricesForAllStocks[i][j]
j++
}while(j< closingPricesForAllStocks[i].length)
average= parseFloat((sum / closingPricesForAllStocks[i].length).toFixed(2))
aveArray.push(average)
}
return aveArray
}

/*
Expand All @@ -49,6 +76,13 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let priceChangeArray=[]
for(i=0 ; i< closingPricesForAllStocks.length ; i++){
let priceChange=0
priceChange =parseFloat((closingPricesForAllStocks[i][(closingPricesForAllStocks[i].length -1)]-closingPricesForAllStocks[i][0]).toFixed(2))
priceChangeArray.push(priceChange)
}
return priceChangeArray
}

/*
Expand All @@ -60,11 +94,20 @@ function getPriceChanges(closingPricesForAllStocks) {
- Returns an array of strings describing what the highest price was for each stock.
For example, the first element of the array should be: "The highest price of AAPL in the last 5 days was 180.33"
The test will check for this exact string.
The stock ticker should be capitalised.
The stock ticker should be capitalized.
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let describingArray=[]
let max
let description
for (i=0 ; i< stocks.length ; i++){
max= Math.max(...closingPricesForAllStocks[i])
description= `The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${max.toFixed(2)}`
describingArray.push(description)
}
return describingArray
}


Expand Down