Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9036939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:07:42+00:00 2026-06-16T09:07:42+00:00

I managed to put together a code that prompts users for a list of

  • 0

I managed to put together a code that prompts users for a list of names and keeps doing so until user inputs ‘q’. Each of the entered names are added to an array and later displayed (additionally, the prompt box displays the total number of items entered).

When i run the code, it displays all entered items PLUS the item ‘undefined’ on top… and I noticed that when i get the prompt and start typing names, it goes from 0 to 2, then 3, 4, etc.

Where is the ‘undefined’ coming from, what did I do wrong?

Also, please note that while the code does run, it returns an error ‘length’ is null or not an object, but list still displays.

function getNames(){
var nameItems = new Array();
var i; 
i = 0;
var userInput; 
userInput = '';
var totalItems;
totalItems = nameItems.length;
do 
{
    if ( userInput > '') { /*performs task only if something is received from     userInput, doesn't add value to array if nothing is entered.*/
    i++;
    nameItems[i] = userInput;
    }
userInput = prompt("Enter a name to add to your favorite names list (enter \'q\'     to quit. - " + nameItems.length + " items entered.","");   
} while ...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:07:43+00:00Added an answer on June 16, 2026 at 9:07 am
    if ( userInput > '') { /*performs task only if something is received from     userInput, doesn't add value to array if nothing is entered.*/
    i++;
    nameItems[i] = userInput;
    

    should be

    if ( userInput > '') { /*performs task only if something is received from     userInput, doesn't add value to array if nothing is entered.*/
    nameItems[i] = userInput;
    i++;
    

    The nameItems[0] location is never changed otherwise.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I finally managed to put together a custom callout UIView that gets shown when
With some help from users of this site, I've managed to put together this
I'm trying to put together some jQuery code that will add form elements (input
I have managed to put this condition and rule below together to redirect a
I managed to create a dynamic three dropdown list using Javascript code and it
I've been trying to put together a script which allows a user to select,
I have a little php/mysql app I put together that takes an input form
I have two pieces of code that I need to use together. I am
Using various tutorials namely here and here I've managed to put together the following
I'm a JavaScript beginner and managed to piece together to following code. It gets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.