Javascript regex to parse human readable dates [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a dates in String in Javascript that could look like:
1h
1h2m
1d3m4s
2d2h2m2s2ms
1ms
3s5ms
The indicators will not change, they are d, h, m, s, ms
What would be a good regex to parse the numbers out:
for 3s5ms, it should be:
parsed = [0,0,0,3,5]
for 1d4m, it should be:
parsed = [1,0,4,0,0]

How about this:
var getNumbers = function (string) {
var numbersArray = string.match(/(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?(?:(\d+)ms)?/);
numbersArray.shift();
return numbersArray.map(function (val) {
return parseInt(val) || 0;
})
};
getNumbers("3s5ms") // [0, 0, 0, 3, 5]
getNumbers("2d2h2m2s2ms") //[2, 2, 2, 2, 2]

Related

Check if array has value from and to a variable [closed]

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 days ago.
Improve this question
I really don know how to title this question but here is what i am wondering.
I have this array of numbers:
numbers = [5, 10, 20, 25, 30, 40]
I want to remove numbers that dont increase by 10. So what i mean by that, inn a for loop, if the current index + 10 is not inn the array, then i want to delete that number. so the correct number array would be.
filteredNumbers = [10, 20, 30, 40]
I hope this makes sense. kinda har to explain exactly with words.
You could check pairs with delta of 10.
const
numbers = [5, 10, 20, 25, 30, 40],
result = numbers.filter((v, i, { [i - 1]: prev, [i + 1]: next }) =>
prev + 10 === v || v + 10 === next
);
console.log(result);

Why is my JavaScript code not accepted as the right answer? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to do this Javascript exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
I am wondering why the solution below is not an accepted answer:
let count = 0;
function cc(card) {
// Only change code below this line
const low = [2, 3, 4, 5, 6];
const high = [10, 'J', 'Q', 'K', 'A'];
if (low.includes(card)) {
count += 1;
}
else if (high.includes(card)) {
count -= 1;
}
let decision;
if (count > 0) {decision = "Bet"}
else {decision = "Hold"}
return count + decision;
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
When I am comparing it to accepted answers I don't see what they are doing differently. One thing that is not clear to me in the assignment is that should return be called every time or only after the last function call (cc('A');).
Add a space between count and decision
return count + " " + decision;
You are giving an answer in the wrong format. Just missing the space between count and decision.
Incorrect:return count + decision;
Correct:return count +" "+ decision;

Is JavaScript eval() function safe to use for numeric operations? [closed]

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 1 year ago.
This post was edited and submitted for review 3 days ago.
Improve this question
Please check the code below first.
const plusFn = function(){
const sign = '+'
inputUser.textContent += sign
const equalFn = function(){
const input = `${inputUser.value}`
const numbers = input.split(`${sign}`)
const result = eval(`${+numbers[0]}${sign}${+numbers[1]}`)
}
btnEqual.addEventListener('click',equalFn)
}
btnPlus.addEventListener('click',plusFn)
Let's assume our user pressed 123 and wrote it to input area, then pressed +, from now on he trigerred the plusFn which determines a specific operator and adding a new event listener for equality button. When he gives second number and presses the equality button, operating will work as upside. Is it safe to use like that?
but I couldn't think any alternative
For something as simple as your example, where you have exactly two inputs and an operator from a constrained list (just + in your example, but I'm guessing you have three others), you could use a dispatcher object or Map:
const operatons = {
"+": (a, b) => a + b,
"*": (a, b) => a * b,
"/": (a, b) => a / b,
// ...
};
// Using it...
const operation = operation[operator];
if (!operation) {
throw new Error(`Invalid operator "${operator}"`);
}
const result = operation(input1, input2);

How can I do this using Reduce function? [closed]

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 6 years ago.
Improve this question
var y= '110001'.split("").reverse();
var sum = 0;
for (var i = 0; i < y.length; i++) {
sum += (y[i] * Math.pow(2, i));
}
console.log(sum);
It would be simplest to do
console.log(Array.from('110001').reduce((prev, cur) => prev << 1 | cur));
<< is the left-bitshift operator, which here essentially multiplies by two.
Array.from (if available) is preferable to split. In this case it doesn't matter, but split will fail with surrogate pair characters such as 🍺, while Array.from will handle them correctly. This could also be written as [...'110001'], which ends up being the same thing.
Of course, you could also just say
parseInt('110001', 2)
check this snippet
var binary = '110001'.split("").reverse();
var sum = binary.reduce(function(previous, current, index) {
previous = previous + (current * Math.pow(2, index));
return previous;
}, 0);
console.log(sum);
Hope it helps

javascript next number on multiples of three [closed]

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
I have a number for example 4, i want to get next number on multiples of 3
Multiples of three: [3,6,9,12,15,18,21,24,27,30,...]
the result must be 6
i'm looking for a javascript function
something like this:
function (myNum) { //myNum = 4;
var multiples = [3,6,9,12,15,18,21,24,27,30];
var result;
// do something!!
return result; // returns 6
}
thanks
I suggest another solution:
function getNext(num, dep){
return (((num % dep) ? dep:0) - num % dep) + num;
}
document.write(getNext(4, 3));//6
//document.write(getNext(200, 7));//203
Updated: You can use this method for finding next number on multiples of any number
There are a lot of ways you can achieve this. Here is an easy one. Increment the number until you get a multiple of three.
function multipleOfThree(num){
while(num % 3 != 0)
num++;
return num;
}
You must try before asking a question. If you stuck at somewhere then it is good to ask questions with problem. By the way here what you can try:
function multiple(number) {
return number % 3 === 0 ? ((number/3) * 3) : parseInt((number/3) + 1) * 3;
}

Categories