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
Expand Up @@ -18,6 +18,14 @@ 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
7 changes: 5 additions & 2 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

// 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 @@ -27,6 +27,10 @@ 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
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 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
33 changes: 33 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,17 @@ 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 +60,11 @@ 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
25 changes: 25 additions & 0 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
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 @@ -49,6 +60,14 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let arr=[];
for (let closingPricesForStock of closingPricesForAllStocks){
// arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0])).toFixed(2));
arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2)));

}
return arr;

}

/*
Expand All @@ -65,6 +84,12 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
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
3 changes: 3 additions & 0 deletions 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

function getHighestRatedInEachGenre(books) {
// TODO
for(let book of books){

}
}


Expand Down
6 changes: 6 additions & 0 deletions 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

function generateFibonacciSequence(n) {
// TODO
let arr=[0,1];
for (let i=0;i<n-2;i++){
arr.push (arr[i]+arr[i+1]);
}

return arr;
}

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