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
// Example 1: because no value assign to the variable "a"
let a;
console.log(a);


// Example 2
// Example 2: because function does not have any return.
function sayHello() {
let message = "Hello";
}
Expand All @@ -23,14 +23,14 @@ let hello = sayHello();
console.log(hello);


// Example 3
// Example 3: because there is no argument when call the function.
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();


// Example 4
// Example 4: because arr[3] dose not have value.
let arr = [1,2,3];
console.log(arr[3]);
10 changes: 10 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,16 @@

function evenNumbers(n) {
// TODO
let i=0;
let evenNumber = 0;
let arr = [];
while(i<n){
evenNumber = i*2;
arr[i] = evenNumber;
i++;
}
let commaSeperated = arr.join(",");
console.log(commaSeperated);
}

evenNumbers(3); // should output 0,2,4
Expand Down
8 changes: 8 additions & 0 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
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.
Expand All @@ -18,6 +19,13 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
let i=0;
while(!(birthdays[i].includes("July"))){

i++;
}
return birthdays[i];

}

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

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

return (sumEven);

}

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.
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-1;i++){
console.log ( WRITERS[i]+" is "+AGES[i]+" years old")
}

/*
The output should look something like this:
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 tubeStation of tubeStations){
console.log (tubeStation)
}


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

function getTemperatureReport(cities) {
// TODO
let arr=[];
for (let citi of cities) {
arr.push(`The temperature in ${citi} is ${temperatureService(citi)} degrees`);

}
return arr;
}


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

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

/*
Expand All @@ -15,6 +22,16 @@ function potentialHeadlines(allArticleTitles) {
*/
function titleWithFewestWords(allArticleTitles) {
// TODO

let arr =[];
myMin= 0;

for (let article of allArticleTitles){
arr.push (article.split(' ').length);
}

myMin= Math.min(...arr);
return allArticleTitles[arr.indexOf(myMin)];
}

/*
Expand All @@ -24,6 +41,18 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let arr=[]
for (let article of allArticleTitles){
for (let char of article){
if (char>="0" && char<="9"){
arr.push (article);
break;
}

}
}
return arr

}

/*
Expand All @@ -32,6 +61,12 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let sum=0;
for (let article of allArticleTitles){
sum+=article.length;
}
return Math.round(sum/allArticleTitles.length)

}


Expand Down
34 changes: 30 additions & 4 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ 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
function getAveragePrices(closingPricesForAllStocks) {
// TODOfunction

let averageArr=[];
for (let closingPricesForStock of closingPricesForAllStocks){
let sum =0;
for (let item of closingPricesForStock){
sum=sum+item;
}
averageArr.push(Number((sum/closingPricesForStock.length).toFixed(2)));

}
return averageArr;

}

/*
Expand All @@ -48,10 +60,17 @@ 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
// TODO
let arr=[];
for (let closingPricesForStock of closingPricesForAllStocks){
arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2)));

}
return arr;

}

/*
/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
- Takes 2 parameters:
Expand All @@ -63,8 +82,15 @@ function getPriceChanges(closingPricesForAllStocks) {
The stock ticker should be capitalised.
The price should be shown with exactly 2 decimal places.
*/

function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let arr=[];
for (let i=0;i<closingPricesForAllStocks.length;i++){
arr.push (`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max(...closingPricesForAllStocks[i])).toFixed(2)}`)
}
return arr;

}


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 i=1;
let multy=1;
while(i<= input){
multy*=i;
i++;
}
return multy;
}

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