[Front-end written test question 2] From a specified array, randomly take a number each time, and it cannot be the same as the last number, that is, to avoid repetition of adjacent numbers

foreword

This article records the real questions I encountered during the written test for your reference.

1. Topic

The system is given an array, and we need to write a function. Each time the function is called, a number is randomly obtained from the array, and it cannot be the same as the number obtained last time.

2. Analysis of ideas

The array already exists, and the first thing we think of is to use Math.random() the method to obtain a random number, and the range of the random number must be within the length of the array, so that the value of the corresponding array can be obtained according to the subscript of the array.

We know that Math.random()the method is to get a random number 0 ~ 1between , including 0 but not including 1

How can I get random numbers within the range of 0 ~ array length - 1 ? And it also needs to be an integer .

Obviously, at this point we already have a random number between 0 and 1. When this random number is multiplied by the length of the array , we can get a random number between 0 and the length of the array.

The next direction is: ensure that the random number obtained each time is an integer

Math also provides three other methods: Math.floor(), Math.ceil(),Math.round()

  • Math.floor(): Round down, that is, the decimal part is discarded directly.
  • Math.ceil(): Round up, that is, discard the decimal part, and the integer part + 1
  • Math.round(): rounded, returns the rounded integer

For which method we should use: we can think about:

Math.random()The random number generated by the method contains 0, but does not contain 1, which means that when we multiply by the length of the array, the resulting tree will never contain the value of the length of the array. That is: if the array length is 5, the random number we get will never be 5.

Obviously, using Math.floor()the method , the random integer we get will always be the subscript of the array.

Key point: How to make the number obtained this time different from the last time

There are two differences here:

1. When the elements in the array are all different elements

2. When there are identical elements in the array

Therefore, the array needs to be deduplicated. Under verification, it must be ensured that there are at least three different numbers in the array.

Idea: define a variable: store the random number taken last time, and compare it every time the method is called. If it is equal, it means that it is the same as the last number taken. At this time, call the function again (recursively), if it is not equal , it returns.

3. Code example

const data = [1,2,3,4,5,6,1,2]

let lastNum = null; // 用于存放上次的取数
const getRandomNum = (arr) => {
    
                                                                                                                                                                                                                                            
	if(!arr.length) return;  // 封装方法时的习惯性写法,可不写
	let newArr = (data) => [...new Set(data)] // 去重
	if(newArr(arr).length < 3) return;   // 确保数组的不同元素超过三个
	let random = Math.floor(Math.random() * arr.length) // 获取 0~数组长度-1  的随机数
	if(lastNum == arr[random]) {
    
    
		return getRandomNum(arr)
	}
	lastNum = arr[random]
	return arr[random]
}

console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))
console.log(getRandomNum(data))

The effect is as follows:

insert image description here

Guess you like

Origin blog.csdn.net/qq_41131745/article/details/128995361