JavaScript Array verification and finding the last item
up vote
0
down vote
favorite
I want to create a function that accepts an array and returns the last item in the array.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr.isArray ===true) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
I write this code and I get this message in the console:
Failed to load resource: net::ERR_FILE_NOT_FOUND
instead of the result
javascript arrays
|
show 2 more comments
up vote
0
down vote
favorite
I want to create a function that accepts an array and returns the last item in the array.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr.isArray ===true) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
I write this code and I get this message in the console:
Failed to load resource: net::ERR_FILE_NOT_FOUND
instead of the result
javascript arrays
3
Your code has typo. Usedlenghth
– Mohammad
Nov 13 at 9:23
2
Why are you returningconsole.log()
? Isn't it strange? lots of errors too...
– Jai
Nov 13 at 9:24
Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26
FYI:isArray
is a static method and should be called asArray.isArray
– hindmost
Nov 13 at 9:26
I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to create a function that accepts an array and returns the last item in the array.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr.isArray ===true) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
I write this code and I get this message in the console:
Failed to load resource: net::ERR_FILE_NOT_FOUND
instead of the result
javascript arrays
I want to create a function that accepts an array and returns the last item in the array.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr.isArray ===true) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
I write this code and I get this message in the console:
Failed to load resource: net::ERR_FILE_NOT_FOUND
instead of the result
javascript arrays
javascript arrays
edited Nov 13 at 9:27
Ivan
5,17231339
5,17231339
asked Nov 13 at 9:22
Hichem Bel Fekih
31
31
3
Your code has typo. Usedlenghth
– Mohammad
Nov 13 at 9:23
2
Why are you returningconsole.log()
? Isn't it strange? lots of errors too...
– Jai
Nov 13 at 9:24
Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26
FYI:isArray
is a static method and should be called asArray.isArray
– hindmost
Nov 13 at 9:26
I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27
|
show 2 more comments
3
Your code has typo. Usedlenghth
– Mohammad
Nov 13 at 9:23
2
Why are you returningconsole.log()
? Isn't it strange? lots of errors too...
– Jai
Nov 13 at 9:24
Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26
FYI:isArray
is a static method and should be called asArray.isArray
– hindmost
Nov 13 at 9:26
I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27
3
3
Your code has typo. Used
lenghth
– Mohammad
Nov 13 at 9:23
Your code has typo. Used
lenghth
– Mohammad
Nov 13 at 9:23
2
2
Why are you returning
console.log()
? Isn't it strange? lots of errors too...– Jai
Nov 13 at 9:24
Why are you returning
console.log()
? Isn't it strange? lots of errors too...– Jai
Nov 13 at 9:24
Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26
Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26
FYI:
isArray
is a static method and should be called as Array.isArray
– hindmost
Nov 13 at 9:26
FYI:
isArray
is a static method and should be called as Array.isArray
– hindmost
Nov 13 at 9:26
I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27
I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27
|
show 2 more comments
4 Answers
4
active
oldest
votes
up vote
0
down vote
accepted
Try this
function getLastItem() {
var arr=["hichem","amine", 2 , 5];
if (arr instanceof Array){
return arr[arr.length-1];
}
}
add a comment |
up vote
2
down vote
If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.
You have used the wrong syntax.
It is Array.isArray(arr).
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (Array.isArray(arr)) {
return (arr[arr.length-1]);
}
return "The entered data is not an Array";
}
console.log(getLastItem(test));
1
It can be just writtenArray.isArray(arr)
.
– chŝdk
Nov 13 at 9:40
It'd be better to moveconsole.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
– hindmost
Nov 13 at 9:47
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
add a comment |
up vote
0
down vote
All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
var isArray=(arr instanceof Array); //or test.constructor === Array
if (isArray) {
return (arr[arr.length-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
add a comment |
up vote
0
down vote
- arr.isArray is undefine, you can coding like this:
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Try this
function getLastItem() {
var arr=["hichem","amine", 2 , 5];
if (arr instanceof Array){
return arr[arr.length-1];
}
}
add a comment |
up vote
0
down vote
accepted
Try this
function getLastItem() {
var arr=["hichem","amine", 2 , 5];
if (arr instanceof Array){
return arr[arr.length-1];
}
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Try this
function getLastItem() {
var arr=["hichem","amine", 2 , 5];
if (arr instanceof Array){
return arr[arr.length-1];
}
}
Try this
function getLastItem() {
var arr=["hichem","amine", 2 , 5];
if (arr instanceof Array){
return arr[arr.length-1];
}
}
answered Nov 13 at 9:38
Prakash N
347
347
add a comment |
add a comment |
up vote
2
down vote
If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.
You have used the wrong syntax.
It is Array.isArray(arr).
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (Array.isArray(arr)) {
return (arr[arr.length-1]);
}
return "The entered data is not an Array";
}
console.log(getLastItem(test));
1
It can be just writtenArray.isArray(arr)
.
– chŝdk
Nov 13 at 9:40
It'd be better to moveconsole.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
– hindmost
Nov 13 at 9:47
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
add a comment |
up vote
2
down vote
If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.
You have used the wrong syntax.
It is Array.isArray(arr).
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (Array.isArray(arr)) {
return (arr[arr.length-1]);
}
return "The entered data is not an Array";
}
console.log(getLastItem(test));
1
It can be just writtenArray.isArray(arr)
.
– chŝdk
Nov 13 at 9:40
It'd be better to moveconsole.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
– hindmost
Nov 13 at 9:47
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
add a comment |
up vote
2
down vote
up vote
2
down vote
If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.
You have used the wrong syntax.
It is Array.isArray(arr).
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (Array.isArray(arr)) {
return (arr[arr.length-1]);
}
return "The entered data is not an Array";
}
console.log(getLastItem(test));
If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.
You have used the wrong syntax.
It is Array.isArray(arr).
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (Array.isArray(arr)) {
return (arr[arr.length-1]);
}
return "The entered data is not an Array";
}
console.log(getLastItem(test));
edited Nov 13 at 9:50
answered Nov 13 at 9:32
Vijay Pushkin
714
714
1
It can be just writtenArray.isArray(arr)
.
– chŝdk
Nov 13 at 9:40
It'd be better to moveconsole.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
– hindmost
Nov 13 at 9:47
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
add a comment |
1
It can be just writtenArray.isArray(arr)
.
– chŝdk
Nov 13 at 9:40
It'd be better to moveconsole.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
– hindmost
Nov 13 at 9:47
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
1
1
It can be just written
Array.isArray(arr)
.– chŝdk
Nov 13 at 9:40
It can be just written
Array.isArray(arr)
.– chŝdk
Nov 13 at 9:40
It'd be better to move
console.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.– hindmost
Nov 13 at 9:47
It'd be better to move
console.log
out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.– hindmost
Nov 13 at 9:47
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
@hindmost Thanks for the suggestion! Now the answer will give output
– Vijay Pushkin
Nov 13 at 9:51
add a comment |
up vote
0
down vote
All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
var isArray=(arr instanceof Array); //or test.constructor === Array
if (isArray) {
return (arr[arr.length-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
add a comment |
up vote
0
down vote
All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
var isArray=(arr instanceof Array); //or test.constructor === Array
if (isArray) {
return (arr[arr.length-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
add a comment |
up vote
0
down vote
up vote
0
down vote
All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
var isArray=(arr instanceof Array); //or test.constructor === Array
if (isArray) {
return (arr[arr.length-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
var isArray=(arr instanceof Array); //or test.constructor === Array
if (isArray) {
return (arr[arr.length-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
answered Nov 13 at 9:34
Negi Rox
1,5271511
1,5271511
add a comment |
add a comment |
up vote
0
down vote
- arr.isArray is undefine, you can coding like this:
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
add a comment |
up vote
0
down vote
- arr.isArray is undefine, you can coding like this:
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
add a comment |
up vote
0
down vote
up vote
0
down vote
- arr.isArray is undefine, you can coding like this:
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
- arr.isArray is undefine, you can coding like this:
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
let test=["hichem","amine", 2 , 5];
function getLastItem(arr) {
if (arr instanceof Array) {
return console.log(arr[arr.lenghth-1]);
}
return console.log("The entered data is not an Array");
}
getLastItem(test);
answered Nov 13 at 9:47
bolt
782
782
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277646%2fjavascript-array-verification-and-finding-the-last-item%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Your code has typo. Used
lenghth
– Mohammad
Nov 13 at 9:23
2
Why are you returning
console.log()
? Isn't it strange? lots of errors too...– Jai
Nov 13 at 9:24
Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26
FYI:
isArray
is a static method and should be called asArray.isArray
– hindmost
Nov 13 at 9:26
I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27