Freecodecamp Algorithm Solutions

Reverse a String

function reverseString(str) {
  var newStr = "";
  for (var i = str.length-1; i >= 0; i-- ) {
    newStr += str[i];
  }
  return newStr;
}

reverseString("hello");

 

Seek and Destroy  [JSBIN]

function destroyer(arr) {
  // Put all arguments, except the 1st one, into an array
  var excludes = [];
  for (var i=1; i < arguments.length; i++ ) {
    excludes.push(arguments[i]);
  }
  return arr.filter( function (element) { 
    return excludes.indexOf(element) == -1; 
  });
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

Leave a Comment