WEB818
9/20/2019 - 9:19 PM

array drills

array drills

function makeList(item1, item2, item3) {
  return [item1, item2, item3];
}



/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testMakeList() {
  const items = ['prime rib', 'fried goat cheese salad', 'fish tacos'];
  const result = makeList(items[0], items[1], items[2]);

  if (
    result &&
    result.length &&
    items.length === result.length &&
    items.every(function(item) {
      return result.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `makeList` works!');
  } else {
    console.error('FAILURE: `makeList` is not working');
  }
}

testMakeList();
function addToList(list, item) {
  list.push(item);
  return list;
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testAddToList() {
  const input1 = ['red', 'blue', 'green'];
  const input2 = 'pink';
  const expected = ['red', 'blue', 'green', 'pink'];
  const result = addToList(input1, input2);

  if (
    result &&
    result.length &&
    expected.length === result.length &&
    expected.every(function(item) {
      return result.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `addToList` works!');
  } else {
    console.error('FAILURE: `addToList` is not working');
  }
}

testAddToList();
function accessFirstItem(array) {
  return array[0];
}

function accessThirdItem(array) {
  return array[2];
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  if (fn(input) === expected) {
    console.log(`SUCCESS: "${fn.name}" works on [${input}]`);
    return true;
  } else {
    console.log(
      `FAILURE: ${fn.name}([${input}]) should be ${expected} but was ${fn(
        input
      )}`
    );
    return false;
  }
}

function runTests() {
  var list = [1, 4, 9, 16, 25];
  var item1 = 1;
  var item2 = 9;

  var testResults = [
    testFunctionWorks(accessFirstItem, list, item1),
    testFunctionWorks(accessThirdItem, list, item2),
  ];

  var numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();
function findLength(array) {
  return array.length;
}

function accessLastItem(array) {
  return array.pop();
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  if (fn(input) === expected) {
    console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
    return true;
  } else {
    console.error(
      'FAILURE: `' +
        fn.name +
        '([' +
        input +
        '])` should be ' +
        expected +
        ' but was ' +
        fn(input)
    );
    return false;
  }
}

function runTests() {
  const list = [1, 4, 9, 16, 25];
  const originalList = [1, 4, 9, 16, 25];
  const length = 5;
  const lastItem = 25;

  const testResults = [
    testFunctionWorks(findLength, list, length),
    testFunctionWorks(accessLastItem, list, lastItem),
  ];

  const numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();
function firstFourItems(array) {
  return array.slice(0,4);
  }

function lastThreeItems(array) {
  const result2 = array.slice(-3);
  console.log(result2);
  return result2;
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  const result = fn(input);
  if (
    result &&
    result.length === expected.length &&
    result.every(function(item) {
      return expected.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `' + fn.name + '` works!');
    return true;
  } else {
    console.error('FAILURE: `' + fn.name + '` is not working');
    return false;
  }
}

function runTests() {
  const list = ['red bull', 'monster', 'amp', 'rockstar', 'full throttle'];
  const result1 = ['red bull', 'monster', 'amp', 'rockstar'];
  const result2 = ['amp', 'rockstar', 'full throttle'];

  const testResults = [
    testFunctionWorks(firstFourItems, list, result1),
    testFunctionWorks(lastThreeItems, list, result2),
  ];

  const numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();
function minusLastItem(array) {
 return array.slice(0,-1);
}

function copyFirstHalf(array) {
  return array.slice(0, array.length/2);
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  const result = fn(input);
  if (
    result &&
    result.length === expected.length &&
    result.every(function(item) {
      return expected.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `' + fn.name + '` works!');
    return true;
  } else {
    console.error('FAILURE: `' + fn.name + '` is not working');
    return false;
  }
}

function runTests() {
  const list = [
    'red bull',
    'monster',
    'amp',
    'rockstar',
    'full throttle',
    'kickstart',
  ];
  const result1 = ['red bull', 'monster', 'amp', 'rockstar', 'full throttle'];
  const result2 = ['red bull', 'monster', 'amp'];
  const list2 = ['lions', 'tigers', 'bears'];
  const result3 = ['lions'];
  
  const testResults = [
    testFunctionWorks(minusLastItem, list, result1),
    testFunctionWorks(copyFirstHalf, list, result2),
    testFunctionWorks(copyFirstHalf, list2, result3)
  ];

  const numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();
function squares(array) {
  return array.map(array => array ** 2);
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  const result = fn(input);
  if (
    result &&
    result.length === expected.length &&
    result.every(function(item) {
      return expected.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `' + fn.name + '` works!');
    return true;
  } else {
    console.error(
      'FAILURE: `' +
        fn.name +
        '([' +
        input +
        '])` should be ' +
        expected +
        ' but was ' +
        fn(input)
    );
    return false;
  }
}

function runTests() {
  const input1 = [1, 2, 3, 4, 5];
  const result1 = [1, 4, 9, 16, 25];
  const input2 = [2, 4, 6, 8];
  const result2 = [4, 16, 36, 64];

  const testResults = [
    testFunctionWorks(squares, input1, result1),
    testFunctionWorks(squares, input2, result2),
  ];

  const numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();
function greatestToLeast(array) {
  return array.sort(function(a,b) {
    return b-a;
  })
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  const result = fn(input);
  if (
    result &&
    result.length === expected.length &&
    result.every(function(item, index) {
      return index === 0 || result[index] < result[index - 1];
    }) &&
    result.every(function(item) {
      return expected.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `' + fn.name + '` works!');
    return true;
  } else {
    console.error(
      'FAILURE: `' +
        fn.name +
        '([' +
        input +
        '])` should be ' +
        expected +
        ' but was ' +
        fn(input)
    );
    return false;
  }
}

function runTests() {
  const input1 = [10, 3, 5, 22, 19];
  const result1 = [22, 19, 10, 5, 3];
  const input2 = [2, 4, 6, 8];
  const result2 = [8, 6, 4, 2];

  const testResults = [
    testFunctionWorks(greatestToLeast, input1, result1),
    testFunctionWorks(greatestToLeast, input2, result2),
  ];

  const numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();
function shortWords(array) {
  return array.filter(string => string.length < 5);
}

/* From here down, you are not expected to 
   understand.... for now :)  
   
   
   Nothing to see here!
   
*/

// tests

function testFunctionWorks(fn, input, expected) {
  const result = fn(input);
  if (
    result &&
    result.length === expected.length &&
    result.every(function(item) {
      return expected.indexOf(item) > -1;
    })
  ) {
    console.log('SUCCESS: `' + fn.name + '` works!');
    return true;
  } else {
    console.error(
      'FAILURE: `' +
        fn.name +
        '([' +
        input +
        '])` should be ' +
        expected +
        ' but was ' +
        fn(input)
    );
    return false;
  }
}

function runTests() {
  const input1 = ['cat', 'oblivion', 'dog', 'patriarchy', 'blue', 'house'];
  const result1 = ['cat', 'dog', 'blue'];
  const input2 = ['rainbow', 'the', 'big', 'broom'];
  const result2 = ['the', 'big'];

  const testResults = [
    testFunctionWorks(shortWords, input1, result1),
    testFunctionWorks(shortWords, input2, result2),
  ];

  const numPassing = testResults.filter(function(result) {
    return result;
  }).length;
  console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}

runTests();