I'm looking for a solution to my problem
if I have numbers
var first = 14:1
var next = 13:8
therefore, the console should give a result
console.log(first_result) // 141
console.log(next_result) // 141
and I want the numbers counted like 141
in the result to be
simply if there is an example
13:8
if both digits are the last, 3 and 8 are bigger than ten, then turn the tens and turn and insert into the previous number and leave the rest at the end
so 13:8
becomes 141
CodePudding user response:
You could separate and check.
const
fn = string => {
const
[a, b] = string.split(':').map(Number),
sum = a % 10 b % 10;
if (sum > 10) return [a 1, sum % 10].join(':');
return string;
};
console.log(fn('13:8'));
CodePudding user response:
If you are starting with strings, then you just simply split the string on :
to get your 2 numbers.
To get the last digit, you can simply use x % 10
. Then just add the numbers and see what happens.
let value = '13:8',
nums = value.split(':').map(Number),
last_digits = nums.map(x => x % 10),
// or last_digits.reduce((x,y) => x y, 0)
sum = last_digits[0] last_digits[1],
result;
if (sum > 10) {
nums[0] ;
nums[1] = sum - 10; // or, probably sum % 10
}
result = nums.join('');
console.log(result);