Posts

coding challenge #4

Image
  // function expression const calcTip = function ( bill ) { return bill <= 300 && bill >= 50 ? 0.15 * ( bill ) : 0.2 * ( bill ); // This reads as if bill is less than equal to 300 And And if bill is greater than equal to 50 , then 0.15* bill , else 0.2* bill. } const bills = [ 22 , 295 , 176 , 440 , 37 , 105 , 10 , 1100 , 86 , 52 ]; const tips = []; const totals = []; for ( let i = 0 ; i < bills.length; i ++ ) { tips. push ( calcTip (bills[i])); totals. push (bills[i] + tips[i]); } console. log (tips); console. log (totals); //BONUS // Function Expression const calcAverage = function ( arr ) { let sum = 0 ; for ( let i = 0 ; i < arr .length; i ++ ) { sum += arr [i]; } // This is the first way // const average = sum / arr.length; // console.log(average); // This way is much more logical as we are returning return sum / arr .length; } // calcAverage(totals); /...

coding challenge #3

Image
  const markObj = { fullName: 'Mark Miller' , mass: 78 , height: 1.69 , calcBMI : function () { this .bmi = this .mass / this .height ** 2 ; return this .bmi; } } const johnObj = { fullName: 'John Smith' , mass: 92 , height: 1.95 , calcBMI : function () { this .bmi = this .mass / this .height ** 2 ; return this .bmi; } } markObj. calcBMI (); // invoking the calcBMI method inside markObj johnObj. calcBMI (); // // invoking the calcBMI method inside johnObj console. log (markObj.bmi, johnObj.bmi); //now since bmi is a property of both the object we can access it using dot or bracket notation. if (markObj.bmi > johnObj.bmi) { console. log ( ` ${ markObj.fullName } 's BMI ( ${ markObj.bmi } ) is higher than ${ johnObj.fullName } 's BMI ( ${ johnObj.bmi } )` ) } else if (johnObj.bmi > markObj.bmi) { console. log ( ` ${ johnObj.fullName } 's BM...

coding challenge #2

Image
  // function expression const calcTip = function ( bill ) { return bill <= 300 && bill >= 50 ? 0.15 * ( bill ) : 0.2 * ( bill ); // This reads as if bill is less than equal to 300 And And if bill is greater than equal to 50 , then 0.15* bill , else 0.2* bill. } //console.log(calcTip(500)); // 100 const bills = [ 125 , 555 , 44 ] const tips = [ calcTip (bills[ 0 ]), calcTip (bills[ 1 ]), calcTip (bills[ 2 ])]; // Here we are invoking the function calcTip in an array format and the returned values from the function is being stored in this tips variable that we have created. console. log (tips) //BONUS const totals = [bills[ 0 ] + tips[ 0 ], bills[ 1 ] + tips[ 1 ], bills[ 2 ] + tips[ 2 ]]; //Here we are adding the bill and the tip in each case. console. log (totals);

Introduction to Arrays

Image
  1) Lets' now talk about our first data structure. And that's gonna be Arrays. 2) An Array is like a big container into which we can throw variables and then later reference them. 3)  The two most important data structures, at-least in javascript are arrays and objects. 4) Array Literal syntax:- 5) A different way of creating an Array is by using Array function and new keyword:- 6) Accessing individual Array elements:- we can access individual array elements by referencing them by index. The first index is 0. 7) Array.length property:- Array.length property will give us the actual length of the Array. It will not respect the indexing rule and will give us the actual number of elements present in the Array. **ignore this       console.log(friends) part in this code. Since the number of elements in the friends Array is 3, friends.length will also give us the output: 3. 8) Accessing the last element of an Array by Array[Array.length-1] method. ** ignore the c...

Reviewing functions and some keyboard shortcuts.

Image
The return statement immediately exits or immediately returns the function and in both the if/else condition in the above code, console.log statement is after the return keyword, therefore they simply gets ignored and have no chance to get executed . If we want them to execute they should be written before the return keyword like we will do in our next example code. In vs code we can simply move up or down our code by pressing : option and up arrow keys together on mac alt and up arrow keys together on windows also command and d keys pressed together highlights the same line in the entire code. In this code the console.log statement is before the return keyword , so the statements gets printed. In vs code if we press shift + () keys together over a selected code, the entire code is wrapped around paranthesis.

functions calling other functions

Image
 

All about Arrow functions

Image
  1) Arrow functions when there is one parameter and one line of code. 2) Arrow function when there is one parameter and more than one line of code. 3) Arrow function when there is more than one parameter and more than one line of code.