-3

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

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 1
    You mean you have strings containing `"14:1"` and `"13:8"`? It's not very clear what the rules for generating the result are. – Dave Newton Dec 13 '21 at 17:28
  • 1
    Do you mean the sum of the last digits bigger than 10..? – Redu Dec 13 '21 at 17:29
  • So, if I follow... `13:8` becomes `[13, 8]`, then you add `3 + 8` and get `11`. `11` is more than `10`, so then you get `[13+1, 1]` or `[14, 1]` and thus `141`. Is that right? – gen_Eric Dec 13 '21 at 17:30
  • @Redu yes curetly – John Prope Dec 13 '21 at 17:30
  • 1
    @RocketHazmat yes this right – John Prope Dec 13 '21 at 17:30
  • @DaveNewton 14:1 4 and 1 are not greater than 9, so the numbers remain in the same position thus 141 and the example where 13: 8 3 and 8 is 11 so 1 (and add 3 to 3) and add 1 at the end thus 141 – John Prope Dec 13 '21 at 17:32
  • 1
    What have you tried? Please see the [How to Ask](https://stackoverflow.com/help/how-to-ask) page. It's best to show your good-faith effort instead of just dropping requirements on SO. – Dave Newton Dec 13 '21 at 17:37

1 Answers1

0

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);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    I'm new here I can not give evaluation reviews sorry and thank you very much – John Prope Dec 13 '21 at 17:40
  • if I can ask for a repair 7:22 gives the result but they need it to become the number "722" in doing so, he should proceed exactly the opposite 722 the result should therefore be correct 29 because 7 and 2 are not greater than 9 – John Prope Dec 13 '21 at 19:25