Algorithm Digest #2

Here we are going to explain 5 algorithms

Algorithm #1

Confirm the Ending

Check if a string (first argument, str) ends with the given target string (second argument, target).



/**
 * Confirm the Ending
Check if a string (first argument, str) ends with the given target string (second argument, target).

This challenge can be solved with the .endsWith() method, which was introduced in ES2015. 
But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.


 */

/*
function confirmEnding(str, target) {
  if (str.endsWith(target)) return true
  return false;
}
*/

 function confirmEnding(str, target) {
  return str.slice(str.length - target.length) === target;
}

confirmEnding("Bastian", "e");

Algorithm #2

Finders Keepers


/**
 * 
 * Finders Keepers
Create a function that looks through an array arr and returns the first element in it that passes a 'truth test'. 
This means that given an element x, the 'truth test' is passed if func(x) is true. 
If no element passes the test, return undefined.
 */
 function findElement(arr, func) {
    let num = 0;
    for (let i = 0; i < arr.length; i++) {
      num = arr[i];
      if (func(num)) {
        return num;
      }
  
    }
    return undefined;
  }
  
  findElement([1, 2, 3, 4], num => num % 2 === 0);

Algorithm #3

Repeat a String

Repeat a given string str (first argument) for num times (second argument).



/**
 * Repeat a String Repeat a String
Repeat a given string str (first argument) for num times (second argument). 
Return an empty string if num is not a positive number. For the purpose of this challenge, 
do not use the built-in .repeat() method.


 */

function repeatStringNumTimes(str, num) {
    let repeatStr='';
    if (num <= 0) repeatStr = '';
    for (let i = 0; i < num; i++) {
        repeatStr += str;
    }
    console.log(repeatStr);
    return repeatStr;
}

repeatStringNumTimes("abc", 3);

Algorithm #4

Truncate a String

Truncate a string (first argument) if it is longer than the given maximum string length (second argument).



/**
 * Truncate a String
Truncate a string (first argument) if it is longer than the given maximum string length (second argument). 
Return the truncated string with a ... ending.
 */

function truncateString(str, num) {
    return str.length > num ? str.slice(0, num) + "..." : str;
}

truncateString("A-tisket a-tasket A green and yellow basket", 8);

Algorithm #5

Boo who

Check if a value is classified as a boolean primitive. Return true or false.



/**
 * Boo who
Check if a value is classified as a boolean primitive. Return true or false.

Boolean primitives are true and false.
 */

function booWho(bool) {
    return typeof bool==="boolean"
  }
  
  booWho(true);

Algorithm Digest #1

Here we are going to explain 5 algorithms

Algorithm #1

convert from Celsius to Fahrenheit

The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.


/**
 * The algorithm to convert from Celsius to Fahrenheit is the temperature in Celsius times 9/5, plus 32.
 */
 function convertToF(celsius) {
    let fahrenheit= (celsius * 9/5)+32;
    return fahrenheit;
  }
  
  convertToF(30);

Algorithm #2

Factorialize a Number

Return the factorial of the provided integer.

If the integer is represented with the letter n, a factorial is the product of all positive
integers less than or equal to n.

Factorials are often represented with the shorthand notation n!

For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

Only integers greater than or equal to zero will be supplied to the function.


/**
 *Factorials are often represented with the shorthand notation n!

For example: 5! = 1 * 2 * 3 * 4 * 5 = 120

Only integers greater than or equal to zero will be supplied to the function.
 */
function factorialize(num) {
    let result = 1;
    if (num >= 0) {
      if (num === 0) result = 1;
      else {
        for (let i = 1; i <= num; i++) {
          result=result * i;
        }
      }
      num = result;
      return num;
    }
  }
  
  factorialize(5);
  convertToF(30);

Algorithm #3

Find the Longest Word in a String

Find the Longest Word in a String
Return the length of the longest word in the provided sentence.

Your response should be a number.


/**
 *Return the length of the longest word in the provided sentence.
 */

function findLongestWordLength(str) {
  let strArray = [];
  strArray = str.split(' ');
  var longestWord = "";
  strArray.forEach(function(word) {
    if(word.length > longestWord.length) {
      longestWord = word;
    }
  });
  return longestWord.length;
}

findLongestWordLength("The quick brown fox jumped over the lazy dog");

Algorithm #4

Return Largest Numbers in Arrays

Return Largest Numbers in ArraysReturn an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.


/**
 *Return Largest Numbers in Arrays
Return an array consisting of the largest number from each provided sub-array. For simplicity, 
the provided array will contain exactly 4 sub-arrays.
 */

function largestOfFour(arr) {
    let largetArray = [];
    arr.forEach(element => {
        largetArray.push(Math.max(...element))
    });
    return largetArray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Algorithm 5

Reverse the provided string.

You may need to turn the string into an array before you can reverse it. Your result must be a string.


/**
 * Reverse the provided string.
   You may need to turn the string into an array before you can reverse it.
   Your result must be a string.
 */
 

function reverseString(str) {
    return str.split('').reverse().join('');
}

reverseString("hello");