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
15 changes: 14 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,20 @@
*/

function evenNumbers(n) {
// TODO
let i = 0;
let length=0;
let newArr=[];

while (length <n ) {
if (i % 2 == 0 && length <= n){
length=newArr.push(i);
}
i++;

}
let String = newArr.toString();
console.log (String);

}

evenNumbers(3); // should output 0,2,4
Expand Down
13 changes: 11 additions & 2 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Loops can be useful when working with arrays.
In the below example, imagine we've defined an array holding the birthdays of your closest friends.
Use a while loop to search through the array until you find the first birthday in July, then return that birthday from the function.
let result = text.indexOf("welcome");
*/

const BIRTHDAYS = [
Expand All @@ -17,7 +18,15 @@ const BIRTHDAYS = [
];

function findFirstJulyBDay(birthdays) {
// TODO
/* let i=0;
while (i<=8 ) {
if (birthdays)
i++;

}*/
let result = birthdays.indexOf('July');
// console.table(birthdays[6]);
return result;

}

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

function evenNumbersSum(n) {
// TODO
let i = 0;
let length=0;
let newArr=[];
let sum=0;

do {
if (i % 2 == 0 && length <= n){
length=newArr.push(i);
sum+=i;
}
i++;

}while (length <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 (let 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 @@ -26,6 +26,10 @@ const AGES = [
49
];

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

// TODO - Write for loop code here

/*
Expand Down
6 changes: 6 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,13 @@ let tubeStations = [
"Oxford Street",
"Tottenham Court Road"
];
for (let x of tubeStations) {
console.log(x);
}


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

function getTemperatureReport(cities) {
// TODO
let temparatureStatements = [];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo here! Typos in variable names often cause bugs so it's really worth picking them up. There is a spellchecker package you can install in VSCode https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker

for (let i = 0; i < cities.length; i++){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When you get the solutions for this week, take a look at the example solution. Yours works and is a correct answer; the mentor chose a different strategy. Can you think what the advantages of the mentor's choices are?

temparatureStatements[i]="The temperature in "+cities[i]+" is "+temperatureService(cities[i])+" degrees";}
return temparatureStatements;
}


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() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This solution works well.

Have you installed Prettier and run it in your VSCode? What does the style guide say about this? https://syllabus.codeyourfuture.io/guides/code-style-guide#using-prettier-to-format-code-automatically

// TODO - implement using a do-while loop
let i = 0;
do{
i=generateRandomNumber();
if (i>50) return(i);
}while(i<=50);
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
51 changes: 50 additions & 1 deletion 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,65 @@
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let checkedArticleTitles=[];
for (let i = 0; i < allArticleTitles.length; i++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This works well and is a correct answer.

When you look at the solutions, the mentor used a different kind of loop. Can you think why?

if (allArticleTitles[i].length<=65) {
checkedArticleTitles.push(allArticleTitles[i]);
}
}
return checkedArticleTitles;
}

/*
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)
let wordNums = []
for (let i = 0; i < allArticleTitles.length; i++) {
wordNums.push(allArticleTitles[i].split(' ').length)
}
return allArticleTitles[wordNums.indexOf(Math.min(...wordNums))]


*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let allArticleTitleswithspacecount=[];
let minIndex;
/*for (i=0;i<allArticleTitles.length;i++)
{

allArticleTitleswithspacecount.push(allArticleTitles[i].split(' ').length);
}

minIndex=Math.min.apply(Math, allArticleTitleswithspacecount);
return(allArticleTitles[minIndex]);
let wordNums = []*/
for (let i = 0; i < allArticleTitles.length; i++) {
allArticleTitleswithspacecount.push(allArticleTitles[i].split(' ').length)
}
return allArticleTitles[allArticleTitleswithspacecount.indexOf(Math.min(...allArticleTitleswithspacecount))]

}


/*
The editor of the FT has realised 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 headlineWithNumberArr=[]
let length=allArticleTitles.length;
const numberPattern=/\d/;

for (let i = 0; i < length; i++) {
if (numberPattern.test(allArticleTitles[i])) {
headlineWithNumberArr.push(allArticleTitles[i]);
}
}
return(headlineWithNumberArr);
}

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


Expand Down
12 changes: 12 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
@@ -1,6 +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:
- an array of stock tickers
- an array of arrays containing the closing price for each stock in each of the last 5 days.
Expand Down Expand Up @@ -35,6 +36,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
/* let sum=0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This code seems to be commented out!

let averagePricesForAllStocks=[];
let length=closingPricesForAllStocks.length;
for (let i = 0; i < length; i++) {
let jlength=closingPricesForAllStocks[i].length;
for (let j=0;j<jlength;j++){
sum+=allArticleTitles[i][j];
}
averagePricesForAllStocks.push(Math.round((sum/jlength)* 100) / 100);
}
return(averagePricesForAllStocks);
}

/*
Expand Down