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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// {
// // Use IntelliSense to learn about possible attributes.
// // Hover to view descriptions of existing attributes.
// // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
// "version": "0.2.0",
// "configurations": [
// {
// "type": "node",
// "request": "launch",
// "name": "Launch Program",
// "skipFiles": [
// "<node_internals>/**"
// ],
// "program": "${workspaceFolder}/1-exercises/B-while-loop/exercise.js"
// }
// ]
// }
8 changes: 5 additions & 3 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// Example 1
let a;
console.log(a);

//In this example, we see undefined because our variable doesn't have any value.

// Example 2
function sayHello() {
let message = "Hello";
// In this example, We don't have return, so I would write return in this line. => return message;
}

let hello = sayHello();
console.log(hello);

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

sayHelloToUser();
sayHelloToUser();//In this example,We do not have an argument because this is a function that has parameters, so outside the function it should have a value.
//I mean => sayHelloToUser(" we should write some string because od user")for example : sayHelloToUser("dear volunteer");


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//We have an array with the indexes o, 1, and 2 here, so there isn't index 3. we can Write arr[0] or arr[1] or arr [2]
13 changes: 12 additions & 1 deletion 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
*/

function evenNumbers(n) {
// TODO
let num= 0 ;
let arr =[]
while(n > 0){
arr.push(num)
num += 2
n--
}

@mcarballopacheco mcarballopacheco Dec 20, 2022

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This function works. Another way to write it which for me it's a bit more readable would be like this:

function evenNumbers(n) {
let num= 0 ;
let arr =[]
while(num < n){
arr.push(2*num)
num ++
}

I find it more natural that the comparison is between n and num but both of them are correct.


console.log(arr)
}

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



13 changes: 12 additions & 1 deletion 1-exercises/C-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ const BIRTHDAYS = [
"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"

//includes

11 changes: 9 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
*/

function evenNumbersSum(n) {
// TODO
let result = 0;
let i = 0 * 2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This could be i =0

do{
result += i * 2;
i++;
}while(i < n)

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 work here Mari!


return result
}

console.log(evenNumbersSum(3)); // should output 6
console.log(evenNumbersSum(0)); // should output 0
console.log(evenNumbersSum(10)); // should output 90
console.log(evenNumbersSum(10)); // should output 90
16 changes: 12 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,17 @@


// 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++;
// 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.
15 changes: 15 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ const AGES = [

// TODO - Write for loop code here

for(i = 0;i <= WRITERS.length;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.

The comparison should be i < WRITERS.length, because array start counting from 0. With the current code, you will get an undefined for the last line.

const result=`${WRITERS[i]} is ${AGES[i]} years old `
console.log(result)
}


//I did with forEach as well with same result

// WRITERS.forEach((num1, index) => {
// const num2 = AGES[index];
// const result = `${num1} is ${num2} years old`
// console.log(result);
// });


/*
The output should look something like this:

Expand Down
8 changes: 7 additions & 1 deletion 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(const element of tubeStations ){
console.log(element)
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.

let str = "codeyourfuture";
for(const element of str){
console.log(element.toLocaleUpperCase())
}
8 changes: 6 additions & 2 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
*/

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


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

function temperatureService(city) {
Expand Down
7 changes: 6 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,12 @@ function generateRandomNumber() {
}

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

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


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

/*
Expand All @@ -23,15 +34,29 @@ 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 arr=[]
for (let article of allArticleTitles){
for (let char of article){
if (char>="0" && char<="9"){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

cool!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

arr.push (article);
break;
}

}
}
return arr
}

/*
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 article of allArticleTitles){
sum+=article.length;
}
return Math.round(sum/allArticleTitles.length)
}


Expand Down
25 changes: 22 additions & 3 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
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 @@ -48,7 +58,12 @@ 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 arr=[];
for (let closingPricesForStock of closingPricesForAllStocks){
arr.push(Number((closingPricesForStock[closingPricesForStock.length-1]-closingPricesForStock[0]).toFixed(2)));

}
return arr;
}

/*
Expand All @@ -64,7 +79,11 @@ function getPriceChanges(closingPricesForAllStocks) {
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
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 sum = 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.

Very small detail, but this is not a sum but a product. The function is correct though.

for (i = 1; i <= input; i++){
sum *= i
}
return sum
}

/* ======= 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
const result = books.reduce((acc, cur) => {
const groupByGenre = cur.genre;
if (!acc[groupByGenre]) {
acc[groupByGenre] = [];
}
acc[groupByGenre].push(cur);
return acc;
}, {});
const genreName = Object.keys(result);
const arr = [];
for (let i = 0; i < genreName.length; i++) {
arr.push(
result[genreName[i]].sort((a, b) => b.rating - a.rating)[0].title
);
}
return arr;
}


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

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

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