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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:28:42+00:00 2026-06-07T10:28:42+00:00

Here i have an array with undefined number of elements. I tried to print

  • 0

Here i have an array with undefined number of elements. I tried to print random element of this array and cut it. Here my code.

function rand(min, max){
   return (Math.floor(Math.random() * (max - min + 1)) + min).toFixed(0);
}
$('#do').click(function(){
    var count = chamarr.length;
    var num = 0;
    if (count == 1) {
      $('#output').html('Nothing can found');
    } else {
      num = rand(1,chamarr.length);
      $('#output').html(chamarr[num]);
      chamarr.splice(num,1);
    }
 });

When I logged an array is cutted, I saw that always ok, but sometimes element is not cut!

  • 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-07T10:28:44+00:00Added an answer on June 7, 2026 at 10:28 am

    My guess is that the problem is with your randnum method:

    function rand(min, max){
       return (Math.floor(Math.random() * (max - min + 1)) + min).toFixed(0);
    }
    

    I believe this will give you a value in the range [min, max] – inclusive at both ends. (Well, actually, it will give you a string version of that value as toFixed returns a string, but when you use it later it’ll get coerced back into a number.)

    Now you’re calling it like this:

    num = rand(1,chamarr.length);
    

    So if the array is 6 elements long, you’ll get a value in the range [1, 6]. But then you’ll try to take chamarr[num] – and the range of valid indexes is [0, 5] as arrays are 0-based. If you try to take element 6, that will give you undefined – but then splicing at element 6 won’t do anything.

    I would change your rand method to be exclusive at the upper bound, like this:

    function rand(min, max) {
       return (Math.floor(Math.random() * (max - min)) + min).toFixed(0);
    }
    

    and then call it like this:

    num = rand(0, chamarr.length);
    

    That will give you a value in the right range for both indexing and splicing.

    EDIT: In response to comments etc:

    • It’s probably worth removing the toFixed(0) part of the rand function; you don’t really want a string, after all. This isn’t really part of what was wrong before, but it’s generally cleaner:

      function rand(min, max) {
         return Math.floor(Math.random() * (max - min)) + min;
      }
      
    • You might also want a version of the function that makes the 0 lower bound implicit

    • If you’re not going to use random numbers anywhere else in your code you could inline the Math.floor() / Math.random() calls instead of having a separate function, but personally I’d want to keep them well away from the “logic” code which just wants to get a random number and use it.
    • The reason I’d change the function is that having an exclusive upper bound is much more common in computer science – it typically goes along with 0-indexing for things like collections. You typically write for loops with inclusive lower bounds and exclusive lower bounds, etc.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code right here, where the $friends variable is an array with
i have an array which could look like this: $config[$name][$array]['key'] = 'value'; // here
I have an array but something goes wrong with it, here is my code
Noob question here! I have an array with hashes that looks like this: arr
I have this $data array: (built on a shell, not a form) (debugged here)
I have some problem to order an array by a field of this, here
I have a JavaScript class defined as shown here: function Item() { this.init(); }
According to my small tests this code works. But, does it have undefined behaviour?
var Items = { FormVariables: function() { if (this.array === 'undefined') { this.array =
Here is my problem: I have an array of model class(Let's say, 'addressModel' with

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.