convert integer to 4 digit decimal in hour format - javascript

I need to write a function where i got one input parameter that can be a number from 1 to 9999. I need that output always be in a 4 digits format with a point between the numbers. For example, if the input is 1, the output must be 00,01. Input 157, ouput 01,57, and so on. Thanks

Consider utilizing padStart and slice:
const formatNum = num => {
if (!Number.isInteger(num)) throw "num must be an integer!";
if (num < 1 || num > 9999) throw "num must be between 1 and 9999 inclusive!";
const paddedNum = String(num).padStart(4, '0');
// TODO(#marcelo-teixeira-modesti): Add check to see if valid time as per train software requirements...
return `${paddedNum.slice(0, 2)},${paddedNum.slice(2)}`;
}
console.log(formatNum(1));
console.log(formatNum(157));
console.log(formatNum(2356));

Related

How to remove the numbers that already occur with their digits reversed

I need a function that will take an array of numbers, and which will return an array that only retains the numbers that are unique in their digit sequence, i.e. that only occur once even if you would reverse their digits.
This is my code so far:
var a=[5,8,3,8,3,7,5,12,21];
console.log(a);
let output=[...new Set([...a])] // it removes the repeated data...
console.log(output);
This works for the numbers 3, 8 and 5, whose duplicates are removed, but the value 21 should also be removed, because there is already 12, which is 21 with the digits in reversed order.
How can I achieve that?
The expected output for the above example is:
[5,8,3,7,12]
My code returns:
[5,8,3,7,12,21]
You need (of course) to include the logic of reversing digits in a number.
I will assume that when the rightmost digit of a number is 0, that the reversal of that number is not defined. So the reversal of 19 is 91, but the reversal of 190 is not 91, but undefined (or NaN).
First define a function for that reversal of digits, and then use the idea of building a set:
function reverseDigits(num) {
// A Number should not have 0 as first digit
// unless it is 0
if (num && num % 10 == 0) return NaN;
return +[...String(num)].reverse().join("");
}
function specialUnique(a) {
const set = new Set;
for (const value of a) {
if (!set.has(value) && !set.has(reverseDigits(value))) {
set.add(value);
}
}
return [...set];
}
// Example input
const a = [5,8,3,8,3,7,5,12,21];
const output = specialUnique(a);
console.log(output);
You can use the filter.
let valueToRemove = 21;
output = output.filter((item) => item !== valueToRemove);

Regex to match numbers between 0 to 25 both inclusive which can be doubles with 1 precision

I want to use Regex to match numbers between 0 to 25 both inclusive which can be doubles with 1 precision.
For ex.- 2, 2.5, 23.0, 8, 24.3, 25
I created following regex-
^(\\s*|0?[0-9]|[0-9]|1[0-9]|2[0-5])$
But it works only for numbers between 0 to 25 both inclusive.
This is the regex pattern I would use here:
^(?:(?:[0-9]|1[0-9]|2[0-4])(?:\.[0-9])?|25(?:\.0)?)$
Demo
The pattern is similar to what you used, except that we only match 2[0-4] with any single decimal point. The end point 25 is a special case, which can only take 25.0 at most for its decimal component.
But in general it would easier in JavaScript to just parse the text number, and then use an inequality to check the range. E.g.
var number = '24.3'
if (number >= 0 && number <= 25.0) {
console.log('within range');
}
To verify the validity, use the >= and <= operators. To meet the regex requirement, just add a garbage regex expression.
let isMatch = number => {
'must use regex'.match(/x/);
return number >= 0 && number <= 25;
};

JavaScript regex two string number separated by ( : ) individual max min calculation

I need to validate JavaScript string like "03:39" with Regex. Rules are following,
1.First part of the string which is before (:) is allowed from 0 to 24
2.Second part of the string which is after (:) is allowed from 0 to 59
3.Single digit number of both part can either be start with 0 like 01 or 02 or with out 0 like 1 , 2.
Now i have wrote a JavaScript regex code which is following.
var dateRegEx = /^n(0[0-2][0-4]|[0-24])(:)(0|[0-5][0-9]|[0-59])$/;
if ($(element).val().match(dateRegEx) === null) {
// my rest of the code
}
now, when user insert 0:38, 01:38 or 02:59 it get validated but when the first part incensed after 2 like 03:38 it not validating the string.
So what am i doing wrong ??
For hours, your current 0[0-2][0-4] requires three digits (none of which can be above 4), whereas [0-24] will only match one digit (a 0, 1, 2, or 4).
Rather, to match hours (00 to 24), you should use (?:2[0-4]|[01]?[0-9]); either a 2 followed by a number from 0 to 4, or an (optional) 0 or 1 followed by any other digit.
To match minutes (00 to 59), use [0-5]?[0-9] - an optional digit from 0 to 5, followed by a digit from 0 to 9.
^(?:2[0-4]|[01]?[0-9]):[0-5]?[0-9]$
https://regex101.com/r/0QxX7w/1
Note that if you're just matching a single character like :, don't put it in a character set. Also, because you don't want an n at the beginning of the match, remove it from the beginning of your pattern.
Well i think you don't need regex here you can do directly like this
function check(input){
let temp = input.split(':')
return (0 <= temp[0] && temp[0] <=24 ) && ( 0<= temp[1] && temp[1] <=59)
}
console.log(check('03:380'))
console.log(check('03:38'))
console.log(check('30:0'))
With regex you can do it like this
function check(input){
return /^(?:[01]?[0-9]|[0-2][0-4]):[0-5]?[0-9]$/.test(input)
}
console.log(check('03:30'))
console.log(check('03:380'))
console.log(check('33:30'))
console.log(check('24:59'))
console.log(check('03:60'))
console.log(check('0:0'))
console.log(check('0:30'))

regex validation for age group

Am looking for a regex validator to validate age between 18 and 65 so far i have this:
^(1[90]|[2-6][0-9])$
I'm not sure why you would want to use regex for checking number ranges.
Below I've got a demo that checks the range the standard way, with greater than and less than but also I've included a regex way.
The regex way requires you to convert the number to a string first which is what the toString() bit is.
const numbers = [1, 20, 55, 67];
for (let num of numbers) {
if (num >= 18 && num <= 65)
console.log(`standard: ${num} is in range`)
}
for (let num of numbers) {
if (num.toString().match(/^([1][8-9]|[2-5][0-9]|[6][0-5])$/))
console.log(`regex: ${num} is in range`)
}
Here is a diagram to explain how the regex works.
^([1][8-9]|[2-5][0-9]|[6][0-5])$
Hope this is helpful.

Javascript Greater than or less than

I did a rewrite of the code I submitted yesterday based on suggestions from others. I now have this but still can't seem to get it to work with greater than less than. I can add/substract the 2 numbers and get a valid answers. I can't get a > < to work however. Hoping someone can offer some additional help keeping it within this format of "If statements".
if ((input.search("what is greater")!= -1) && (input.search(/\d{1,10}/)!=-1) && (input.search(/\d{1,10}/)!=-1))
{var numbersInString = input.match(/\d+/g);
var num1 = parseInt( numbersInString[0], 10 );
var num2 = parseInt( numbersInString[1], 10 );
if (num1 < num2) document.result.result.value = ""+num1+" is less than "+num2+"";
if (num1 > num2) document.result.result.value = ""+num1+" is greater than "+num2+"";
if (num1 = num2) document.result.result.value = "Both numbers are equal";
return true;}
It sounds like you want to manipulate a number in two ways:
1) You want to refer to the individual characters.
2) You want to compare the number to another number and see if one is greater than another.
If you have a string called input, then you can use the function parseInt(input, 10) to convert it from a string to the number represented by that string.
If you want to get just the first two characters, you can use the substring function.
The important thing to keep in mind is that to the computer, the string '12345' and the number 12345 are completely different. The computer has a completely different set of operations that it will perform on each.
also, #albin is correct to point out that putting semicolons after your if statements is wrong.
The output of the match method is an array of strings, so I think you are NOT comparing numbers but strings. Try doing this before comparing your numbers.
var num1 = parseInt( numbersInString[0], 10 );
var num2 = parseInt( numbersInString[1], 10 );
And then compare num1 and num2.
http://jsfiddle.net/qt3RW/
Simple input box:
​​​​
<input id="input1" value="Is 6 greater than 5"></input>
Parser find 'Is # greater than #' where # is digit and alert this digits:
var IsStringValid = $("#input1").val().match(/Is \d greater than \d/g);
alert(IsStringValid);
if(IsStringValid){
var values = $("#input1").val().match(/\d/g);
for(var i = 0; i < values.length; i++){
alert(values[i])
}
}
​

Categories

Resources