Home > Back-end >  How to return only one array in a function if searching for the first and last array and the given a
How to return only one array in a function if searching for the first and last array and the given a

Time:07-21

sorry if this doesn't make sense. Im very, very new to this.

I am trying to solve a problem where it returns the First and Last value of an array. I feel like I have got the foundations right with this code. e.g:

function getFirstAndLast(array) {
return [array.shift(), array.pop()] 
}
let newArray = []
newArray = getFirstAndLast([5, 10])

console will print: [5, 10]

However, when given just 1 argument the console will return the value and undefined. e.g:

function getFirstAndLast(array) {
return [array.shift(), array.pop()] 
}
let newArray = []
newArray = getFirstAndLast([5])

console will now print: [5, undefined]

Question is how do I change my code to only return the single value without the undefined but still retain the ability to pull the First and Last array value if given 2 or more arguments.

again sorry if this makes no sense or if i have mistakenly called something wrong. Thanks

CodePudding user response:

Just use length, if length is 1 return same number like:

function getFirstAndLast(array) {
return (array.length === 1) ? [array[0], array[0]] : [array.shift(), array.pop()];
}
console.log(getFirstAndLast([5]));
console.log(getFirstAndLast([5,10]));
console.log(getFirstAndLast([5,10,15]));


Better check for empty value like:

function getFirstAndLast(array) {
  const lenArr = array.length;
  if (lenArr === 0) {
    return [];
  } else {
    return (lenArr === 1) ? [array[0], array[0]] : [array.shift(), array.pop()];
  }
}
console.log(getFirstAndLast([]));
console.log(getFirstAndLast([5]));
console.log(getFirstAndLast([5, 10]));
console.log(getFirstAndLast([5, 10, 15]));

Reference:

CodePudding user response:

From the above question I am getting the impression that you want to get the first and the last value of an array. I hope this will work.

var myArray = [5];

function firstAndLast(array) {

var firstItem = myArray[0];
var lastItem = myArray[myArray.length-1];

var objOutput = {};
objOutput[firstItem]=lastItem

return objOutput;
}

var display = firstAndLast(myArray);

console.log(display);

CodePudding user response:

I would use approach without mutating the array and take for smaller arrays slicing and for larger array the indices.

function getFirstAndLast(array) {
    return array.length < 3
        ? array.slice(0, array.length)
        : [array[0], array.at(-1)];
}

console.log(getFirstAndLast([]));        // []
console.log(getFirstAndLast([5]));       // [5]
console.log(getFirstAndLast([3, 5]));    // [3, 5]
console.log(getFirstAndLast([1, 3, 5])); // [1, 5]
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

Try this :

function getFirstAndLast(array) {
    return array.length > 2 ? [array[0], array.at(-1)] : array
}

console.log(getFirstAndLast([1, 2, 3, 4, 5]));

  • Related