-2

i am backend PHP Developer and recently i have been trying to learn basic JavaScript as i am planning to work as Full stack developer, it's (hot in Market). I have this question which i can resolve in PHP easily but find it really hard to solve in JS, as js doesn't support built in functions for array like range(), shuffle and blah blah. so here's my simple question

Q : Write a JavaScript function to generate a random array of 500 integers (values of 1 – 500 inclusive). Randomly remove and discard an arbitrary element from this newly generated array. Write the code to efficiently determine the value of the missing element.

I have solved this in PHP which i am pasting here too in case.

// creating array with a range from 1-500
$array_range = range(1, 500);

shuffle($array_range);

$array_shuffle = $array_range;

$remove_elem = rand(1, 500);

unset($array_shuffle[$remove_elem]);

$missing_elem = array_diff($array_range, $array_shuffle);

echo 'Value of missing element is : '.current($missing_elem);

Can someone help me with this how can i solve this? P.S thanks in advance and sorry for long question

Manuel Abascal
  • 5,616
  • 5
  • 35
  • 68
Messi
  • 7
  • 2

2 Answers2

0

For this problem, you just need to do step by step. - Random in javascript you have the function Math.random

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

  • You know all language have for loop or similary search for loop in javascript

  • Remove an element in array. we have many methods for it Array.splice, Array.slice or Array.delete

Linh Nguyen
  • 925
  • 1
  • 10
  • 23
0

Let's divide this into sub problems

Problem #1, generate an array of random Ints --> Create an array with random values

Problem #2, generate a random number in the range ---> Generating random whole numbers in JavaScript in a specific range?

Problem #3, remove element at that index

array.splice(index, 1)

Please note that splice will return the removed elements, it's removing function is a side effect, so don't assign this to a variable

Problem #4, get the diff

arrA.filter(x => !arrB.includes(x));

Putting them all together

function getRandomInt(min, max) {
   min = Math.ceil(min);
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min + 1)) + min;
}

var a = Array.from({length: 500}, () => Math.floor(Math.random() * 500));

var rnd = getRandomInt(0, 500);

var tempA = [...a]; //this makes a copy of a, otherwise splice would act on a
tempA.splice(rnd, 1);

function getDiff(arrA, arrB){
  return arrA.filter(x => !arrB.includes(x));
}

var result = getDiff(a, tempA)

A few notes about this solution

1) The random array generation here might not guarantee uniqueness. If you want unique, look at the accepted answer in the question that uses the Fisher-Yates Shuffle

2) The main inefficiency here is (obv after random number generation and shuffling), is the copying of the array. I neither like JS nor use it so you might want to dig into how to work around the pointer logic in JS

3) Since you want to be a fullstack engineer, things that are computationally heavy such as generating a large quantity of random numbers, is better done in the server

Hope this helps

sinanspd
  • 2,589
  • 3
  • 19
  • 37