JavaScript How to exclude certain values from randomly generated array exclude certain values from pivot table,exclude certain values in

I have written a function which generates an array with 10 randomly generated unique Values.

I would like to extend it such that certain values are not included.

This is my code:

let excludedValues = [20,32,40,46,64,66,14,30,34];
let randomArray = [];

  while(randomArray.length < 10){
    let randomValue = Math.floor(Math.random() * 100) + 1;

    let valueNotAllowed = excludedValues.includes(randomValue);
    while (valueNotAllowed) {
      randomValue = Math.floor(Math.random() * 100) + 1;
      valueNotAllowed = excludedValues.includes(randomValue);
    }

    if(randomArray.indexOf(randomValue) === -1) randomArray.push(randomValue);
}
console.log(randomArray);

It creates an array of length 10 with unique values between 1 and 100 but ii still includes the values in the excludedValues Array

How can I get it to work that those values do not end up in the array

Thanks in advance

Answer:1

As an alternative to already posted answer you can use of Set

function getUniqueRandomValues(exclusion = []) {
  // By definition, values inside of a Set are unique
  const randomArray = new Set();

  do {
    const randomValue = Math.floor(Math.random() * 100) + 1;

    // If the exclusion rule is satisfied push the new value in the random array
    if (!exclusion.length || !exclusion.includes(randomValue)) {
      randomArray.add(randomValue);
    }
  } while (randomArray.size < 10);

  return Array.from(randomArray);
}

console.log(getUniqueRandomValues([20, 32, 40, 46, 64, 66, 14, 30, 34]));
Answer:2

You could take a single loop and omit code repetition.

const excludedValues = [20, 32, 40, 46, 64, 66, 14, 30, 34],
    randomArray = [];

while (randomArray.length < 10) {
  const randomValue = Math.floor(Math.random() * 100) + 1;

  if (excludedValues.includes(randomValue) || randomArray.includes(randomValue)) continue;
  randomArray.push(randomValue);
}

console.log(...randomArray);
Answer:3

How to configure IntelliSense in Visual Studio Code? For example, lets consider the below code snippet. 'use strict'; import React from 'react'; import ReactDOMServer from 'react-dom/server'; I ...

How to configure IntelliSense in Visual Studio Code? For example, lets consider the below code snippet. 'use strict'; import React from 'react'; import ReactDOMServer from 'react-dom/server'; I ...

  1. configure intellisense visual studio code c++
  2. configure intellisense visual studio
  3. configure intellisense visual studio code
  4. enable intellisense visual studio 2017
  5. enable intellisense visual studio
  6. enable intellisense visual studio code
  7. enable intellisense visual studio 2019
  8. enable intellisense visual studio 2015
  9. enable intellisense visual studio 2013
  10. enable intellisense visual studio 2012
  11. enable intellisense visual studio javascript
  12. intellisense settings visual studio
  13. intellisense settings visual studio 2017
  14. intellisense settings visual studio 2019

When you click the link apply any CSS animation during show/hide toggle. The toggle works, but animation is not applied. $('.working-hours-link').click(function(e) { $(this).siblings(".hours")....

When you click the link apply any CSS animation during show/hide toggle. The toggle works, but animation is not applied. $('.working-hours-link').click(function(e) { $(this).siblings(".hours")....

  1. transition doesn't work
  2. transition doesn't work with height auto
  3. transition doesn't work in firefox
  4. transition doesn't work in safari
  5. transition doesn't work in ie
  6. transition doesn't work javascript
  7. transition doesn't work html
  8. does not transition
  9. vue transition doesn't work
  10. css transition doesn't work first time
  11. transition delay doesn't work
  12. transition opacity doesn't work
  13. transition width doesn't work
  14. css transition doesn't ease out
  15. css transition doesn't work in chrome
  16. css transition doesn't work react
  17. transition height doesn't work
  18. hover transition doesn't work
  19. transition background doesn't work
  20. d3 transition doesn't work

I have an Javascript array as below var data = [{ "Ticket_id": "239248", "Order_Issue": "SAP", "Region": "EU", "Line_No": "10", "...

I have an Javascript array as below var data = [{ "Ticket_id": "239248", "Order_Issue": "SAP", "Region": "EU", "Line_No": "10", "...

  1. group array items javascript
  2. group array items by pair ruby
  3. group array items using object
  4. group array items php
  5. group array items
  6. javascript group array items by property
  7. js group array items
  8. swift group array items
  9. linq group array items
  10. jquery group array items
  11. typescript group array items

I'm new user of ReactJS and I bumped in a point. I'm creating a generic map component, it works perfectly. However, only can render one component by page. I would like to instead use getElementById, I ...

I'm new user of ReactJS and I bumped in a point. I'm creating a generic map component, it works perfectly. However, only can render one component by page. I would like to instead use getElementById, I ...