Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Looking for Javascript program. Without using Math.random() method in Javascript. How do generate random no.
You can use getTime() for that like that, if length doesn't matter for your, if it does, use .slice() method but before using it convert it in string using .toString()
const randomNumber = parseInt(new Date().getTime())
console.log(randomNumber)
use timestamp
Math.floor(Date.now() /1000);
Without Math library
Date.now() /1000
Use this for 3 digit random number
console.log((new Date().getMilliseconds()));
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 15 days ago.
Improve this question
I have other ways of doing the same thing .. So I am not looking for a different solution to this problem ... I am looking for an explanation as to why if I have defined a integer, it still concatenates with .map as if it were a string.
I have a basic set of data retrieved from an API:
"data":["8","8","12","1","7","4","2"]
If I map it using
let count = response.data.metrics.data.map((item) => + parseInt(item));
I am having a hard time understanding why it's treating this as a string returning
88121743
When I feel like because I am parsing it as an integer it should add and come out with 42.
Is this just an issue just using .map? Can shortcut math functions be used here?
Here is my Reproducible Example
Your current approach using Array#map creates a new array with each element converted to a number. React renders this array as multiple text nodes, so it looks like a single string.
To sum the values, use Array#reduce with parseFloat/parseInt, the Number function, or the unary plus operator to convert each string to a number.
const visitCount = data.reduce((a,b) => a + parseFloat(b), 0);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
Maybe tittle is not too descriptive, but how do I get this kind of result
var string = 11+'-'+10
// expected result 11-10
console.log(string);
Every time I try to do the above I get 1, or whatever the result of the subtraction is
I will be a little bit clear about this. What I want to do with this is generate a button with onclick like this:
onClick = method(1,[11-10, 12-10])
method(id,...array){
console.log(array)
//result [1,2]
}
even if inspecting the button actually shows the correct output
In your first example, you use
11+'-'+10
In the second one, you use
11-10
There is a clear difference
Using the first method in the second code will work as expected
method(1,[11+'-'+10, 12+'-'+10])
To make it shorter just use strings
method(1,['11-10', '12-10'])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
i want to change my date format from "20221015T000000Z" to yyyy-mm-dd. i tried looking up solutions but not able to find anything relatable.
The example date string already contains all the information you need in clear text. It's not a standard date string, but assuming the format of the string is always going to be like that, all you have to do is to extract the substrings and stitch them back together using the - separator.
This is exactly what this function does by using String's substr method and Array's join method:
const parseDateString = s => [s.substr(0,4), s.substr(4,2), s.substr(6,2)].join('-');
Usage:
const result = parseDateString('20221015T000000Z');
// do something with result, e.g. console.log(result)
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 have to extract a string from backwards upto 10 digits.
Use case is when we select a mobile number it is prefixed with country code sometimes and sometimes we get only the mobile number.
Let's say the number is : +91-84040355236545
I have to extract the number from the end say from 5 to last 10 degits so the end result would be 0355236545
I have a solution of using string.substring method
Here's a simple solution using substrings:
var number = "+91-84040355236545";
var lastTenDigitsNumber = number.substr(number.length - 10);
console.log(lastTenDigitsNumber);
A simpler solution is using slice:
var number = "+91-84040355236545";
console.log(number.slice(-10));
Another solution using a function and RegEX:
function extractTenDigits(number) {
var rx = /(\d{10}$)/g;
var arr = rx.exec(number);
return arr[1];
}
var number = "+91-84040355236545";
console.log(extractTenDigits(number));
I did a simple benchmark and the best solution for small quantity of numbers is the one using slice.
If you provide more details I can provide a more tailored suggestion.
You can try the following using substring :
let str = "+91-84040355236545";
str.substr(str.length - 10 ,10);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to get the minimum value from an array without changing the way the the value are aligned.
function removeSmallest(numbers){
var min=Math.min.apply(null,numbers);
var indexvalue=numbers.indexOf(min);
delete numbers[indexvalue];
return numbers;
}
Two issues:
Math.min.apply(null, numbers) should be Math.min.apply(Math, numbers) (note Math instead of null)
Normally, delete is not the right tool for arrays (see this question's answers for details; use splice to remove array entries if you're going to modify the array in place:
numbers.splice(indexvalue, 1);
You might also consider creating and returning a new array instead of mutating and returning the same array you received.