Posts

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...