0

My objective is to determine if a field called name is empty.
My code is returning this error:

"object is not a function'.

This is resolved because I initially used (name) not [name] in the ifstatement

function notEmptyOrNull() {
    var name1 = "Sherlock";
    var name2 = "Watson";
    var name3 = "";

    var namesArray= ["Ben", "Lucy", "Watson", "Sherlock"];

    if  (namesArray[name1] !== null) {  // resolved error

        console.log(name1);
        return true;
    }
    return false;
}
notEmptyOrNull();

Updated the code:
if (namesArray(name) to if (namesArray[name] which resolved the error.

But when I tested for name3 which I have changed to an 'empty string' = ""

How do I do this ?

In Chrome Dev Tools I would like to see output as true or false for each.

I would also like to output the data of each field.

Again, this is rather simple but I am still sorting this out.

I know that I'm missing sections of the code to accomplish this.

4
  • An array is not a function, you can't do namesArray() (except in old IE), you do namesArray[name1] ! Commented Mar 18, 2015 at 16:58
  • And you probably want if ( namesArray.indexOf(name1) === -1 ) anyway Commented Mar 18, 2015 at 16:59
  • wow. that was quick and an easy fix. I am trying to do this for name2 which i renamed with a typo to return false. thanks. Commented Mar 18, 2015 at 17:01
  • @adeneo you said this "And you probably want if ( namesArray.indexOf(name1) === -1 ) anyway " and yes I do want to make use of indexOf(). But when I used this code, I am not getting any output in the console ! Commented Mar 18, 2015 at 17:12

3 Answers 3

1

What about namesArray.indexOf(name1) > -1 Like this you are not checking for null, you are checking if the value exist in the array

Sign up to request clarification or add additional context in comments.

Comments

0

You're calling the array values wrongly. The "()" are used for calling functions, to get an element from an array you have to use "[]"

Like this:

if  (namesArray[name1] !== null) {

P.D. In fact what I think that you want is to find if the name is INSIDE the array, in which case you should use this method:

if (namesArray.indexOf(name1) !== -1)

You can check the documentation here

3 Comments

wont this still fail as it's not an associative array. There needs to be an indexOf function to search the array.
@adeneo This thread took a turn. My original question was to test ' if name is null'. The thread began to suggest ways to test 'if name exists in the array. Your original response is correct but I cannot click on an arrow to upVote your answer. also. When I change this field to be an empty string name1 = "" I am 'not returning -1 or false in the console.' Do you or does anyone know why ?
@JLRishe hi. if you have a chance can you view this post, especially the post directly above this one by me. thanks.
0

Use Array.indexOf()

Replace:

 if  (namesArray(name1) !== null) { ...

With:

 if  (name1.indexOf(namesArray) !== -1 ) { ...

Array.indexOf() will return -1 if the element can't be fount in the array or else, the index of the element inside the array (where 0 represents the first element).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.