CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2021-04-22
by atx

Original Post

Original - Posted on 2011-04-23
by Tom Wadley



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Basically, you need to remove an element from the original array on every iteration and then get a random number with a max value equals to the size of filtered array.
Try this code (obviously it can be improved, eg copying original array ecc):


function removeItemOnce(arr, value) { var index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); } return arr; }

function newCustomer() { showElement("customerPic"); for (var i = 0; i < 3; i++) { let ix = randomNumber(0, toppings.length); let v = toppings[ix]; toppings = removeItemOnce(toppings,v) console.log("I would like " + v + " on my pizza, please"); } }

More info about array item removing: https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array
Find the `index` of the array element you want to remove using [`indexOf`][2], and then remove that index with [`splice`][1].
> The splice() method changes the contents of an array by removing > existing elements and/or adding new elements.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5); if (index > -1) { array.splice(index, 1); }
// array = [2, 9] console.log(array);
<!-- end snippet -->
The second parameter of `splice` is the number of elements to remove. Note that `splice` modifies the array in place and returns a new array containing the elements that have been removed. ___ For the reason of completeness, here are functions. The first function removes only a single occurrence (i.e. removing the first match of `5` from `[2,5,9,1,5,8,5]`), while the second function removes all occurrences:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function removeItemOnce(arr, value) { var index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); } return arr; }
function removeItemAll(arr, value) { var i = 0; while (i < arr.length) { if (arr[i] === value) { arr.splice(i, 1); } else { ++i; } } return arr; } // Usage console.log(removeItemOnce([2,5,9,1,5,8,5], 5)) console.log(removeItemAll([2,5,9,1,5,8,5], 5))
<!-- end snippet -->
In TypeScript, these functions can stay type-safe with a type parameter:
```typescript function removeItem<T>(arr: Array<T>, value: T): Array<T> { const index = arr.indexOf(value); if (index > -1) { arr.splice(index, 1); } return arr; } ```
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf


        
Present in both answers; Present only in the new answer; Present only in the old answer;