0

I am getting an error on the last two functions, but to me they look fine.

// This is your starting array.
const arr = [10, 10, 16, 12];

function returnFirst(arr) {
  // Return the first item from the array.
  return arr[0];
  
  console.log(arr[0]);
}

function returnLast(arr) {
  // Return the last item of the array.
  return arr[arr.length - 1];
  
  console.log(arr[arr.length - 1]);
}

function getArrayLength(arr) {
  // Return the length of the array.
  return arr.length
  
  console.log(arr.length);
}

function incrementByOne(arr) {
  // `arr` is an array of integers (numbers). Increment all items in the array by
  // Return the array.
  return arr.map(value => value + 1);
  
  console.log(arr);
}

function addItemToArray(arr, item) {
  // Add the parameter `item` to the end of the array `arr`.
  // Return the array // This is my issue 1.
  return arr.push(10);
  
  console.log(arr);
}

function addItemToFront(arr, item) {
  // Add the parameter `item` to the front of the array `arr`.
  // Return the array.
  // Hint: use the array method `.unshift` // This my issue 2.
  return arr.unshift(10);
  
  console.log(arr);
}

// Uncomment these lines to check results in browser console.
console.log("returnFirst result:" + returnFirst(arr))
console.log("returnLast result:" + returnLast(arr))
console.log("getArrayLength result:" + getArrayLength(arr))
console.log("incrementByOne result:" + incrementByOne(arr))
console.log("addItemToArray result:" + addItemToArray(arr, 10))
console.log("addItemToFront result:" + addItemToFront(arr, 10))

//////////////////////////
// Don’t change this line.
if (typeof module !== 'undefined') {
  module.exports = {
    returnFirst,
    returnLast,  
    getArrayLength,
    incrementByOne,
    addItemToArray,
    addItemToFront,
  };
}

This is the error code that spits out. I ran this on the console and everything shows fine. But class uses next tech. Maybe someone has more insight into this.

addItemToArray(arr, item) › should return the array with the item added to the end

expect(received).toEqual(expected) // deep equality

Expected: [10, 10, 16, 12, 10]
Received: 5

  41 |   describe('addItemToArray(arr, item)', function() {
  42 |     it('should return the array with the item added to the end', function() {
> 43 |       expect(addItemToArray([10, 10, 16, 12], 10)).toEqual([10, 10, 16, 12, 10]);
     |                                                    ^
  44 |   
  45 |       expect(addItemToArray([], true)).toEqual([true]);
  46 |     });

  at Object.<anonymous> (nt-test.js:43:52)
Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Dan
  • 1

0 Answers0