A:
You can use the following regex:
[a-zA-Z0-9]*$
Which matches any string that begins with a letter, followed by zero or more characters, and ends with a letter or a decimal digit.
You may need to update your code to use '+' instead of '*' in the regex, for example:
var regex = /[a-zA-Z0-9]+$/;
var items = item.match(regex);
In your case, you would probably want to add a check for the string being a number first.
Q:
Append index value of array to my own random number generator
I'm trying to make a random Number Generator. Basically I want to append the index value of the array to my own random number generator. It gives the correct number(index) so that's not the problem.
The actual problem is that I can't use the ".indexOf" to append my array, because I have to make a string. I can't use the '.concat' because the Number Generator doesn't work like that.
I want to make something like this:
myArray[myRandomNumberGenerator] = myArray[index];
How could I do that?
var numbers = ['1','2','3','4','5','6','7','8','9','10'];
function randomNumberGenerator() {
var number = Math.floor(Math.random() * 10);
var myArray = numbers.concat([number]);
console.log(myArray[myArray.indexOf(number)]);
}
randomNumberGenerator();
A:
Just generate a random number (not an array index), and increment the same in the loop
var numbers = ['1','2','3','4','5','6','7','8','9','10'];
function randomNumberGenerator() {
var number = Math.floor(Math.random() * 10);
while (numbers.indexOf(number) > -1) number = Math.floor(Math.random() * 10);
return number;
}
console.log(randomNumberGenerator()) be359ba680
Related links:
Comments