Javascript: What is best approach to generate unique numeric string? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
What is best approach out of below options:
option 1:
var d = new Date();
uniqueString = d.getTime();
option 2:
uniqueString = Math.random();

It is possible (however unlikely) that by using dates (sequential, not random) that two different instances could coincide.
The odds of an overlap from Math.random() are much lower (again possible, however unlikely).

Out of the two I would go for the second option.
While getTime() would yield 13 digits, most of them being constant in a period of weeks, random() would randomize a number with about 16 digits.
Note that if by numeric you mean digits only, then you would have to work a little more to get rid of the 0. part of the randomized number.

Related

Function that transforms a string of upvote counts into an array of numbers in JavaScript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What is the best possible way to create a function that transforms a string of upvote counts into an array of numbers. Each k represents a thousand.
transformUpvotes("6.8k 13.5k") ➞ [6800, 13500]
transformUpvotes("5.5k 8.9k 32") ➞ [5500, 8900, 32]
transformUpvotes("20.3k 3.8k 7.7k 992") ➞ [20300, 3800, 7700, 992]
Return the upvotes as an array.
Now i tried to do this myself with or without regex, the pattern i used was this /\.\d(k)/g
I first converted the string into a javascript array using array.split(' '); but i don't know how to replace the k with zeroes properly so that the k after floating point get two zeroes and a k without floating point get three zeroes.
function transformUpvotes(upvotes) {
return upvotes.split(" ").map(x => {
parsed = parseFloat(x);
return x.endsWith("k") ? parsed * 1000 : parsed;
});
}

RegEx for having minimum 3 chars also first and last char should be single quote [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Team
I would like to have a RegEx (in javascript) to validate below conditions.
Value should have atleast 3 chars
First and last char should be single quote.
please advice how the regex should be?
Thanks in advance.
If you mean three chars total:
const regex = /'.+'/;
If you mean three chars plus quotes:
const regex = /'.{3,}'/;
More info:
https://javascript.info/regexp-quantifiers

consider a string which has question marks, numbers , letters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
consider a string which has question marks, numbers , letters.
check for three question marks between two numbers where in when adding those two numbers it should be 10.In that case return it as string true or else string false.example: "bdhfr6???4hfyrt5???eee5".Above example return string true because between 6 and 4 there are 3 question marks and between 5 and 5 exactly 3 question marks
or else false
I leave the refactoring of regex on you, but this is something you can do using String.prototype.match.
function checkStr(str) {
let match = str.match(/(\d)\?{3}(\d)/);
return match && +match[1] + +match[2] === 10;
}
let out = checkStr('bdhfr6???3hfyrt5???eee5');
console.log(out)
out = checkStr('bdhfr6???4hfyrt5???eee5');
console.log(out)

Regex url parameters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I don't no how regex works. but I have a url like:
http://localhost/BetaLeren/public/dashboard/general/video.php?video=13&time=19
but I want it that way:
http://localhost/BetaLeren/public/dashboard/general/video.php?video=13
How I can i do that with refex?
or is there a better way?
If you'll always have video= followed by time=, you could use the following regex:
const link = 'http://localhost/BetaLeren/public/dashboard/general/video.php?video=13&time=19';
const updatedLink = link.match(/^.+video=\d+/)[0];
console.log(updatedLink);
^ represents the beginning of the string.
.+ represents any character 1 or more times.
\d+ represents any digit 1 or more times.

how to get the last digit from format like ip address javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Does anyone know how can I get the last digits 192.168.1.180
Example : 192.168.1.180 from this ip address i want to extract 180.
Thanks in advance
Well, if that ip address is a string you could always use the split function.
let str = "192.168.1.180";
let x = str.split(".")[3];
// where str is the ip and x is the last part you want to extract
console.log(x)
Use:
let ip = '192.168.1.180';
let last = ip.split('.')[3];
console.log(last); // '180'

Categories