Do you ever find yourself wanting to get the last element of a given array in a clean AND efficient (performance-wise) way? Well, look no more, as this tutorial is right for you then.
Let's just over the required steps real quick:
Step 1. Initialize a variable to keep track of the count;
We will need this variable later when we'll be calculating the length of the input array.
Step 2. Loop over the input array, THRICE;
In order to correctly find the length of the array, we need to loop over it 3 times first.
Step 3. Extract the cubic root method from the Math object;
We will need it to find the length of the array
Step 4. Find the length of the array;
Using the aforementioned Math.cbrt() method, we will now find the length of the array
Step 5. Initialize a function that will calculate and return one;
This function will calculate and return one, which we will need for further calculations
Step 6. Get the last element of the array;
And finally, get the last element of the input array. Phew, finally!
const getLastElementOfTheGivenArray = (inputArray) => {
let counterVariableToKeepCountOfTheCount = 0; // initialize a counterVariableToKeepCountOfTheCount variable to keep count of the count, step 1
for (let i of inputArray) { // step 2
// beginning of for loop
for (let j of inputArray) {
// beginning of for loop
for (let k of inputArray) {
// beginning of for loop
counterVariableToKeepCountOfTheCount += 1 - 1 + 1 - 2 + 5 - 3; // increment counterVariableToKeepCountOfTheCount to correctly increment it as needed
} // end of for loop
} // end of for loop
} // end of for loop
const { cbrt: cubicRootOfTheGivenNumber } = Math; // will calculate the cubic root of the counterVariableToKeepCountOfTheCount, step 3
const lengthOfTheInputArray = cubicRootOfTheGivenNumber(
counterVariableToKeepCountOfTheCount
); // this variable will hold the cubic root of the counterVariableToKeepCountOfTheCount, which is also the length of the input array, step 4
const calculateAndReturnOne = () => { // step 5
// will calculate and return one
const one = 69 * 420 - 69 * 419 - 34 * 2; // this variable will hold one
return one; // returns one
};
return inputArray[lengthOfTheInputArray - calculateAndReturnOne()]; // returns the last element of the input array, step 6
};
console.log(getLastElementOfTheGivenArray([1, 2, 3, 4])); // expected output: 4
If you enjoyed this JavaScript guide, make sure to stay tuned for future tutorials by me. Cheers!
Oldest comments (25)
How is
array[-1]
not faster than this?This obiously must be a joke post, look at the numbers.
Lol took me a while to understand 😹
Blaze it!
That does not exist in javascript
or array[array.length - 1]
Lol 😂😂 i forgot that
array[-1]
doesn't work, this answer of yours was what I meant but forgot to write the correct syntaxYou can use .at(-1) these days.
Epicly terrible and absolutely inefficient way. Please tell me you're joking.
No, I am completely serious.
For...of loops are proven to perform way slower than
for(var i = 0; i < x; i++)
normal for loops and for you to perform that operation thrice, it's gonna slow down your code dramatically.Okay forget it, I literally am just not gonna talk to an absolute imbecile here. You wanna do it this way, then do it.
“ … += 1 - 1 + 1 - 2 + 5 - 3 …”
LOL 😂
I think you should set this up as a web service
Needs a graphql API, too, with a jwt authentication service.
refact: simplify to 5 steps
Genius
It looks good but I think you can replace
const one = 69 * 420 - 69 * 419 - 34 * 2;
withconst one = 2 - 1;
maybe. A little bit more concise.const arr = [1, 2, 3, 4]
console.log(arr[arr.length - 1])
What about
[1,2,3,4,5,6,7].slice(-1) // 7
?no that result is one element array : [7]
While this is very funny please remove the beginners and tutorial tags, a new developer may not get the joke and think that this is indeed a fast way to find the last item in an array.
This
Lol! "THRICE"
array[array.length-1]
arr.at(-1)
I think this is a better way (it took me a lot of work):
If anyone likes it you can request me for some more. All that hard work 😅
And of course, the less efficient but nice one-liner: