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
// a does'nt have any value.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

a has the default value for a variable when no assignment has taken place, which is undefined.

let a;
console.log(a);


// Example 2
// The function sayHello doesn't return anything.
function sayHello() {
let message = "Hello";
}
Expand All @@ -23,14 +23,14 @@ let hello = sayHello();
console.log(hello);


// Example 3
// The function is recalled with no parameter.
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();


// Example 4
// The array has no 4th element (index is counted from 0).
let arr = [1,2,3];
console.log(arr[3]);
4 changes: 2 additions & 2 deletions 1-exercises/B-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Declare some variables assigned to arrays of values
*/

let numbers = []; // add numbers from 1 to 10 into this array
let mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares
let numbers = [1,2,3,4,5,6,7,8,9,10]; // add numbers from 1 to 10 into this array
let mentors=["Daniel", "Irina","Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/C-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

function first(arr) {
return; // complete this statement
return arr[0]; // complete this statement
}

function last(arr) {
return; // complete this statement
return arr[arr.length-1]; // complete this statement
}

/*
Expand Down
3 changes: 2 additions & 1 deletion 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

let numbers = [1, 2, 3]; // Don't change this array literal declaration

numbers[numbers.length]=4;
numbers[0]=1; //!
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
3 changes: 3 additions & 0 deletions 1-exercises/D-for-loop/exercise.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
11 changes: 8 additions & 3 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ const BIRTHDAYS = [
"July 11th",
"July 17th",
"September 28th",
"November 15th"
"November 15th",
];

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"
5 changes: 5 additions & 0 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

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


Expand Down
41 changes: 35 additions & 6 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,61 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
const fitArticleTitles=[];
for (let i=0; i<allArticleTitles.length; i++){
if(allArticleTitles[i].length<=65){
fitArticleTitles.push(allArticleTitles[i]);
}
}
return fitArticleTitles;
}

/*
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)
(you can assume words will always be separated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
}

let fewestWords=allArticleTitles[0];
for( let i =1; i<allArticleTitles.length; i++){
if(fewestWords.length>allArticleTitles[i].length){
fewestWords=allArticleTitles[i];
}
}
return fewestWords;
}
// function getWordsCount(allArticleTitles){
// return allArticleTitles.split(" ").length;
// }
/*
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
const containNumbers=[];
for(let i =0; i<allArticleTitles.length; i++){
let isContainNum= /[0-9]/.test(allArticleTitles[i]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Interesting use of a regular expression here!

if(isContainNum){
containNumbers.push(allArticleTitles[i]);
}
}
return containNumbers;
}


/*
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 numOfChar=0;
for(let i =0; i<allArticleTitles.length; i++){
numOfChar += allArticleTitles[i].length;
}
let average= numOfChar / allArticleTitles.length;
return Math.round(average);
}


Expand Down
44 changes: 40 additions & 4 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
THESE EXERCISES ARE QUITE HARD. JUST DO YOUR BEST, AND COME WITH QUESTIONS IF YOU GET STUCK :)

Imagine we a working for a finance company. Below we have:
Imagine we are working for a finance company. Below we have:
- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
For example, CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2] contains the prices for the last 5 days for STOCKS[2] (which is amzn)
Expand Down Expand Up @@ -35,6 +35,19 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
const averagePrice=[];
let sum=0;
let average=0
for(let i =0; i<closingPricesForAllStocks.length; i++){
for( let j=0; j<closingPricesForAllStocks[i].length;j++){
sum +=closingPricesForAllStocks[i][j];
}
average=sum / 5;
averagePrice.push(Math.round(average*100)/100);
sum=0;

}
return averagePrice;
}

/*
Expand All @@ -48,8 +61,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
}
const priceChanged=[];
let change=0;
let average=0
for(let i =0; i<closingPricesForAllStocks.length; i++){
let j=closingPricesForAllStocks[i][closingPricesForAllStocks[i].length-1];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's usually better to use full descriptions in variable names - variables like I, j and k can be used but usually as counters in loops (as has been used here with i) by convention. Try to think of a quite short but descriptive name to use - it should make the code that uses the variables easier to understand!

let k= closingPricesForAllStocks[i][0];
change =j-k;
priceChanged.push(Math.round(change*100)/100);
change=0;
}
return priceChanged;
}

/*
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 @@ -64,7 +87,20 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const hightestPrice=[];
let maxPrice=0;
for(let k=0; k<stocks.length;){
for(let i=0; i< closingPricesForAllStocks.length; i++){

maxPrice=Math.max(...closingPricesForAllStocks[i]);
hightestPrice.push(`The highest price of ${stocks[k].toUpperCase()} in the last 5 days was ${maxPrice.toFixed(2)}`)
maxPrice=0;
k++;

}

return hightestPrice;
}
}


Expand Down
20 changes: 20 additions & 0 deletions 3-extra/1-radio-stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
*/

// `getAllFrequencies` goes here
function getAllFrequencies(){
const frequencies=[];
let k=87;
for( let i=0; i<22;i++){
frequencies.push(k);
k++;
}
return frequencies;

}

/**
* Next, let's write a function that gives us only the frequencies that are radio stations.
Expand All @@ -25,7 +35,17 @@
* - Return only the frequencies that are radio stations.
*/
// `getStations` goes here
function getStations(){
const stations=[];
const availableFreq= getAllFrequencies();
for(let i =0; i<availableFreq.length; i++){
if(isRadioStation(availableFreq[i])){
stations.push(availableFreq[i]);
}
}
return stations;

}
/*
* ======= TESTS - DO NOT MODIFY =======
* Note: You are not expected to understand everything below this comment!
Expand Down
53 changes: 53 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,58 @@
*/

function getHighestRatedInEachGenre(books) {
let booksLength= books.length;
let children=[];
let fiction=[];
let cooking=[];
let resultArr=[];
for (let i=0;i<booksLength; i++){
if(books[i].genre=== "children"){
children.push(books[i]);
}
if(books[i].genre=== "non-fiction"){
fiction.push(books[i]);
}
if(books[i].genre=== "cooking"){
cooking.push(books[i]);
}
}
let childrenRate = 0 ;
let childrenTitle = "";
for (let j=0 ;j<children.length; j++){
if(children[j].rating > childrenRate){
childrenRate = children[j].rating;
childrenTitle = children[j].title;
}

}


let nonfictionRate = 0 ;
let nonfictionTitle = "";
for (let j=0 ;j<fiction.length; j++){
if(fiction[j].rating > nonfictionRate){
nonfictionRate = fiction[j].rating;
nonfictionTitle = fiction[j].title;
}

}


let cookingRate = 0 ;
let cookingTitle = "";
for (let j=0 ;j<cooking.length; j++){
if(cooking[j].rating > cookingRate){
cookingRate = cooking[j].rating;
cookingTitle = cooking[j].title;
}

}
resultArr.push(nonfictionTitle);
resultArr.push(childrenTitle);
resultArr.push(cookingTitle);
return resultArr;

// TODO
}

Expand Down Expand Up @@ -68,6 +120,7 @@ const BOOKS = [
rating: 4.85
},
]
// console.log(getHighestRatedInEachGenre(BOOKS));


/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
8 changes: 8 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

function generateFibonacciSequence(n) {
// TODO
const FibSequence=[];
FibSequence[0]=0;
FibSequence[1]=1;
for(let i=2; i<n; i++){
FibSequence[i]=FibSequence[i-1]+FibSequence[i-2];
}
return FibSequence;

}

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