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

  • Home
  • SEARCH
  • 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 8854529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:54:08+00:00 2026-06-14T13:54:08+00:00

I have finally gotten around to creating my first little practice program in Javascript.

  • 0

I have finally gotten around to creating my first little practice program in Javascript. I know it’s not elegant as it could be. I have gotten most of this code to work, but I still get an “undefined” string when I run it a few times. I don’t know why. Would someone be kind enough to explain to me where this undefined is coming from?

var work = new Array();
work[1] = "product design";
work[2] = "product system design";
work[3] = "product social media post x5";
work[4] = "product Agent Recruitment system design";
work[5] = "product profile system design";
work[6] = "product Agent testing design";
work[7] = "product customer support";
work[8] = "product promotion";
var course = new Array();
course[1] = "javascript";
course[2] = "mandarin";
course[3] = "javascript practical-Code Academy";
course[4] = "javascript practical-learn Street";
course[5] = "mandarin practical-memrise";
course[6] = "new stuff with audiobooks";
var activity = new Array();
activity[1] = "listen to podcasts";
activity[2] = "chat online";
activity[3] = "Exercise";
activity[4] = "take a walk";
activity[5] = "call a friend";
var picker1 = Math.floor(Math.random()*3+1);
var picker2 = Math.floor(Math.random()*work.length+1);
var picker3 = Math.floor(Math.random()*course.length+1);
var picker4 = Math.floor(Math.random()*activity.length+1);
var group_pick = function(){
  if(picker1 === 1){
    return "Time to work on ";
  } else if(picker1 === 2){
    return "Time to learn some ";
  } else if (picker1 === 3){
    return "Lets relax and ";
  } else {
    return "error in group_pick";
  }
};
var item_pick = function() {
  if (picker1 === 1) {
    return work[picker2] ;
  } else if (picker1 === 2) {
    return course [picker3] ;
  } else if (picker1 === 3) {
    return activity[picker4] ;
  } else {
    return "error in item_pick";
  }
};
var task = group_pick() + item_pick();
document.write(task);
  • 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-14T13:54:12+00:00Added an answer on June 14, 2026 at 1:54 pm

    Array’s start with an index of zero. When you assign a value to the 1 index, a 0 index is created you, with no value (undefined).

    var arr = new Array();
    arr[1] = 'hi!';
    console.log(arr); // [undefined, "hi!"]
    console.log(arr.length) // 2
    

    Length is 2, check that out. You thought you had one item in that array but length is 2.

    Usually it’s easier to not manage the array indices yourself. And the array literal syntax is usually preferred for a number of reasons.

    var arr = [];
    arr.push('hi!');
    console.log(arr); // ["hi!"]
    console.log(arr.length) // 1
    

    Or just create the array with the items in it directly, very handy.

    var arr = [
      "hi",
      "there!"
    ];
    console.log(arr); // ["hi", "there"]
    console.log(arr.length) // 2
    

    Once you are making the arrays properly, you can get a random item with simply:

    var arr = ['a','b','c'];
    var index = Math.floor(Math.random() * arr.length);
    console.log(arr[index]); // "a", "b" or possibly "c"
    

    This works because var index will be calculated by a random value of between 0.0 and up to but not including 1.0 times 3 (the length of the array). Which can give you a 0, 1 or a 2.

    So this arr right here, has 3 items, one at 0, one at 1, and one at 2.

    Learning to address arrays from zero can be mentally tricky. You sort of get used to it. Eventually.


    A working example using these tips here: http://jsfiddle.net/du5Jb/

    I changed how the arrays are declared, and removed the unneeded +1 from var pickerX calculations.

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

Sidebar

Related Questions

I have finally started messing around with creating some apps that work with RESTful
I have finally gotten my first app to work properly and I'm attempting to
I have finally gotten my login application to function with my online database. I
So, I've finally made the plunge, and have gotten to the state where I'm
I have finally gotten the ReportViewer to work in Local mode. Now I would
I'm still writing my financial software :P And have finally gotten a reliable real
I've finally gotten around to learning Java and am looking for some documentation for
My first real foray into using PHP OOP and PDO extensively. I have finally
I have finally gotten my Flash Builder to look at my remote services but
I have finally gotten all of my functions to work after testing the insert

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.