1

I am receiving in a function an array with defined values and others to be defined.


["y", null, null, null, null, null, null, null, null]

What I am trying to do is that every time I receive this array I check the values that are at null and to one of them, chosen in a random way, I want to set the value "Z" and return the array modified.

For this I am doing the following in my function:

    nextValue(array) {
        
        const random = Math.floor(Math.random() * array.length);        
    }
};

At this point , which I have found in other references , if I print the value "random = Nan" and "array[random] = undefined".

How could I modify the value ?

Would there be a better method without the need to extract the value, and modify it directly?

3 Answers 3

2

It's really not clear what exactly you want. Assuming the task is "take an array with elements both null and some other values, flip one random null to some preset string, do that in place", here's what you might use:

function flipRandomNull(arrayWithNulls, character) {
  const indexesOfNulls = arrayWithNulls.reduce((acc, item, index) => {
    return item === null ? [...acc, index] : acc;
  }, []);

  if (!indexesOfNulls.length) return; // no nulls for ya, nothing to flip

  const indexToFlip = indexesOfNulls[ Math.random() * indexesOfNulls.length | 0];
  arrayWithNulls[ indexToFlip ] = character;
}

const initialField = ["y", null, null, null, null, null, null, null, null];

[...Array(initialField.length - 1)].forEach(() => {
  flipRandomNull(initialField, 'Z');
  console.log(initialField.join(' | '));
});

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

Comments

2

You may use array.map and array.filter methods to find out the index of null items in your array, then fill a random location of your main array with "z":

const arr = ["y", null, null, null, null, null, null, null, null];

const nextValue = (orgArr) => {
    const nullLocations = orgArr
        .map((item, idx) => (item === null ? idx : null))
        .filter((item) => item !== null);
    orgArr[nullLocations[Math.floor(Math.random() * nullLocations.length)]] = "z";
    return orgArr;
};

console.log(nextValue(arr));

Comments

0

Here is my take on this...

let arr = ["y", null, null, null, null, "y", null, null, null];
let size = arr.length;
// size of array

function random() {
  var random = Math.floor(Math.random() * size + 1)
  //generate radnom number betwen 0 and size of array
  if (arr[random] === null) {
    arr[random] = "z";
    // if random array  is null set z
    return size = 0;
    //stop loop
  } else {
    random = Math.floor(Math.random() * size + 1);
    // if not set new radnom number
  }
}

var i = 0;
while (i < size) {
  random()
  i++;
}
// loop function untill it sets radnom array null to z
console.log(arr)

Plus even shorter function:

function random() {
  var random = Math.floor(Math.random() * size + 1)
  arr[random] === null ? (arr[random] = "z", size = 0 ) : (random = Math.floor(Math.random() * size + 1));  
}

let arr = ["y", null, "y", "y", null, "y", null, null, null];
let size = arr.length;


var i = 0;
while (i < size) {
  random()
  i++;
}

function random() {
  var random = Math.floor(Math.random() * size + 1)
  arr[random] === null ? (arr[random] = "z", size = 0, console.log("set, " + (i + 1) + " trys")) : (random = Math.floor(Math.random() * size + 1));
}


console.log(arr)
.as-console-wrapper {
  min-height: 100%
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.