Cont'd Day1, completed level1 and level2 of Function exercises, still need to finish level3 before merging to master branch
parent
bc35b18aab
commit
ffc248bffc
@ -0,0 +1,75 @@
|
||||
// Declare a function fullName and it takes firstName, lastName as a parameter and it returns your full - name.
|
||||
function fullName(firstName,lastName){
|
||||
return `${firstName} ${lastName}`;
|
||||
}
|
||||
console.log(fullName('Seto', 'Kaiba'));
|
||||
|
||||
// Declare a function addNumbers and it takes two parameters and it returns sum.
|
||||
function addNumbers(num1,num2){
|
||||
return num1 + num2;
|
||||
}
|
||||
console.log(addNumbers(4,8));
|
||||
|
||||
// Area of a circle is calculated as follows: area = π x r x r. Write a function which calculates _areaOfCircle
|
||||
let _areaOfCircle = (radius)=>{
|
||||
let pi = Math.PI;
|
||||
let area = pi * radius * radius;
|
||||
return area;
|
||||
}
|
||||
console.log(_areaOfCircle(5));
|
||||
|
||||
// Temperature in oC can be converted to oF using this formula: oF = (oC x 9/5) + 32.
|
||||
// Write a function which convert oC to oF convertCelciusToFahrenheit.
|
||||
let convertCelciusToFahrenheit = (oC) => {
|
||||
let oF = (oC * (9/5)) + 32;
|
||||
return oF;
|
||||
}
|
||||
console.log(convertCelciusToFahrenheit(20) + `\u00B0F`)
|
||||
|
||||
|
||||
/*Body mass index(BMI) is calculated as follows: bmi = weight in Kg / (height x height) in m2.
|
||||
Write a function which calculates bmi. BMI is used to broadly define different weight groups in
|
||||
adults 20 years old or older.Check if a person is underweight, normal, overweight or obese based the information given below.
|
||||
|
||||
The same groups apply to both men and women.
|
||||
Underweight: BMI is less than 18.5
|
||||
Normal weight: BMI is 18.5 to 24.9
|
||||
Overweight: BMI is 25 to 29.9
|
||||
Obese: BMI is 30 or more */
|
||||
let BMI = (weight, height)=>{
|
||||
let bmi = (weight / (height * height)).toFixed(2);
|
||||
let answer = '';
|
||||
switch (true) {
|
||||
case bmi >= 30: answer = `BMI is ${bmi}: You are Obese!`;
|
||||
break;
|
||||
case bmi >=25 && bmi < 30: answer = `BMI is ${bmi}: You are Overweight!`;
|
||||
break;
|
||||
case bmi >= 18.5 && bmi < 25: answer = `BMI is ${bmi}: You are Normal weight!`;
|
||||
break;
|
||||
case bmi < 18.5: answer = `BMI is ${bmi}: You are Underweight!`;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
console.log(BMI(73, 1.77));
|
||||
|
||||
// Write a function called checkSeason, it takes a month parameter and returns the season:Autumn, Winter, Spring or Summer.
|
||||
let checkSeason = (month) => {
|
||||
let season = '';
|
||||
switch(true){
|
||||
case month == 'September' || month == 'October' || month == 'November': season = `The season is Autumn in the month of ${month}`;
|
||||
break;
|
||||
case month == 'December' || month == 'January' || month == 'February': season = `The season is Winter in the month of ${month}`;
|
||||
break;
|
||||
case month == 'March' || month == 'April' || month == 'May': season = `The season is Spring in the month of ${month}`;
|
||||
break;
|
||||
case month == 'June' || month == 'July' || month == 'August': season = `The season is Summer in the month of ${month}`;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return season;
|
||||
}
|
||||
console.log(checkSeason('May'));
|
@ -0,0 +1,155 @@
|
||||
// Quadratic equation is calculated as follows: ax2 + bx + c = 0.
|
||||
// Write a function which calculates value or values of a quadratic equation, solveQuadEquation.
|
||||
let solveQuadratic = (...args) =>{
|
||||
if(args.length === 0){
|
||||
return `{0}`;
|
||||
}
|
||||
let a = args[0], b = args[1], c = args[2];
|
||||
let discriminant = b * b - 4 * a * c;
|
||||
let answer = '', real = '', imaginary = '';
|
||||
switch (true) {
|
||||
case discriminant > 0:
|
||||
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
|
||||
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
|
||||
answer = `{${root1}, ${root2}}`
|
||||
break;
|
||||
case discriminant == 0:
|
||||
root1 = root2 = -b / (2 * a);
|
||||
answer = `{${root1}}`
|
||||
break;
|
||||
case discriminant < 0:
|
||||
real = (-b / (2 * a)).toFixed(2);
|
||||
imaginary = (Math.sqrt(-discriminant) / (2 * a)).toFixed(2);
|
||||
answer = `{${real} + ${imaginary}i, ${real} - ${imaginary}i}`
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return answer;
|
||||
}
|
||||
|
||||
console.log(solveQuadratic()) // {0}
|
||||
console.log(solveQuadratic(1, 4, 4)) // {-2}
|
||||
console.log(solveQuadratic(1, -1, -2)) // {2, -1}
|
||||
console.log(solveQuadratic(1, 7, 12)) // {-3, -4}
|
||||
console.log(solveQuadratic(1, 0, -4)) //{2, -2}
|
||||
console.log(solveQuadratic(1, -1, 0)) //{1, 0}
|
||||
|
||||
|
||||
// Declare a function name printArray. It takes array as a parameter and it prints out each value of the array.
|
||||
let items = ['Shoe', 'Shirt', 'Pants'];
|
||||
let printArray = (array) => {
|
||||
for (const element of array) {
|
||||
console.log(element);
|
||||
}
|
||||
}
|
||||
printArray(items);
|
||||
|
||||
|
||||
// Write a function name showDateTime which shows time in this format: 08/01/2020 04:08 using the Date object.
|
||||
let showDateTime = () => {
|
||||
let dateTime = new Date();
|
||||
let month = dateTime.getMonth();
|
||||
let day = dateTime.getDate();
|
||||
let year = dateTime.getFullYear()
|
||||
let time = dateTime.getHours();
|
||||
let minutes = dateTime.getMinutes()
|
||||
if(month < 10 || day < 10){
|
||||
month = `0${month+1}`;
|
||||
day = `0${day}`;
|
||||
}
|
||||
if(time == 0){
|
||||
time = 12;
|
||||
}
|
||||
if(minutes < 10){
|
||||
minutes = `0${minutes}`;
|
||||
}
|
||||
|
||||
console.log(`${month}/${day}/${year} ${time}:${minutes}`);
|
||||
|
||||
}
|
||||
showDateTime() // 08/01/2020 04:08
|
||||
|
||||
|
||||
// Declare a function name swapValues. This function swaps value of x to y.
|
||||
let swapValues = (x,y) => {
|
||||
let temp1 = x;
|
||||
let temp2 = y;
|
||||
x = temp2;
|
||||
y = temp1;
|
||||
console.log(`x => ${x}, y => ${y}`);
|
||||
}
|
||||
swapValues(3, 4) // x => 4, y=>3
|
||||
swapValues(4, 5) // x = 5, y = 4
|
||||
|
||||
|
||||
// Declare a function name reverseArray. It takes array as a parameter and it returns the reverse of the array (don't use method).
|
||||
let reverseArray = (array) => {
|
||||
return array.reverse();
|
||||
}
|
||||
console.log(reverseArray([1, 2, 3, 4, 5])) //[5, 4, 3, 2, 1]
|
||||
console.log(reverseArray(['A', 'B', 'C'])) //['C', 'B', 'A']
|
||||
|
||||
|
||||
// Declare a function name capitalizeArray. It takes array as a parameter and it returns the - capitalizedarray.
|
||||
let names = ['john', 'claire', 'billy'];
|
||||
let capitalizeArray = (array) => {
|
||||
array.forEach((element,index,arr) => {
|
||||
let capitalized = element.charAt(0).toUpperCase() + element.slice(1).toLowerCase();
|
||||
arr.splice(index,1,capitalized);
|
||||
});
|
||||
return array;
|
||||
}
|
||||
console.log(capitalizeArray(names));
|
||||
|
||||
|
||||
// Declare a function name addItem. It takes an item parameter and it returns an array after adding the item
|
||||
let itemArray = ['Milk'];
|
||||
let addItem = (item) =>{
|
||||
itemArray.push(item);
|
||||
return itemArray;
|
||||
}
|
||||
console.log(addItem('Pizza'));
|
||||
|
||||
|
||||
// Declare a function name removeItem. It takes an index parameter and it returns an array after removing an ite
|
||||
let itemRemove = ['Candy', 'Snacks', 'Vegetables'];
|
||||
let removeItem = (item) =>{
|
||||
itemRemove.splice(itemRemove.indexOf(item),1);
|
||||
return itemRemove;
|
||||
}
|
||||
console.log(removeItem('Snacks'))
|
||||
|
||||
|
||||
// Declare a function name evensAndOdds . It takes a positive integer as parameter and it counts number of evens and odds in the number.
|
||||
let evensAndOdds = (positive) => {
|
||||
let evenCount = 0;
|
||||
let oddCount = 0;
|
||||
for(let i = 0; i <= positive; i++){
|
||||
(i % 2 == 0) ? evenCount++ : oddCount++;
|
||||
}
|
||||
console.log(`The number of odds are ${oddCount}\nThe number of evens are ${evenCount}`);
|
||||
}
|
||||
evensAndOdds(100);
|
||||
// The number of odds are 50.
|
||||
// The number of evens are 51.
|
||||
|
||||
|
||||
// Write a function which takes any number of arguments and return the sum of the arguments
|
||||
let sum = (...args) =>{
|
||||
let value = args.reduce((a,b)=>{
|
||||
return a + b;
|
||||
}, 0);
|
||||
console.log(value);
|
||||
}
|
||||
sum(1, 2, 3); // -> 6
|
||||
sum(1, 2, 3, 4) // -> 10
|
||||
|
||||
|
||||
// Declare a function name userIdGenerator. When this function is called it generates seven character id. The function return the id, ex: 41XTDbE
|
||||
let userIdGenerator = ()=>{
|
||||
// toString(36) is base 36, mixture of binary to ASCII characters.
|
||||
// substring(2,9) specifies to move 2 right of starting char, and then grab the rest to the right 7 more for total of 9
|
||||
let randomId = ((Math.random() + 1).toString(36).substring(2,9))
|
||||
return randomId;
|
||||
}
|
||||
console.log(userIdGenerator());
|
@ -0,0 +1,18 @@
|
||||
//Skipped exercise 1 and 2
|
||||
|
||||
|
||||
|
||||
// Call your function shuffleArray, it takes an array as a parameter and it returns a shuffled array
|
||||
|
||||
|
||||
|
||||
// Call your function factorial, it takes a whole number as a parameter and it return a factorial of the number
|
||||
|
||||
|
||||
|
||||
// Call your function isEmpty, it takes a parameter and it checks if it is empty or not
|
||||
|
||||
|
||||
|
||||
// Write a function called average, it takes an array parameter and returns the average of the items.
|
||||
// Check if all the array items are number types. If not give return reasonable feedback.
|
@ -0,0 +1,55 @@
|
||||
// Function with unlimited number of parameters
|
||||
/* - A function declaration provides a function scoped arguments array like object.
|
||||
Any thing we passed as argument in the function can be accessed from arguments
|
||||
object inside the functions. Let us see an example*/
|
||||
|
||||
// function declaration
|
||||
function sumAllNums() {
|
||||
let sum = 0;
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
sum += arguments[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
console.info(sumAllNums(10, 20, 13, 40, 10)); // 93
|
||||
|
||||
|
||||
// Unlimited number of parameters in arrow function
|
||||
|
||||
/* - Arrow function does not have the function scoped arguments object.
|
||||
To implement a function which takes unlimited number of arguments in
|
||||
an arrow function we use spread operator followed by any parameter name.
|
||||
Any thing we passed as argument in the function can be accessed as array in
|
||||
the arrow function. Let us see an example*/
|
||||
|
||||
// Let us access the arguments object
|
||||
const addAllNums = (...args) => {
|
||||
// console.log(arguments), arguments object not found in arrow function
|
||||
// instead we use an a parameter followed by spread operator
|
||||
console.log(args);
|
||||
}
|
||||
addAllNums(1, 2, 3, 4); // [1, 2, 3, 4]
|
||||
|
||||
|
||||
// Self Invoking Functions
|
||||
// - Self invoking functions are anonymous functions which do not need to be called to return a value.
|
||||
(function (n) {
|
||||
console.log(n * n);
|
||||
})(2) // 4, but instead of just printing if we want to return and store the data, we do as shown below
|
||||
|
||||
let squaredNum = (function (n) {
|
||||
return n * n;
|
||||
})(10)
|
||||
console.log(squaredNum);
|
||||
|
||||
|
||||
// Function with default parameters
|
||||
// - Sometimes we pass default values to parameters, when we invoke the function if we do not pass an argument
|
||||
// the default value will be used. Both function declaration and arrow function can have a default value or values.
|
||||
function greetings(name = 'Peter') {
|
||||
let message = `${name}, welcome to 30 Days Of JavaScript!`
|
||||
return message
|
||||
}
|
||||
console.log(greetings());
|
||||
console.log(greetings('Asabeneh'));
|
||||
|
Loading…
Reference in new issue