Posts

Junior Developer Central JS Practice Exercise - 02

  Question:  Write a Javascript program to get the extension of a filename.  Answer:  function fileNameExt (str) { let extFileName = str.slice(str.lastIndexOf('.')); return extFileName; } > declare a function 'fileNameExt'  > it has one parameter 'str'  > we declare a variable 'extFileName'  > it is initialized with a value when we use some basic string methods to get our output > our program should extract the extension of a filename and return just that >> like this:  fileNameExt ( 'index.html') should return '.html' > so first we use the slice function to slice the input and return the last portion of the string  > in order for the slice function to work, we need to pass in a start index number and an end index number (optional) > since we do not know what the filename is, we cannot be sure of a specific index number > the one thing we are sure of is that the extension name of a file is always followed...

Junior Developer Central JS Practice Exercise - 01

Question: Write a Javascript program to check two numbers and return true if one of the number is 100 or if the sum of the two numbers is 100. Answer: function check2nos (a,b) { if (a === 100 || b === 100 || (a+b) === 100) { return true; } else {  return false } }; > declare a function check2nos > it has two parameters 'a' and 'b' > we use an 'if / else statement' to check our conditions before we return the answer > we have three major conditions to fulfil >> we have to check if 'a' equals 100 or >>  we have to check if 'b' equals 100 or >>  we have to check if 'a + b' equals 100 > if any one of the above three conditions are met, then our program must return 'true > else it should return false

Javascript Practice 80: JS Hero - To be continued ...

  Question:  Write a function digitsum that calculates the digit sum of an integer. The digit sum of an integer is the sum of all its digits. Example: digitsum(192) should return 12. Answer:  function digitsum (n) { let digits = n.toString().split('').map(x => x * 1);  let sum = digits.reduce((acc,cv) => acc + cv);  return sum; }; > declare a function 'digitsum' > it has one parameter 'n'  > we create our first variable 'digits'  > we initialize it with the output we get by converting the input 'n'  >>> from a number to a string,  >>> then splitting the string in an array format  >>> and then converting the array of strings to a number by using the map method > we then create our final variable sum and initialize it with a value we get by using the reduce method on the variable 'digits'  > finally we return 'sum' 

Javascript Practice 79: JS Hero - Project Euler

  Question:  Write a function sumMultiples taking a natural number n and returning the sum of all multiples of 3 and of 5 that are truly less than n. Example: All multiples of 3 and 5 less than 20 are 3, 5, 6, 9, 10, 12, 15 and 18. Their sum is 78. sumMultiples(20) should return 78. Answer:  function sumMultiples (n) { let sum = 0;  for (let i = 1; i < n; i++) { if (i % 3 === 0 || i % 5 === 0) {sum += i;} } return sum;  }; > we declare a function 'sumMultiples' > it has one parameter 'n'  > we declare a variable 'sum' > we initialize it with a value of '0'  > we open a 'for loop'  > this loop will use all the numbers below 'n' to check for our conditions > we open an 'if' statement that will have to meet any one of the two conditions to execute the code in its block > the 'for loop' will only add the number to the 'sum' if it is a multiple of 3 or 5  > it will not add any number to the...

Javascript Practice 78: JS Hero - Roman numerals II

  Question:  Write a function roman that converts an Arabic number (up to 1000) into a Roman numeral. Example: roman(483) should return 'CDLXXXIII'. Answer:  function roman (n) { let romanObj = { M: 1000,  CM: 900,  D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }; let roman2 = '';  for (let key in romanObj) { while (n >= romanObj[key]) { roman2 += key; n -= romanObj[key];  } } return roman2;  };  > we declare a function 'roman' > it has one parameter 'n' > we declare a variable ‘romanObj’  > we initialize it with roman numerals and their values in ‘key: value’ format > like this: {I:1, V:5, X:10, etc} > thus we make the variable ‘romanObj’ an object; that holds multiple values in terms of properties and methods > we declare another variable ‘roman2’ and it is an empty string > we leave it empty as we shall initialize it later by adding the roman version of the integers and then...

Javascript Practice 77: JS Hero - Roman numerals I

  Question:  Write a function arabic that converts a Roman number (up to 1000) into an Arabic. Example: arabic('CDLXXXIII') should return 483. Answer:  function arabic (s) { let romanObj = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 };  let arabicArr = [];  for (let i = 0; i < s.length; i++) { if (romanObj[s[i]] < romanObj[s[i+1]]) { arabicArr.push(romanObj[s[i+1]] - romanObj[s[i]]);  i++;  continue; } else {arabicArr.push(romanObj[s[i]]);} } return arabicArr.reduce((acc,cv) => acc + cv); } > we declare a function ‘arabic’ > it has one parameter ‘s’  > we declare another variable ‘romanObj’  > we initialize it with roman numerals and their values in ‘key: value’ format > like this: {I:1, V:5, X:10, etc} > thus we make the variable ‘romanObj’ an object; that holds multiple values in terms of properties and methods > we also declare another variable ‘arabicArr’ , it is an empty array, like this: ‘ [] ‘ >...

Javascript Practice 76: JS Hero - Recursion

  Question:  Write a function reverse that reverses the order of the characters in a string. The function should be recursive. Example: reverse('live') should return 'evil'. Answer:  function reverse (str) { if (str < 1) { return '';  } else { return reverse(str.substr(1)) + str.charAt(0); } }; > we first declare a function 'reverse' > it has one parameter 'str'  > our goal is to take a string and reverse it as the name suggests > we shall do the reversing using recursion  > recursion is a function or a method that calls itself several times till it meets the base case   > because it is a recursion we shall be needing a base case > this base case will let us know when to stop running the recursion over and over  > in the process of 'reverse' function we shall be chopping off the first letter / character over and over > which will eventually lead to the length of the string be: ' less than 1 '  > so...