^_~ List

home

FCC - Basic Algorithm Scripting

05 Dec 2016

Reverse a String

function reverseString(str) {

  return str.split("").reverse().join("");
}

reverseString("hello");

Factorialize a Number

function factorialize(num) {
  var factorial = 1;
  for (var n = 2; n <= num; n++) {
    factorial = factorial * n;
  }

  return factorial;
}
factorialize(5);

Check for Palindromes

function palindrome(str) {
  // Good luck!
  var toChk = str.toLowerCase().match(/[a-zA-Z]|\d/g);
  return toChk.join("") === toChk.reverse().join("");
}



palindrome("eye");

Find the Longest Word in a String

function findLongestWord(str) {
  var chk = 0, arr = str.split(" ");

  for(var i=0; i<arr.length;i++){
    chk = arr[i].length > chk ? arr[i].length :chk;
  }
  return chk;
}

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

Title Case a Sentence

function titleCase(str) {

  var arr = str.split(" ");

  for(var i = 0; i<arr.length; i++){
    var corrSpell = ""
    for(var j = 1; j<arr[i].length; j++){
        corrSpell += arr[i][j].toLowerCase();
      }
    arr[i] = arr[i][0].toUpperCase() + corrSpell;
  }

  return arr.join(" ");
}

titleCase("I'm a little tea pot");

Return Largest Numbers in Arrays

function largestOfFour(arr) {
  // You can do this!
  var returnArr = [];
  for (i=0; i<arr.length; i++){
    returnArr[i] = arr[i][0];
    for (j=0; j<arr[i].length; j++){
      returnArr[i] = returnArr[i] > arr[i][j] ? returnArr[i] : arr[i][j];
    }

  }
  return returnArr;
}

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

Confirm the Ending

function confirmEnding(str, target) {


  return str.substr("-"+target.length) == target;
}

confirmEnding("Bastian", "n");

Repeat a string repeat a string

function repeatStringNumTimes(str, num) {
  // repeat after me
 if (num <=0){
   return "";
 }
  var returnStr = "";
  for (i=1; i<=num;i++){
    returnStr += str;
  }
  return returnStr;
}

repeatStringNumTimes("abc", 3);

Truncate a string

function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (num>3){
    return str.length <= num ? str : str.slice(0,num-3)+"...";
  }
  return str.length <= num ? str : str.slice(0,num)+"...";
}

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


Chunky Monkey

function chunkArrayInGroups(arr, size) {
  // Break it up
  var newArr = [];
  for (i = 0; i<arr.length; i= i+size){

  var chunk = arr.slice(i, i+size);

  newArr.push(chunk);
  }

  return newArr;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);


Slasher Flick

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  arr.splice(0,howMany);
  return arr;
}

slasher([1, 2, 3], 2);

Mutations

function mutation(arr) {

  var str = arr[0].toLowerCase();
  var letters = arr[1].toLowerCase().split("");

  for (i=0; i<letters.length; i++){
    if (str.indexOf(letters[i]) <0){
      return false;
    }
  }

  return true;
}

mutation(["hello", "hey"]);

Falsy Bouncer

function isBoolean(val){
  if (val){
    return true;
  }
  return false;
}

function bouncer(arr) {
  // Don't show a false ID to this bouncer.

  return arr.filter(isBoolean);
}

bouncer([7, "ate", "", false, 9]);


Seek and Destroy

function destroyer(arr) {
  // Remove all the values
  var arrChk = arguments;
  function filterFunc(val){
    for (i=1;i<arrChk.length;i++){

      if (val== arrChk[i]){
        return false;
      }
    }
    return true;
  }

   var returnArr = arrChk[0];

  return returnArr.filter(filterFunc);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Where do I belong

function getIndexToIns(arr, num) {


  arr = arr.sort(function(a,b){
    return a-b;
  });

  for (i=0;i<arr.length;i++){

    if (num == arr[i]){
      return i;
    }
    if (num>=arr[i] && num<arr[i+1]){
      return i+1;
    }
  }
  return arr.length;
}

getIndexToIns([2, 5, 10], 15);
//getIndexToIns([40, 60], 50);


Caesars Cipher


function rot13(str) { // LBH QVQ VG!
  var returnStr="";
  // char 65-90



  for(i=0;i<str.length;i++){
    var index = str.charCodeAt(i);

    if (index < 65 || index > 90){
      returnStr +=str[i];
    }
    if(index+13>90){

      returnStr +=String.fromCharCode(65-(91-index-13));
    }
    if(index >= 65 && index+13<=90){
      returnStr +=String.fromCharCode(index+13);
    }

  }

  return returnStr;
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");