Posts

Junior Developer Central JS Practice Exercise - 22

  Question:  Write a Javascript function that accepts a string as a parameter and counts the number of vowels within the string.  Answer:  function vowelCount (str) {   let vowels = /[aeiou]/gi;   let result = str.match(vowels);   let count = result.length;   return count;       }; > we declare a function 'vowelCount'  > it has one parameter 'str', which is a string > we declare a variable 'vowels' and initialize it with the 5 vowels > the 'g' modifier is used on the 'vowels' variable to perform a global match  > a global match will find all matches and will not stop after finding the first match >  additionally   the 'i' modifier is used  on the 'vowels' variable to perform case insensitive matching > we declare another variable 'result' and initialize it with the result we get when we use the match () method on the 'str' parameter > the match() method searches a string for a ma...

Junior Developer Central JS Practice Exercise - 21

  Question:  Write a Javascript function that returns a passed string with letters in alphabetical order.  Example String:            'javascript'  Example Output:             ' aacijprstv' Answer:  function alphaStr (str) { return str.split('').sort().join('') }            > we declare a function 'alphaStr'  > it has one parameter 'str'  > we use the split ('') method on the string 'str' to split all the characters in an array > we then use the sort() method to sort the characters in alphabetical order > we then use the join('') method to create a string by concatenating all the sorted elements of the array created  > we return this line of code to get our alphabetically sorted string

Junior Developer Central JS Practice Exercise - 20

  Question:  Write a Javascript function that returns true if the provided predicate function returns true for all elements in a collection, false otherwise.  Answer:  function checkFunction(arr, fn) {   for (let i = 0; i < arr.length; i++) {     if (!fn(arr[i])) {       return false;     }   }   return true; } > we declare a function 'checkFunction'  > it has two parameters 'arr' & 'fn'  > we want to return false if 'fn' (which is a function) returns false for even one element in 'arr' (which is an array) > our function should return true if the function 'fn' returns true for all elements in array 'arr' > so in order to check the result of the function 'fn' on each and every element of array 'arr' we use the for loop >  we additionally use the if statement to check the boolean value on each iteration of the for loop > we return false if the condition is met and the loop is t...

Junior Developer Central JS Practice Exercise - 19

  Question:  Write a Javascript program to generate a random hexadecimal color code Answer:  function getRandomColor() {                    let letters = '0123456789ABCDEF'.split('');         let color = '#';         for (var i = 0; i < 6; i++ ) {             color += letters[Math.round(Math.random() * 15)];         }         return color;     } > we declare a function 'getRandomColor'  > it has no parameters > we create a variable 'letters'  > it is a string of 16 characters - '0123456789ABCDEF' > we use the split () method to return an array with 16 array elements > we declare another variable 'color'  > its is a string with only the '#' character > we then create a for loop  >> initialization - before the loop starts we set our variable i = 0 ...

Junior Developer Central JS Practice Exercise - 18

  Question:  Write a Javascript program to convert a comma-separated values (CSV) string to a 2D array. A new line indicates a new row in the array.  for eg.  Input:  const oldString = `abc,def,ghi jkl, mno, pqr stu, vwx, yza`; Output:  0: (3) ["abc", " def", " ghi"] 1: (3) ["jkl", " mno", " pqr"] 2: (3) ["stu", " vwx", " yza"] Answer:  function createArr(str) {   return str.split('\n').map(row => row.split(',')); } > we declare a variable 'createArr'  > it has one parameter 'str'  > to explain this function, we shall use the input example in the question above 'oldString' > we first split the 'str' with the split () method that will return an array of substrings > the split ('\n') method will return a new array which will have three items  > each of these items is a row from the 'oldString' mentioned above >...

Junior Developer Central JS Practice Exercise - 17

  Question:  Write a Javascript program to compare two objects to determine if the first one contains the same properties as the second one (which may also have additional properties) for eg:  const objA = { a: 1, b: 2, c: 1 };  const objB = { a: 1, b: 1, c: 1 }; const objC = { a: 1, b: 1, d: 1 }; Answer:  function comTwoObj (obj1, obj2) { const obj2Keys = Object.keys(obj2);  return Object.keys(obj1).every(key => obj2Keys.includes(key)); } > we declare a function 'comTwoObj'  > it has 2 parameters 'obj1' & 'obj2'  > we want to compare the keys of the two objects 'obj1' & 'obj2'  > we do not want to compare the values, only the keys > in our case the 'keys' are: a, b, c & so on > we want to check if 'obj2' has all the keys of 'obj1'  > 'obj2' can have additional keys as well, for eg: a, b, c, d, e & so on > but it should have all the keys of 'obj1'  > so first we...

Junior Developer Central JS Practice Exercise - 16

  Question:  Given a year, report if it is a leap year.  Answer:  function isLeapYear (year) { const leapYear01 = year;  const leapYear02 = leapYear01 / 4; if (Number.isInteger( leapYear02 )) { return true;  } else {  return false; } } > we declare a function 'isLeapYear'  > it has one parameter 'year'  > leap year has 366 days > leap year comes every 4 years > all leap years are divisible by 4 > hence all the years that are divisible by 4 are 'leap years'  > so first we create a variable 'leapYear01'  > we initialize it with a value that we get in 'year' argument > then we create another variable 'leapYear02'  >  we initialize it with a value that we get by dividing the  'leapYear01' by 4 > we then open an if/else statement > we want to check if the value stored in  'leapYear02' is an integer > an integer is a whole number > an integer is a number that can be written wit...