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 8964879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:41:13+00:00 2026-06-15T16:41:13+00:00

Javascript noob here…. I am trying to build a site that will help my

  • 0

Javascript noob here….
I am trying to build a site that will help my kids read predefined sentences from a select group, then when a button is clicked it will display one of the sentences. Is an array the best option for this?

For example, I have this array (below) and on the click of a button I would like one of these sentences to appear on the page.

<script type="text/javascript">
Sentence = new Array()
Sentence[0]='Can we go to the park.';
Sentence[1]='Where is the orange cat? Said the big black dog.';
Sentence[2]='We can make the bird fly away if we jump on something.'
Sentence[3]='We can go down to the store with the dog. It is not too far away.'
Sentence[4]='My big yellow cat ate the little black bird.'
Sentence[5]='I like to read my book at school.'
Sentence[6]='We are going to swim at the park.'
</script>

Again, is an array the best for this and how could I get the sentence to display? Ideally I would want the button to randomly select one of these sentences but just displaying one of them for now would help.

  • 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-15T16:41:14+00:00Added an answer on June 15, 2026 at 4:41 pm

    It’s simple enough to just use an array. Here is an example of doing so and retrieving a random sentence:

     var sentences = [
        'Can we go to the park.',
        'Where is the orange cat? Said the big black dog.',
        'We can make the bird fly away if we jump on something.',
        'We can go down to the store with the dog. It is not too far away.',
        'My big yellow cat ate the little black bird.',
        'I like to read my book at school.',
        'We are going to swim at the park.'
    ],
    
    //the number of sentences in the array
    maxSentences = sentences.length;
    
    //get and return a random sentences from array
    function getRandomSentence() {
        //calculate a random index
        var index = Math.floor(Math.random() * (maxSentences - 1));
        //return the random sentence
        return sentences[index];
    }
    

    To display the random sentence in a div, you can use a function like this: (note that this example uses jQuery to simplify, and to provide for cross browser usage):

    //show a random sentences in a DOM selector
    function showRandomSentence(selector){
        var randomSentence = getRandomSentence();  
        $(selector).html(randomSentence);
    }
    

    To see a working example of the above, visit this jsFiddle
    ​

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

Sidebar

Related Questions

Total noob here with javascript. I'm trying to alter a function. This is the
Javascript noob here (I'm assuming I will need Javascript to solve this). I need
javascript noob trying to figure something out. I saw a script located here: Using
javascript noob here. So I'm trying to use this s3 uploader jQuery example here
Another Vimeo-related problem from a javascript noob... I've set my problem up here: http://jsfiddle.net/ajtTF/
First timer here, and total noob when it comes to PHP and JavaScript. I
I'm a bit of a javascript noob, but I have a general question that
Noob here - I'm working on a simple Javascript calculator, and I'm using lists
A noob question here, by a novice programmer. I am trying to do a
jQuery noob here (don't most posts start like that?) I know I could do

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.