Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Closed
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
4 changes: 3 additions & 1 deletion exercises/B-hello-world/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
console.log("Hello world");
console.log("Hello World. I just started learning JavaScript!");
console.log("I hope I will master JS someday");
console.log(2);
4 changes: 3 additions & 1 deletion exercises/C-variables/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `greeting`

let greeting= "Hello world";
console.log(greeting);
console.log(greeting);
console.log(greeting);
4 changes: 3 additions & 1 deletion exercises/D-strings/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

let message="This is a string"
let messageType= typeof message;
console.log(message);
console.log(messageType);
4 changes: 3 additions & 1 deletion exercises/E-strings-concatenation/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

let name= "Daniel";
let greetingMessage= "Hello, my name is "
let message= greetingMessage + name;
console.log(message);
5 changes: 3 additions & 2 deletions exercises/F-strings-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Start by creating a variable `message`

console.log(message);
let name= "Daniel";
let nameLength= name.length;
console.log("My name is Daniel and my name is " + nameLength +" characters long");
5 changes: 3 additions & 2 deletions exercises/F-strings-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const name = " Daniel ";

console.log(message);
let nameTrimmed = name.trim();
let nameLength= name.length;
console.log("My name is "+nameTrimmed+" and my name is " + nameLength +" characters long");
4 changes: 4 additions & 0 deletions exercises/G-numbers/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
// Start by creating a variables `numberOfStudents` and `numberOfMentors`
let numberOfStudents=15;
let numberOfMentors=8;
let total= numberOfMentors + numberOfStudents;
console.log("Total numnber of students and mentors: "+ total);
6 changes: 6 additions & 0 deletions exercises/I-floats/exercise.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
var numberOfStudents = 15;
var numberOfMentors = 8;
let total = numberOfMentors + numberOfStudents;
let poStudents = Math.round((15 / total) * 100);
let poMentors = Math.round((8 / total) * 100);
console.log(poStudents);
console.log(poMentors);

5 changes: 5 additions & 0 deletions exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
function halve(number) {
// complete the function here
return number/2;
}

var result = halve(12);
var result2 = halve(24);
var result3 = halve(48);

console.log(result);
console.log(result2);
console.log(result3);
1 change: 1 addition & 0 deletions exercises/J-functions/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function triple(number) {
// complete function here
return number*3;
}

var result = triple(12);
Expand Down
3 changes: 2 additions & 1 deletion exercises/K-functions-parameters/exercise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Complete the function so that it takes input parameters
function multiply() {
function multiply(a, b) {
// Calculate the result of the function and return it
return a*b;
}

// Assign the result of calling the function the variable `result`
Expand Down
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Declare your function first

function divide (a,b){
return a/b;
}
var result = divide(3, 4);

console.log(result);
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Write your function here

function createGreeting (name){
return "Hello, my name is " + name;
}
var greeting = createGreeting("Daniel");

console.log(greeting);
6 changes: 4 additions & 2 deletions exercises/K-functions-parameters/exercise4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Declare your function first

function add (a,b){
return a+b;
}
// Call the function and assign to a variable `sum`

let sum = add(13,124);
console.log(sum);
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Declare your function here

function createLongGreeting(name, age){
return "Hello, my name is " + name + " and I'm 30 " + age + " years old";
}
const greeting = createLongGreeting("Daniel", 30);

console.log(greeting);
9 changes: 9 additions & 0 deletions exercises/L-functions-nested/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";

function capitalLetters (name) {
return "HELLO " + name.toUpperCase();
}
console.log(capitalLetters(mentor1));
console.log(capitalLetters(mentor2));
console.log(capitalLetters(mentor3));
console.log(capitalLetters(mentor4));
console.log(capitalLetters(mentor5));
11 changes: 9 additions & 2 deletions extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}
function convertToUSD(price) {
let total = price *1.4;
return total;
}

/*
CURRENCY CONVERSION
Expand All @@ -15,7 +18,11 @@ function convertToUSD() {}
They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL.
*/

function convertToBRL() {}
function convertToBRL(price) {
let priceToBeConverted = 0.99*price;
let convertedPrice = priceToBeConverted * 5.7;
return parseFloat(convertedPrice.toFixed(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.

Nice job with the parseFloat(). I just got to know about it from reviewing your codes... Great job Farzad!

}

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
21 changes: 12 additions & 9 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,29 @@
the final result to the variable goodCode
*/

function add() {

function add(a, b) {
return a+b;
}

function multiply() {

function multiply(a, b) {
return a*b;
}

function format() {

function format(a) {
let string = "£" + a;
return string;
}

const startingValue = 2;

// Why can this code be seen as bad practice? Comment your answer.
let badCode =
let badCode = format(add(10,startingValue)*2);
// Comment : It is complicated to understand and perfum again

/* BETTER PRACTICE */

let goodCode =
let sum = add(10, startingValue);
let totalNumber = sum * 2;
let goodCode = format(totalNumber);

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
11 changes: 6 additions & 5 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// There are syntax errors in this code - can you fix it to pass the tests?

function addNumbers(a b c) {
function addNumbers(a, b, c) {
return a + b + c;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correct, as you can see, while setting up parameters in a function, you need to separate them with commas.


function introduceMe(name, age)
return "Hello, my name is " + name "and I am " age + "years old";
function introduceMe(name, age){
return "Hello, my name is " + name +" and I am " + age + " years old";
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Correct, when writing JavaScript functions, you need to use curly brackets to define the inside of the function, otherwise JavaScript won't understand that part and throw an error. In addition, as you did above, you need to put the + icon between strings and variables, so that you can concatenate them.


function getTotal(a, b) {
total = a ++ b;
total = a + b;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Don't forget the let keyword while defining a variable!


return "The total is total";
return "The total is " + total;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Another nice way of concatenating a String with a variable. In addition, you can also use String interpolation to do this.


/*
Expand Down
8 changes: 4 additions & 4 deletions mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// The syntax for this function is valid but it has an error, find it and fix it.

function trimWord(word) {
return wordtrim();
return word.trim();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's right. There is no function called wordtrim() in JavaScript. We use trim() instead.


function getStringLength(word) {
return "word".length();
return word.length;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That is right, never put quotation marks around a variable, since JavaScript will identify it as a String instead of a variable.


function multiply(a, b, c) {
a * b * c;
return;
let total = a * b * c;
return total;
}

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 way of using the let keyword to define a variable.


/*
Expand Down
11 changes: 8 additions & 3 deletions mandatory/3-function-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
function getRandomNumber() {
return Math.random() * 10;
}
// Comment : math.random gives us a random number between 0 and 1. then we times it by 10, we will have a random number between 0 and 10.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

From my findings on Google, I could be wrong though, the getRandomNumber function will generate only floating numbers between 1 - 10

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

math.random() gives you a number between 0 and 1 , if you times it by 10 , then gives you 0 to 10

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 Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. "

As you can see from the description taken from MDN Docs above, math.random() will randomly generate a number between 0 and 1. After that, it will multiply a number of your choice.


// Add comments to explain what this function does. You're meant to use Google!
function combine2Words(word1, word2) {
return word1.concat(word2);
}
// Comment : This functions takes 2 parametrs and append word2 to word 1 (combine) and return a new string.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's correct, this function will concatenate 2 given Strings.


function concatenate(firstWord, secondWord, thirdWord) {
// Write the body of this function to concatenate three words together.
// Look at the test case below to understand what this function is expected to return.
let wordCombination = firstWord.concat(secondWord);
let final = wordCombination.concat(thirdWord);
return final;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's correct.


/*
Expand All @@ -25,13 +30,13 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 3-f
*/

test("concatenate example #1", () => {
expect(concatenate("code", "your", "future")).toEqual("code your future");
expect(concatenate("code", " your ", "future")).toEqual("code your future");
});

test("concatenate example #2", () => {
expect(concatenate("I", "like", "pizza")).toEqual("I like pizza");
expect(concatenate("I", " like ", "pizza")).toEqual("I like pizza");
});

test("concatenate doesn't only accept strings", () => {
expect(concatenate("I", "am", 13)).toEqual("I am 13");
expect(concatenate("I", " am ", 13)).toEqual("I am 13");
});
10 changes: 8 additions & 2 deletions mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Sales tax is 20% of the price of the product.
*/

function calculateSalesTax() {}
function calculateSalesTax(productPrice) {
let total = productPrice + productPrice * 0.2;
return total;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's correct, I also like the way you calculated the total value in just 1 line, nice job.


/*
CURRENCY FORMATTING
Expand All @@ -17,7 +20,10 @@ function calculateSalesTax() {}
Remember that the prices must include the sales tax (hint: you already wrote a function for this!)
*/

function addTaxAndFormatCurrency() {}
function addTaxAndFormatCurrency(productPrice) {
let total = calculateSalesTax(productPrice);
return "£" + total.toFixed(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 is also correct. In addition to this way, you can also use String interpolation to concatenate a String to a variable.


/*
===================================================

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Had a great time going through your codes. it gave me alternate perspectives to solve a problem!

Thumbs up Farzad!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks a lot Chioma

Expand Down