Posts

Showing posts from May, 2021

Javascript Practice 69: JS Hero - gcd

  Question:  Write a function gcd that takes two natural numbers and calculates their gcd. Example: gcd (6, 15) should return 3. Answer:  function gcd (a,b) { if (b == 0) { return a; } else {  let remainder = a % b;  return gcd (b, remainder); } } --- we need to understand 'euclid's alogrithm for gcd' --- if we subtract a smaller number from a larger (we reduce a larger number), GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we end up with GCD. --- now instead of subtraction, if we divide the smaller number, the algorithm stops when we find remainder 0. --- If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop --- If B = 0 then GCD(A,B)=A, since the GCD(A,0)=A, and we can stop. > we declare a function 'gcd' > it has 2 parameters: 'a' and 'b' > if b is equal to 0, then a shall be the gcd (if you understand euclids alogorithm) > if b is not equal to 0, then we calculate the remainder > we create a v...

Javascript Practice 68: JS Hero - do...while loop

  Question:  Write a function lcm that takes two natural numbers and calculates their least common multiple (lcm). The lcm of two natural numbers a und b is the smallest natural number that is divisible by a and b. Example: lcm(4, 6) should return 12. Answer:  function lcm(a, b) {   let theLCM = 0;   let remainderA;   let remainderB;   do {     theLCM++;     remainderA = theLCM % a;     remainderB = theLCM % b;   } while (remainderA !== 0 || remainderB !== 0)   return theLCM; } > we declare a function lcm > it has 2 parameters: 'a' and 'b' > we declare a variable 'theLCM' & initialize it with a value of 0;  > we declare 2 empty variables: 'remainderA' & 'remainderB'  > we open a do while loop  > do/while loop is a variant of the while loop > this loop will execute the code block once, before checking if the condition is true > then it will repeat the loop a...

Javascript Practice 67: JS Hero - while loop

  Question:  Write a function spaces that takes a natural number n and returns a string of n spaces. Example: spaces(1) should return ' '. Answer:  function spaces (num) {   let mySpaces = '';   while (num-- > 0)     mySpaces += ' ';    return mySpaces; }; > declare a function spaces > it has 1 parameter 'num' > declare a variable mySpaces > initialize it with an empty string > we use a while loop as we need to repeat an action multiple times > in this case we need to add blank spaces to a string  > our '''empty spaces string''' will be stored in the variable mySpaces > blank spaces will be equal to the 'num' parameter, which is fed when the function 'spaces' is called > while loops have: a condition and a code (loop body)  > our condition is to ensure that the code should execute as long as the num is greater than 0 > our condition is (num-- > 0) > our code is: mySpaces +=...

Javascript Practice 66: JS Hero - Loops and arrays

  Question:  Write a function mean that accepts an array filled with numbers and returns the arithmetic mean of those numbers. Example: mean([1, 2, 3]) should return (1+2+3)/3 = 2. Answer:  function mean (arr) {  let sum = 0;  for (let i = 0; i < arr.length; i++) {  sum = sum + arr[i];  }  return sum / arr.length } > we declare a function mean > it has one parameter 'arr' > we declare a variable sum and initialize it with a value of 0 > we first use the for loop to add up all the elements of an array  > first expression: let i = 0 > second expression or condition: i < arr.length > third expression: i++ (i++ is identical to i = i + 1) > loop code: sum = sum + arr[i] > to understand this for loop, we call the function mean with an arr of 3 elements > [1, 2, 3]  > in the start expression ' i ' is initialized with a value of 0 > this means that 0 < 3; hence the loop code is executed >...

Javascript Practice 65: JS Hero - Factorial

  Question:  Write a function factorial that calculates the factorial of a positive integer. Example: factorial(3) should return 6. Answer:  function factorial (num) { let product = 1;  for (let i = 1; i <= num; i++) { product = product * i }  return product } > we declare a function factorial > it has one parameter 'num'  > we first declare a variable 'product' with a value of 1 > we then open the 'for' loop  >> let us assume that we call the factorial function with number 3 as a 'num' parameter >> first expression: let i = 1 >> second expression or condition: i <= num >> third expression: i++ (i++ is identical to i = i + 1) >> loop code: product = product * i >> in the start expression ' i ' is initialized with a value of 1  >> the loop condition is i <= num (3); >> if ' i ' is less than or equal to the num then the loop code is executed (1 <= 3) >> the produc...

Javascript Practice 64: JS Hero - for loop

  Question:  Write a function addTo that accepts a number as a parameter and adds all natural numbers smaller or equal than the parameter. The result is to be returned. Example: addTo(3) should return 1+2+3 = 6. Answer:  function addTo (num) { let sum = 0; for (let i = 1; i <= num; i++) {   sum = sum + i; } return sum } > we declare a function addTo > it has one parameter 'num'  > we first declare a variable 'sum' with a value of 0 > we then open the 'for' loop  > the 'for' statement creates a loop  > this loop consists of 3 optional expressions > these 3 expressions are enclosed in parenthesis > they are separated by semi colons > like this (let i = 0; i < 10; i++)  > the 'for' loop is usually followed by a statement / block statement which is to be executed in the loop; also called loop code >  the first expression is the start expression  > it is executed once at the beginning of the loop...

Javascript Practice 63: JS Hero - null

  Question:  Write a function cutComment that takes one line of JavaScript and returns a possible line comment trimmed. If the line contains no line comment, null should be returned. For simplicity, we assume that the code does not contain the comment characters within a string. Example: cutCommt ('let foo; // bar') should return 'bar'. Answer:  function cutComment (line) { let a = line.indexOf('//');  if (a === -1) {  return null;  } else { return line.slice(a+2).trim();  }};  > we declare a function cutComment > it has one parameter 'line' > we need to check if the function cutComment is called with a string that has a comment line  > if it has a comment line then we need to remove it  > and return all the elements of the string after the comment line characters ' // '  >>>> so our solution will begin with declaring a variable ' a '  >> it will be initialized with a value returned from the ind...

Javascript Practice 62: JS Hero - undefined

  Question:  Write a function hello having one parameter and returning 'Hello <parameter>!'. If hello is called without an argument, 'Hello world!' should be returned. Example: hello('Nala') should return 'Hello Nala!'. Answer:  function hello (p) { if (p === undefined) {          return `Hello world!`; } else {          return `Hello ${p}!`} }; or function hello (param) { if (param) { return 'Hello ' + param + '!' ; } else {  return 'Hello world!'} }; > we declare a function 'hello'  > it has one parameter - 'p' > we open if / else statements to check  >> if the function 'hello' is called with a parameter 'p' >> or it is called without a parameter > if it is called with a parameter then we have to return a string  > this string will append the parameter 'p' to 'Hello' and return it > in case the function is called without a parameter > then we ...

Javascript Practice 61: JS Hero - Comments

  Question:  Write a function median that takes an array of ascending numbers and returns the median of that numbers. Example: median([1, 2, 10]) should return 2 and median([1, 2, 10, 100]) should return 6. Answer:  function median (arr) { arr.sort(function(a, b){return a-b}); let isVar = arr.length / 2; if (isVar % 1 == 0) { return (arr[isVar  - 1] + arr[isVar]) / 2 } else { return arr[Math.floor(isVar)]; } }; > declare a function median  > it has 1 parameter 'arr'  > the median function will only take arrays as arguments when called  >>>>> we first need to ensure that the array is in ascending order > we shall need to sort the array before any other operation is done on the array > we sort the array using the sort method > as the name suggests, this method, sorts the elements of an array and returns the sorted array > the default sort order is ascending > it first converts elements into strings > it then...

Javascript Practice 60: JS Hero - Array of arrays

  Question:  Write a function flat that flattens a two-dimensional array with 3 entries. let row1 = [4, 9, 2]; let row2 = [3, 5, 7]; let row3 = [8, 1, 6]; let loshu = [row1, row2, row3]; Example: flat(loshu) should return [4, 9, 2, 3, 5, 7, 8, 1, 6]. Thereby loshu is the magic square from the example above. Answer:  function flat (array) { let entryA = array[0];  let entryB = array[1];  let entryC = array[2];  let finalD = entryA.concat(entryB, entryC); return finalD; }; > declare a function flat > it has 1 parameter array > we know that we need to flatten a two dimensional array which has 3 entries / arrays > an array that contains an array is called a two dimensional array > we declare 3 variables that access the three entries / arrays within the 2D array > we then concat all three arrays using the concat () method and store the output in another variable finalD > we return the finalD variable to get our single flattened array ...

Javascript Practice 59: JS Hero - Array: join()

  Question:  Write a function list that takes an array of words and returns a string by concatenating the words in the array, separated by commas and - the last word - by an 'and'. An empty array should return an empty string. Example: list(['Huey', 'Dewey', 'Louie']) should return 'Huey, Dewey and Louie'. Answer:  function list (array) { if (array.length === 0) { return ''; } else if (array.length === 1) { return array[0]; } else {  let wordsExLast = array.slice(0, array.length - 1);  let lastWord = array[array.length-1];  return wordsExLast.join(', ') + ' and ' + lastWord; };  }; > we create a function declaration 'list' > it has a parameter array > we then open if / else if / else statements > the first condition is to check if the array length is 0 > if it is 0 then the function should return an array which is empty > else if the length of the array has only one word then the function should r...

Javascript Practice 58: JS Hero - Array: slice()

  Question:  Write a function halve that copies the first half of an array. With an odd number of array elements, the middle element should belong to the first half. Example: halve([1, 2, 3, 4]) should return [1, 2]. Answer:  function halve (array) { let half = Math.ceil(array.length / 2);  return array.slice(0, half); }; > we declare a variable halve > it has one parameter - array > this function will basically copy the first half of an array and return those elements > in order to divide an array in half, we simply divide the length of an array by 2 > now the array length could be even or odd > if its even then we get a whole number  > but if its odd then we get a number with a decimal  > also if the array is odd then we need to ensure that the middle element is added to the first half > so in order to solve this we basically first declare a variable half in the function  > we initialize it with a value array.length / 2 ...

Javascript Practice 57: JS Hero - Array: indexOf()

  Question:  Write a function concatUp that concatenate two arrays. The longer array should be appended to the shorter array. If both arrays are equally long, the second array should be appended to the first array. Example: concatUp([1, 2], [3]) should return [3, 1, 2] and concatUp([5, 7], [6, 8]) should return [5, 7, 6, 8]. Answer:  function concatUp (a1, a2) { if (a2.length >= a1.length) { return a1.concat(a2);  } else { return a2.concat(a1);  } }; > we declare a function concatUp  > it has 2 parameters a1, a2 > this function will concatenate two arrays - a1 & a2 > the two arrays can be of equal length > or a1 can be longer than a2 > or a2 can be longer than a1 > if a2 is longer than a1, then we append (meaning add to the end) a2 to a1 > like this: a1.concat(a2) > if a1 length is equal to a2 length then we again append a2 to a1 > so we have 2 conditions for same output > else if a1 is longer than a2 then we append a...

Javascript Practice 56: JS Hero - Array: indexOf()

  Question:  Write a function add that adds an element to the end of an array. However, the element should only be added if it is not already in the array. Example: add([1, 2], 3) should return [1, 2, 3] and add([1, 2], 2) should return [1, 2]. Answer:  function add (arr, e) { if (arr.indexOf(e) === -1) { arr.push(e); return arr; } else { return arr;  } };  > declare a function add > it has 2 parameters arr & e > we need to first check if 'e' is already present in the array - arr > if it is present then it will not be added to the array - arr  > if it is not present then it will be added to the array - arr > so after we declare the function, we will open an if / else statement > the condition will be to check if the indexOf method on the array contains the 'e' element > if it returns the -1 or is equal to -1, then we shall add that element using the push method > or else we shall just return the original array 

Javascript Practice 55: JS Hero - Array: shift() and push()

  Question:  Write a function rotate that rotates the elements of an array. All elements should be moved one position to the left. The 0th element should be placed at the end of the array. The rotated array should be returned. Example: rotate(['a', 'b', 'c']) should return ['b', 'c', 'a']. Answer:  function rotate (array) { let firstEl = array.shift(); array.push(firstEl); return (array);  } > we declare a function rotate > it has one parameter array > we declare a variable firstEl  > we initialize it with a value of an array on which we have used the shift method > this method changes the length of the array  > the shift method removes the first element from an array and returns that removed element > it also shifts all other elements to a lower index > we then use the push method on the array  > we push the 'firstEl' variable where we stored the first element which we removed from the same array > t...

Javascript Practice 54: JS Hero - Sorting arrays

  Question:  Write a function sort that takes an array filled with 3 numbers and returns these 3 numbers sorted in ascending order as an array. Example: sort([2, 3, 1]) should return [1, 2, 3]. Answer:  function sort (array) { return array.sort(function(a,b){return a - b}); } > declare a function sort > it has one parameter array > we use the sort () method to sort the array elements in ascending order > we declare a function within a function > if we subtract b - a, then we will get the elements sorted in descending order

Javascript Practice 53: JS Hero - Array: length

  Question:  Write a function getLastElement that takes an array and returns the last element of the array. Example: getLastElement([1, 2]) should return 2. Answer:  function getLastElement (anArray) {   let lastIndex = anArray.length - 1;   return anArray[lastIndex]; } > we learn how to access the last element of an array  > we use the length property  > this property will return the number of elements in the array  > subtracting 1 from the length of an array gives the index of the last element of that array > this index can be used to access the last element of that array  > we declare a function getLastElement > it has 1 parameter anArray > we declare a variable lastIndex > we initialize it with a value, which will be the index of the last element of the array > we then return the last element like this: anArray [lastIndex]

Javascript Practice 52: JS Hero - Set array elements

  Question:  Write a function setFirstElement that takes an array and an arbitrary variable. The variable should be inserted as the first element in the array. The array should be returned. Example: setFirstElement([1, 2], 3) should return [3, 2]. Answer:  function setFirstElement (array, v1) { array[0] = v1;  return array } > declare a function setFirstElement  > it has 2 parameters array & v1 > we set the first element of the array to the v1 parameter > like this: array[0] = v1 > we return the array > we learnt how to get the element of an array in the last exercise > and now we learnt how to set an element in this exercise

Javascript Practice 51: JS Hero - Get array elements

  Question:  Write a function getFirstElement that takes an array and returns the first element of the array. Example: getFirstElement([1, 2]) should return 1. Answer:  function getFirstElement (array) {  let firstEl = array[0]; return firstEl } > declare a function getFirstElement  > it has 1 parameter array > we declare a variable firstEl  > we initialize it with the first element of the array > we access the first element of the array by entering '0' in between the square brackets > index in an array begins with '0'  > we return the firstEl variable 

Javascript Practice 50: JS Hero - Arrays

  Question:  Write a function toArray that takes 2 values and returns these values in an array. Example: toArray(5, 9) should return the array [5, 9]. Answer:  function toArray (a,b) { let array = [a, b];  return array  } > declare a function toArray > it has 2 parameters > we declare a variable array > we initialize it with an array that contains the two parameters  > like this [a, b]  > we return the array

Javascript Practice 49: JS Hero - else if

  Question:   Write a function addWithSurcharge that adds two amounts with surcharge. For each amount less than or equal to 10, the surcharge is 1. For each amount greater than 10 and less than or equal to 20, the surcharge is 2. For each amount greater than 20, the surcharge is 3. Example: addWithSurcharge(10, 30) should return 44. Answer:   function addWithSurcharge(a, b) {     let sum = a + b;     if (a <= 10) {                sum += 1; } else if (a <= 20) {                sum += 2; } else      {sum += 3;}     if (b <= 10) {                sum += 1; } else if (b <= 20) {                sum += 2; } else      {sum += 3;}     return sum; };  or  function lineItemToSurcharge(a) {    if ( a <= ...

Javascript Practice 48: JS Hero - if...else

  Question:  Write a function addWithSurcharge that adds two amounts with surcharge. For each amount less than or equal to 10, the surcharge is 1. For each amount greater than 10, the surcharge is 2. Example: addWithSurcharge(5, 15) should return 23. Answer:  function addWithSurcharge (a,b) { if (a <= 10 && b <= 10) { return a + b + 2;  } else if (a > 10 && b > 10) {  return a + b + 4;  } else return a + b + 3; } ; or  function addWithSurcharge(a, b) {   let surcharge = 0;   if (a <= 10) {     surcharge = surcharge + 1;   } else {     surcharge = surcharge + 2;   }   if (b <= 10) {     surcharge = surcharge + 1;   } else {     surcharge = surcharge + 2;   }   return a + b + surcharge; } > in the first example we declare a function addWithSurcharge >>> it has two parameters >>> we create if / else if / else statements to...

Javascript Practice 47: JS Hero - Two returns

  Question:  Write a function repdigit that determines whether a two-digit decimal is a repdigit or not. If the decimal is a repdigit, 'Repdigit!' should be returned, otherwise 'No Repdigit!'. Example: repdigit(22) should return 'Repdigit!' and repdigit(23) should return 'No Repdigit!'. Answer:  function repdigit (n) { if (Number.parseInt(n/10) === (n/11)) { return 'Repdigit!'; }  return  'No Repdigit!'; }; or function repdigit(n) {   let ones = n % 10;   let tens = Math.floor(n / 10);    if (ones === tens) {     return 'Repdigit!';   }   return 'No Repdigit!'; }; > in the first example we have used the parseInt function to convert the result of n/10 into an integer >>> then once it is converted to an integer we check its equality with the result of n/11  >>> if both the results are equal then we are returned the Repdigit output  >>> or else we are returned the No  Repdigit outp...

Javascript Practice 46: JS Hero - if

  Question:  Write a function equals that checks two values for strict equality. If the two values are equal, the string 'EQUAL' should be returned. If they are unequal, you should get 'UNEQUAL'. Example: equals(1, 1) should return 'EQUAL' and equals(1, 2) should return 'UNEQUAL'. Answer:  function equals (a,b) { if (a === b) { return 'EQUAL'; } else { return 'UNEQUAL';} };  > declare a function equals  > it has 2 parameters > it uses if / else statements to execute a block of code > if a is strictly equal to b then the function would return 'Equal' or else 'Unequal

Javascript Practice 45: JS Hero - Compare numbers

  Question:  Write a function isThreeDigit that checks if a number is greater than or equal to 100 and less than 1000. Example: isThreeDigit(500) should return true and isThreeDigit(50) should return false. Answer:   function isThreeDigit (n) {  return n >= 100 && n < 1000;  };  > create a function isThreeDigit > has one parameter > return statement is: n >= 100 && n < 1000 > two conditions passed for result to come true > n has to be greater then or equal to 100 & less than 1000 

Javascript Practice 44: JS Hero - Strict inequality

  Question:  Write a function unequal that checks 3 values for strict inequality. The function should return true if all three parameters are strict unequal. Otherwise false. Example: unequal(1, 2, 3) should return true and unequal(1, 1, 2) should return false. Answer:  function unequal (a,b,c) { return a !== b && b !== c && a !== c;  };  > declare a function unequal  > it has 3 parameters > return statement is: a !== b && b !== c && a !== c > it checks for strict inequality between the 3 parameters > it will return true of all parameters are strictly unequal > it will return false if any 2 or all 3 of them are equal

Javascript Practice 43: JS Hero - Even numbers

  Question:  Write a function isEven that checks if a passed number is even. If the given number is even, true should be returned, otherwise false. Example: isEven(2) should return true and isEven(3) should return false. Answer:  function isEven (n) { return n % 2 === 0; };  > declare a function isEven > it has 1 parameter > return statement is: n % 2 === 0 > % = modulo operator gives the remainder obtained by dividing two numbers  >  ∴ if n is divided by 2 and the remainder is 0 then n is an even number > we shall get true if n is even and false if n is odd by using the modulo operator and the strict equality operator 

Javascript Practice 42: JS Hero - Three identical values

  Question:  Write a function equals that checks 3 values for strict equality. The function should only return true if all 3 values are equal. Example: equals(1, 1, 1) should return true and equals(1, 2, 1) should return false. Answer:  function equals (a,b,c) { return a === b && a === c; };  > declare a function equals > it has 3 parameters > the return statement is: a === b && a === c > so the expression above will first check if a is strictly equal to b  > then it will check if a is strictly equal to c > if a is strictly equal to b and a is strictly equal to c then it will give a true statement  > or else it will return false

Javascript Practice 41: JS Hero - Strict equality

  Question:  Write a function equals that checks two values for strict equality. Example: equals(1, 1) should return true and equals(1, 2) should return false. Answer:  function equals (a,b) { let x = a === b;  return x };   > create a variable x  > initialize it with a value  > like this a === b > the '===' or strict equality operator checks if the values a & b are equal  > the strict equality operator also checks if the data type of a & b are equal > we then return the variable 'x'

Javascript Practice 40: JS Hero - XOR

  Question:  Write a function xor that takes two Boolean values. If both values are different, the result should be true. If both values are the same, the result should be false. I.e.: The calls xor(true, false) and xor(false, true) should return true. The calls xor(true, true) and xor(false, false) should return false. Answer:  function xor (boo1, boo2) { let xor = (boo1 || boo2) && (!boo1 || !boo2); return xor;  };  > in this post, I will code a XOR Gate (eXclusive OR) > we declare a variable 'xor'  > we initialize it with a value of both our Boolean parameters > like this (boo1 || boo2) && (!boo2 || !boo1); > so if boo1 & boo2 are both true (or both are false) then variable 'xor' will store the value false and in all other cases it will store the value of true >> xor(true, true) = false >>>> (true || true) && (false || false) >>>> check in console >> xor(false, ...

Javascript Practice 39: JS Hero - NOR

  Question:  Write a function nor that takes two Boolean values. If both values are false, the result should be true. In the other cases the return should be false. I.e.: The call nor(false, false) should return true. The calls nor(true, false), nor(false, true) and nor(true, true) should return false. Answer:  function nor (boo1, boo2) { let or = boo1 || boo2;  return !or;  };  > in the last post, I coded a NAND Gate (not and, !and)  > in this post, I will code a NOR Gate (not or, !or) > we declare a variable or > we initialize it with a value of both our Boolean parameters > like this boo1 || boo2 > so if boo1 or boo2, any one of them is true then variable 'or' will store the value true and in all other cases it will store the value of false >> nor(true, false) = true >> nor(false, true) = true >> nor(true, true) = true >> nor(false, false) = false > but we need nor(false, false) to return true > s...

Javascript Practice 38: JS Hero - Boolean

  Question:  Write a function nand that takes two Boolean values. If both values are true, the result should be false. In the other cases the return should be true. I.e.: The call nand(true, true) should return false. The calls nand(true, false), nand(false, true) and nand(false, false) should return true. Answer:  function nand (boo1, boo2) { if (boo1 === true && boo2 === true) { return false;} else {return true} };  or  function nand (b1, b2) { let and = b1 && b2;  return !and }; > what is a Boolean > a Boolean is a value that either represents true or false > the reason we use Booleans, is to evaluate certain circumstances  > it is not a string but a Boolean (keyword) > in the question we have to write a function 'nand' that takes two Boolean values as parameters > we then evaluate them  > if both values are true then our result should be false > the first solution is fairly simple with the use of if and ...

Javascript Practice 37: JS Hero - parseInt

  Question:  Write a function add that takes a string with a summation task and returns its result as a number. Two natural numbers should be added. The summation task is a string of the form '102+17'. Example: add('102+17') should return 119. Answer:  function add (string) {  let firstNumber = parseInt(string);  let plusSignPosition = string.indexOf('+');  let extractSecondNumber = string.substr(plusSignPosition + 1);  let secondNumber = parseInt(extractSecondNumber); let totalValue = firstNumber + secondNumber;  return totalValue;  };  > let us understand what we are getting in a string for this question > a summation expression in a string object > for eg. '2+3' or '256+351', '15985 + 65', etc           -check in the console by simply typing typeof '65+9' and pressing 'enter' > these are not numbers but they are strings as they are wrapped in quotes ' '  > the summation task could have...

Javascript Practice 36: JS Hero - Random numbers

  Question:  Write a function dice that returns like a dice a random number between 1 and 6. Answer:  function dice () { return (Math.floor(Math.random() * 6)) + 1; };  > function dice has to return a random number between 1 and 6  > random numbers can be generated using the Math.random method > Math.random returns a floating-point, pseudo-random number > random number is usually between 0 (inclusive) and 1 (exclusive) > let me repeat - random number generated will be between 0 & 1 > 0 will be inclusive & 1 will be exclusive > 0 will be included and 1 will not be included  > we shall be generating numbers with decimal points (real numbers) and not a whole number > for eg 0.75 or 0.89 or 0.9998898988898552 etc > but we need to get a random whole number between two values i.e. 1 and 6  > so first we need to use Math.random() to generate a number between 0 & 1 > we then multiply that with 6 like this: (...

Javascript Practice 35: JS Hero - Rounding

  Question:  Write a function round100 that rounds a number to the nearest hundred. Example: round100(1749) should return 1700 and round100(856.12) should return 900. Answer:  function round100 (number) {  return Math.round (number / 100) * 100;  };  > this was a bit tough for me > i spent almost an hour understanding the logic behind this > however I did a lot of googling to understand this > its fairly simple in excel, i checked the function Round in excel > i also realized that I can always check and experiment on the console to get some hints > however you are supposed to be good not only in math but mainly in logic to execute this in JS > this question needed me to round off any number to the nearest 100  > lets say the number is 149 > lets break the above equation Math.round (number / 100) * 100 number / 100: 149 / 100 = 1.49 Math.round(1.49) =  1 (check this in the debugging console) Math.round(1.49)*100 =  1...