Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

45
Views
conversion data calculations

Have a nice holiday and thank you for your help

I have already tried to solve my problem but I have not given enough information I will try to reformulate it

I want the output to be these numbers

input 14:1   output 141 
input 13:8   output 141
input 13:18   output 241 
input 18:13   output 291 

the calculation is as follows input 14:1 4+1 is not greater than 9 so the number one is the rest and we leave it at the end and add 14 before it output 141

input 13:8 3 + 8 is greater than 9 (11) so I add 1 to 3 of them become 4 and 1 is the rest we add at the end output 141

input 13:18   output 241 
input 18:13   output 291 

trying to help me here is an example code they thank a woman from Germany for it

const
    fn = string => {
        const
            sum = (a, b) => (a + b).toString().padStart(2, 0).split(''),
            [a, b] = string.split(':').map(s => s.padStart(2, 0)),
            result = [];
 
        let i = 2;
        while (i--) {
            const sum = +a[i] + +b[i];
            result[i] = sum > 10
                ? +[+a[i] + Math.floor(sum / 10), sum % 10].join('')
                : sum === 10 ? 1 : sum;
        }
        return result.join('');
    };
    
 
console.log(fn('13:18')); // I want 241 correct output 241
console.log(fn('18:13')); // I want 291 correct output 291

console.log(fn('13:15')); //  I want 28 correct output 28
console.log(fn('15:13')); //  I want 28 correct output 28

console.log(fn('14:1')); // I want 141 bad output   15
console.log(fn('13:8')); // I want 141 correct output 141
console.log(fn('78:96')) // I want 174 bad output   "8694"

this code calculates some results correctly and some incorrectly

if less than 10 console.log (fn ('14: 1 ')); // I want this 141 output / gives a bad output "15"

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

As per the logic you've explained, and assuming the numbers will be max of 2 length.

P.S. +(plus sign) prefix converts string to integer runtime

First Solution =>

const fn = (string) => {
      const [a,b] = string.split(':').map(s=>s.padStart(2,0)), result=[];
      // for first position
      result[0] = +a[0] + +b[0];
      // for second position
      let secondIndexTotal =  (+a[1] + +b[1]).toString();

      result[1] = +secondIndexTotal > 9 ? (+a[1] + 
      +secondIndexTotal[0]).toString() + secondIndexTotal[1] : a[1]+b[1];

      return result.join('');
}

console.log(fn('13:18')); // output 241
console.log(fn('18:13')); // output 291

console.log(fn('13:15')); // output 235
console.log(fn('15:13')); // output 253

console.log(fn('14:1')); // output 141
console.log(fn('13:8')); // output 141

Updated Solution =>

const fn = (string) => {
  const [a, b] = string.split(":").map((s) => s.padStart(2, 0)),
    result = [];
  // for second position
  const secondIndexTotal = (+a[1] + +b[1]).toString();

  result[1] = +secondIndexTotal > 9 ? secondIndexTotal[1] : b[1];

  // for first position
  const firstIndexTotal = +a[0] + +b[0];
  result[0] =
    firstIndexTotal > 9
      ? firstIndexTotal + (+secondIndexTotal > 9 ? +secondIndexTotal[0] : 0)
      : firstIndexTotal.toString() +
        (+a[1] + (+secondIndexTotal > 9 ? +secondIndexTotal[0] : 0));

  return result.join("");
};
console.log(fn("13:18")); // output 241
console.log(fn("18:13")); // output 291
console.log(fn("78:96")); // output 174
console.log(fn("14:1")); // output 141
console.log(fn("13:15")); // output 235
console.log(fn("15:13")); // output 253

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.