3

I have numbers array, through my_number function I create a random numer, if it already exist in the array, I call the function again and create a new random number, if it is not in the array I send it to it through push.

Unfortunately it sems to send all numbers again and again until browser crashes.

How could I fix this.

Thanks for your help

var numbers = [1,2,3,4,5];

function mainFunction(){
    function my_number(){
        var randomNumber = Math.floor((Math.random()*7)+1);
        for(let i=0; i<numbers.length; i++){
            if(numbers[i] === randomNumber){
                my_number();
            }else{
                numbers.push(randomNumber);
            }
        }
    }
    
    my_number();
}

mainFunction();

5
  • 1
    Every time you push another number onto the array, numbers.length changes. Commented Oct 5, 2020 at 2:55
  • 1
    Oh, and also you're comparing i, not numbers[i]. Commented Oct 5, 2020 at 2:55
  • I already changed i for numbers[i] and I get this in console : Maximum call stack size exceeded Commented Oct 5, 2020 at 3:00
  • 1
    @JP You can use numbers.includes(randomNumber). The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. Commented Oct 5, 2020 at 3:10
  • Checking for duplicates doesn't come for free. And you want most numbers out of a given set of numbers. That means the further you progress, the more duplicates will you probably generate, and the slower you will progress. A better approach here would be to generate a list with all possible values, shuffle it and then take the first n values from that shuffled list. Commented Oct 5, 2020 at 6:59

3 Answers 3

2

In my_number function, you have called my_number again inside for-loop and it is needed to call my_number function with the result of the for-loop.

So it is needed to get the result if the randomNumber has existed or not through for-loop and based on that, you need to decide to call the function again or not.

  1. Preferred Way

    const numbers = [1,2,3,4,5];
    
    function mainFunction(){
      function my_number(){
        const randomNumber = Math.floor((Math.random()*7)+1);
        const isExisted = numbers.some((item) => item === randomNumber);
        if (isExisted) {
          my_number();
        } else {
          numbers.push(randomNumber);
        }
      }
      my_number();
    }
    
    mainFunction();
    console.log(numbers);

  2. Regarding your CodeBase.

const numbers = [1,2,3,4,5];

function mainFunction(){
  function my_number(){
    const randomNumber = Math.floor((Math.random()*7)+1);
    let isExisted = false;
    for (let index = 0; index < numbers.length; index ++) {
      if (numbers[index] === randomNumber) {
        isExisted = true;
        break;
      }
    }
    if (isExisted) {
      my_number();
    } else {
      numbers.push(randomNumber);
    }
  }
  my_number();
}

mainFunction();
console.log(numbers);

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

Comments

2

There are quite a few problems with the code.

  • As Pointy pointed out, i should be numbers[i] to compare the array element instead of the index.
  • You have a function definition inside another function, which is fine, but the only thing the outer function does is call the inner function, so it's redundant.
  • numbers.push(randomNumber) should be done outside the for loop because it would otherwise only check if randomNumber matches the first element and ignores the rest.
  • After the call to my_number in the for loop, you need a return statement to prevent the loop from continuing after detecting failure.

var numbers = [1,2,3,4,5];

function my_number() {
    var randomNumber = Math.floor((Math.random() * 7) + 1);
    for(let i = 0; i < numbers.length; i++){
        if (numbers[i] === randomNumber){
            my_number();
            return;
        }
    }
    numbers.push(randomNumber);
}

my_number();

console.log(numbers);

Comments

1

This question should include more details and clarify the problem. According to given details, Please check the following answer.

// I have numbers array
var numbers = [1, 2, 3, 4, 5];

function mainFunction() {
    // through my_number function  
    function my_number() {
        // I create a random numer
        var randomNumber = Math.floor((Math.random() * 7) + 1);

        var isExists = numbers.includes(randomNumber);

        // if it already exist in the array
        if (isExists) {
            // I call the function again
            my_number();
        } else { // if it is not in the array
            // I send it to it through push
            numbers.push(randomNumber);
        }

    }

    my_number();
}

mainFunction();
console.log(numbers);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.