'Please help me to solve this problem about dates and function
Write a function normalize, that replaces '-' with '/' in a date string.
Example: normalize('20-05-2017') should return '20/05/2017'.
I have tried this:
let d = new Date('27-11-2021');
function normalize(session){
let normal = d.replace('-','/');
return (session);
}
Solution 1:[1]
Try This or Use replaceAll instead of replace function because in new ES replaceAll is working.
let date = normalize('20-05-2017');
console.log('date: ',date);
function normalize(date){
return (date.replace(/-/g,'/'));
}
Solution 2:[2]
As you are being asked to pass a string representation of a date to the normalize function, you can use String.prototype.replaceAll() to replace each - with / in the string.
For this problem, there does not appear to be a need to parse the string as an actual javascript Date object.
// Write a function normalize, that replaces '-' with '/' in a date string.
// Example: normalize('20-05-2017') should return '20/05/2017'.
function normalize(stringDate) {
const normal = stringDate.replaceAll('-', '/');
return normal;
}
const result = normalize('20-05-2017')
console.log(result)
Solution 3:[3]
First off, the date is invalid for some reason. In the constructor, you cannot pass in 27-8-2021. So for example, you can go this way.
let d = new Date();
d.toLocaleDateString() // returns '8/27/2021'
d.toLocaleDateString('en-in') // returns '27/8/2021'
Solution 4:[4]
function normalize(Date){
let normal = Date.replace('-','/');
let norm2= normal.replace('-','/');
return norm2;
}
let Date = '27-11-2021';
Solution 5:[5]
I like to order the pairs (or, in general, ordered tuples) in what's called "colex" order, which is lexicographic order of the reversed tuple. Or, in other words, sorted by the largest element (and using the next largest element as a tie-breaker if the tuples are bigger than pairs.) This results in the ordering
0: (0, 1)
1: (0, 2)
2: (1, 2)
3: (0, 3)
4: (1, 3)
5: (2, 3)
6: (0, 4)
7: (1, 4)
8: (2, 4)
9: (3, 4)
The advantage of this ordering is that it doesn't depend on N, which is extremely helpful if you might later need to increase N without invalidating any existing index.
You can then compute (x, y) as:
n = floor((sqrt(8 * i + 1) - 1) / 2)
x = i - n * (n + 1) / 2
y = n + 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Usman Sabuwala |
| Solution 4 | Yogiraj Dharmraj |
| Solution 5 |
