Basic Algorithm Scripting Flashcards

1
Q

Reverse string (code)

A
function reverseString(str) {
  str = str.split("").reverse().join("");
  return str;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Factorial (code)

A
function factorialize(num) {
  if(num == 0){ //the base case
    return 1;
  } else {
  num = factorialize(num-1) * num;
  }
  //console.log(num);
  return num;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Find the longest length word in a string (code)

A
function findLongestWordLength(str) {
  var words = str.split(' ');
  var maxLength = 0;
  for (var i = 0; i < words.length; i++) {
    if (words[i].length > maxLength) {
      maxLength = words[i].length;
    }
  }

return maxLength;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Return largest number in array of arrays (code)

A
function largestOfFour(arr) {
  var largest = [];
  for (let i = 0; i < arr.length; i++){
    var maxNum = 0;
    for (let j = 0; j < arr[i].length; j++){
      if (arr[i][j] > maxNum){
        maxNum = arr[i][j];
      }
    }
    largest.push(maxNum);
  }

return largest;
}

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A
function confirmEnding(str, target) {
    let testRegex = new RegExp(target+"$","i");
    return testRegex.test(str);
// OR A BETTER WAY IS TO DO
// return str.slice(str.length - target.length) === target;
}

console.log(confirmEnding(“Bastian”, “n”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Repeat a string (first arg) however many times (second arg)

A
function repeatStringNumTimes(str, num) {
    if (num < 0){
      return "";
    } else {
      let finalStr = '';
      while(num > 0){
        finalStr += str;
        num--;
      } return finalStr;
    }
  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Truncate a string if it’s longer than length given

A

function truncateString(str, num) {

  if(str.length > num){
    return str.slice(0,num) + "...";
  } else {
    return str;
  }
}

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument as function). If no element passes the test, return undefined (code)

A

function findElement(arr, func) {

for(let i = 0; i < arr.length; i++){
  let num = func(arr[i]);
  if (num == true){
    return arr[i];
  }
}
}

findElement([1, 2, 3, 4], num => num % 2 === 0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

Boolean primitives are true and false.

A
function booWho(bool) {
  if (typeof bool === 'boolean'){
    return true
  } else {
    return false;
  }

}

booWho(null);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like “the” and “of”. (code)

A
function titleCase(str) {
  return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());
}

titleCase(“I’m a little tea pot”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

You are given two arrays and an index.

Use the array methods slice and splice to copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

Return the resulting array. The input arrays should remain the same after the function runs.

A
function frankenSplice(arr1, arr2, n) {
  let newArr = arr2.slice();
  for(let i = 0; i < arr1.length; i++){
newArr.splice(n+i,0,arr1[i]);

}

return newArr;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Remove all falsy values from an array.

Falsy values in JavaScript are false, null, 0, “”, undefined, and NaN.

Hint: Try converting each value to a Boolean.

A
function bouncer(arr) {
    let newArr = [];
    for (let i = 0; i < arr.length; i++) {
        if (!!arr[i]) {
            newArr.push(arr[i]);
        };
    }
return newArr;

}

bouncer([7, “ate”, “”, false, 9]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

A
function getIndexToIns(arr, num) {
  return arr.filter(val => num > val).length;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

For example, [“hello”, “Hello”], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments [“hello”, “hey”] should return false because the string “hello” does not contain a “y”.

Lastly, [“Alien”, “line”], should return true because all of the letters in “line” are present in “Alien”.

A

function mutation(arr) {
var test = arr[1].toLowerCase();
var target = arr[0].toLowerCase();
for (var i = 0; i < test.length; i++) {
if (target.indexOf(test[i]) < 0) return false;
}
return true;
}
mutation([“hello”, “Hello”]);
mutation([“hello”, “hey”]) ;
mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

A
function chunkArrayInGroups(arr, size) {
  // Break it up.
  var arr2 = [];
  for (var i = 0; i < arr.length; i += size) {
    arr2.push(arr.slice(i, i + size));
  }
  return arr2;
}

chunkArrayInGroups([“a”, “b”, “c”, “d”], 2);
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3);
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly