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 @@ -11,13 +11,13 @@

// Example 1
let a;
console.log(a);
console.log(a); // value of a is not declared so the output is undefined


// Example 2
function sayHello() {
let message = "Hello";
}
} // function returns anything so the output is undefined

let hello = sayHello();
console.log(hello);
Expand All @@ -28,9 +28,9 @@ function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); // The Parameter user is not declared so the output is Hello undefined


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]); // the value of arr[3] is not declared so the output is undefined

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice explanations

14 changes: 10 additions & 4 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
*/

function evenNumbers(n) {
// TODO
let i=0
let evenNumbers=[];
while(i<n){
evenNumbers.push(2*i);
i++
}
return evenNumbers;
}

evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18
console.log(evenNumbers(3)); // should output 0,2,4
console.log(evenNumbers(0)); // should output nothing
console.log(evenNumbers(10)) // should output 0,2,4,6,8,10,12,14,16,18
9 changes: 7 additions & 2 deletions 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ const BIRTHDAYS = [
"September 28th",
"November 15th"
];

function findFirstJulyBDay(birthdays) {
// TODO
let i=0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi, Piramanathan, I check your code and it is fine, it's functional. good job

while(i<birthdays.length){
if(birthdays[i].includes("July")){
return birthdays[i];
}
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.

Good

}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
8 changes: 7 additions & 1 deletion 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/

function evenNumbersSum(n) {
// TODO
let i=0

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great job!!!

let sumOfEven=0;
do{
sumOfEven=sumOfEven+i*2;
i++;
}while(i<n)
return sumOfEven;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice codes. You can also do it this way
let sum= 0;
let i = 0;
do {
sum += i * 2;
i = i + 1;
} while (i < n);
return sum;

}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
6 changes: 2 additions & 4 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {
console.log(String.fromCharCode(97 + i));
i++;
for(let i = 0;i < 26; i++){
console.log(String.fromCharCode(97 + i));
}
// The output shouldn't change.
4 changes: 3 additions & 1 deletion 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const AGES = [
49
];

// 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
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 char of tubeStations) {
console.log(char)
}


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

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


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() {
// TODO - implement using a do-while loop
let randomNumber;
do{
randomNumber=generateRandomNumber();
} while (randomNumber<=50)
return randomNumber;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
30 changes: 26 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let articleTitle=[];
for(let i=0; i<allArticleTitles.length; i++){
if(allArticleTitles[i].length<=65){
articleTitle.push(allArticleTitles[i])
}
}
return articleTitle;
}

/*
Expand All @@ -14,7 +20,13 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let fewWordsTitle = allArticleTitles.map((title) => {
return title.trim().split(" ").length;
});
let min = Math.min(...fewWordsTitle);
let index = fewWordsTitle.indexOf(min);
return allArticleTitles[index];

}

/*
Expand All @@ -23,15 +35,25 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let titleWithNumbers=[];
for(let i=0; i<allArticleTitles.length; i++){
if (/\d/.test(allArticleTitles[i])) {
titleWithNumbers.push(allArticleTitles[i]);
}
}
return titleWithNumbers;
}

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


Expand Down
22 changes: 19 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let average=[];
for(let i=0; i<closingPricesForAllStocks.length; i++){
let sum=0;
for(let j=0; j<closingPricesForAllStocks[i].length; j++){
sum=sum+closingPricesForAllStocks[i][j];
}
average.push(parseFloat((sum/closingPricesForAllStocks[i].length).toFixed(2)));
}
return average;
}

/*
Expand All @@ -48,7 +56,11 @@ 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
let priceChange=[];
for(let i=0; i<closingPricesForAllStocks.length; i++){
priceChange.push(parseFloat((closingPricesForAllStocks[i][4]-closingPricesForAllStocks[i][0]).toFixed(2)));
}
return priceChange;
}

/*
Expand All @@ -64,7 +76,11 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let highestPrice=[];
for(let i=0; i<closingPricesForAllStocks.length; i++){
highestPrice.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${(Math.max.apply(Math,closingPricesForAllStocks[i])).toFixed(2)}`)
}
return highestPrice;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job Vanipriya



Expand Down
6 changes: 5 additions & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
*/

function factorial(input) {
// TODO
let fact=1;
for(let i=0; i<input; i++){
fact=fact*(i+1);
}
return fact;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
17 changes: 16 additions & 1 deletion 3-extra/2-array-of-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
*/

function getHighestRatedInEachGenre(books) {
// TODO
let listOfGenre = ["non-fiction", "children", "cooking"];
let bookTitles = [];
for (const genre of listOfGenre) {
let maxRating = 0;
let index = 0;
for (let i = 0; i < books.length; i++) {
if (books[i].genre === genre) {
if (books[i].rating > maxRating) {
maxRating = books[i].rating;
index = i;
}
}
}
bookTitles.push(books[index].title);
}
return bookTitles;
}


Expand Down
8 changes: 7 additions & 1 deletion 3-extra/3-fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
*/

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



/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the first 10 numbers in the Fibonacci Sequence", () => {
expect(generateFibonacciSequence(10)).toEqual(
Expand Down