lezzles11
5/6/2019 - 8:26 PM

Free Code Camp - guess and actual

// GUESS
function truncateString(str, num) {
  // Clear out that junk in your trunk
  if (str.length > num) {
    var array = str.split("");
    var slice = array.slice(0, [num + 3]);
    return (slice + "..."); 
} else if (str.length === num) {
  return str; 
}
}; 
// END GUESS
// ACTUAL ANSWER
function truncateString(str, num) {
  //if string length is bigger than number and number is bigger than 3
  if (str.length > num && num > 3) {
    return str.slice(0, (num - 3)) + '...';
    // if string length is greater than num and num is less than 3
  } else if (str.length > num && num <= 3) {
    //return the string slice and the ... after
    return str.slice(0, num) + '...';
  } else { 
    return str; 
  }
}

// END OF ACTUAL ANSWER
truncateString("A-tisket a-tasket A green and yellow basket", 8);

//GUESS
function booWho(bool) {
  // What is the new fad diet for ghost developers? The Boolean.
  return bool;
  if (bool === true || bool === false) {
    return true; 
  } else {
    return false; 
  }
}

booWho(null);

// ACTUAL ANSWER
function booWho(bool) {
  return typeof bool === 'boolean'; 
}

//GUESS
function titleCase(str) {
  let array = str.split(""); 
  for (var i = 0; i <= str.length; i++) {
    
    array[i].toUpperCase();
  }
  return array;
}

// ACTUAL ANSWER
function titleCase(str) { 
  //string to all lowercase in array 
  var array = str.toLowerCase().split(" ");
  //copy array and map on to another array
  //at beginning of every word, replace it with the uppercase 
  var result = array.map(function(val){
    return val.replace(val.charAt(0), val.chartAt(0).toUpperCase()); 
  });
  //join everything at the end
  return result.join(" ");
}
titleCase("I'm a little tea pot");

//GUESS
  var falsey = [];
  for (var i = 0; i <= arr.length; i++) {
    if (typeof (arr[i] === 'boolean')) {
      var falsey = [];
      falsey.push(arr[i]); 
      return falsey; 
    }
  }
  return arr;
}

//ACTUAL ANSWER
function bouncer(arr) {
  //filter out the following values:
  return arr.filter(function (value) {
    // if the value does not equal boolean 
    if (value !== Boolean) {
      //return the value
      return value; 
    }
  }); 
}

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

// GUESS 
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.

// 1. loop through each number in the array 
// 2. Find if the number is less than the number
// 3. Count those numbers 
  for (var i = 0; i <= arr.length; i++) {
    var total = 0; 
    if (arr[i] < num) {
      total += 1; 
    } 
    } return total; 
  }
// ACTUAL
function getIndexToIns (arr, num) {
  // count the length of array
  var times = arr.length; 
  // begin variable count 
  var count = 0; 
  // while looping over array 
  for (var i = 0; i < times; i++) {
    // if array is less than array
    if (num > arr[i])
    // add one to count 
      {count++;} 
      // return the total count 
  } return count; 
  }

get IndexToIns([40, 60], 50);

//GUESS
function mutation(arr) {
  
// 1. find the two elements in array 
// 2. identify all letters in each array 
// 3. turn evcerything into lower case
// 4. if letters in second match letters in first, return true 
  var first = arr[0].toLowerCase();
  var second = arr[1].toLowerCase();  
  if (first.)
  if (arr.indexOf(1))
    arr.indexOf(arr.length);
}
//ACTUAL ANSWER
 function mutation(arr) {
   var test = arr[1].toLowerCase();
   var target = arr[0].toLowerCase();
   // looping over the length of the first word 
   for (var i = 0; i < test.length; i++) {
     // comparing strict equality
     // if, in each letter of the second letter, is in the index of the first, 
     if (target.indexOf(test[i]) < 0)
     return false; 
   }
   return true; 
 }


mutation(["hello", "hey"]);
// GUESS
 function chunkArrayInGroups(arr, size) {
  // Break it up.
  //  1. size is the number in each array
  // 2. collect the number of arr[i] based on size
  // 3. put it in an array
  // 4. print out all the arrays 
  var newArray = [];
  for (var i = 0; i <= arr.length; i++) {
    arr.length = size; 
    newArray.push(); 
    return newArray; 
  } 
}

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

// REAL
function chunkArrayInGroups(arr, size) {
  // create new arrays
  var temp = []; 
  var result = []; 
  // looping through the array
  for (var a = 0; a < arr.length; a++) {
    // push the array into temp array 
    if (a % size !== size - 1) 
      temp.push(arr[a]); 
      // push the arr[i] into result array 
    else {
      temp.push(arr[a]);
      result.push(temp);
      temp = []; 
    }

  }

  if (temp.length !== 0)
    result.push(temp);
  return result; 
}