0

I have this number 55555

If we add this single number then the result will be = 25 and then if we add 2 + 5 then the result will be 7.

I want to make it but can't get the result:

What I have done so far:

function createCheckDigit(membershipId) {
  // Write the code that goes here.
    string = membershipId.split('');                
    let sum = 0;                               
    for (var i = 0; i < string.length; i++) {  
        sum += parseInt(string[i],10);         
    }
  
    console.log(sum.split(''));
   
    
  
}

console.log(createCheckDigit("55555"));
3
  • ...you made sum a number, what would sum.split even do? Commented Sep 11, 2020 at 6:00
  • 1
    Its recursion. Call same function until you get number less than 10 Commented Sep 11, 2020 at 6:00
  • Please share more details. What does work? What exactly does not? What have you tried to make it work? Commented Jun 18, 2024 at 7:00

2 Answers 2

4

Make the function recursive - after the loop, if the sum is 10 or more, call it again:

function createCheckDigit(membershipId) {
  // Write the code that goes here.
    string = membershipId.split('');                
    let sum = 0;                               
    for (var i = 0; i < string.length; i++) {  
        sum += parseInt(string[i],10);         
    }
    return sum >= 10 ? createCheckDigit(String(sum)) : sum;
}

console.log(createCheckDigit("55555"));

Or, more concisely:

function createCheckDigit(num) {
  const nextNum = num.split('').reduce((a, b) => a + Number(b), 0);
  return nextNum >= 10 ? createCheckDigit(String(nextNum)) : nextNum;
}

console.log(createCheckDigit("55555"));

Sign up to request clarification or add additional context in comments.

5 Comments

can you explain this line please: return sum >= 10 ? createCheckDigit(String(sum)) : sum;
why we need to check with 10
@creativeartbd If the sum is 10 or more, you need to go through the process again until the result is a single digit (0 to 9), right? So, check if it's >= 10 to see if it's needed.
can you give me a resource where I can learn that type of algorithms?
@creativeartbd All I really added to your algorithm was the recursive check. Just think about how you'd solve it on paper ("If the sum is 10 or more, repeat") and then translate into code. Knowing about concise idiomatic ways to solve it in a particular programming language (like .reduce in JS) is nice too, but it's mostly just critical thinking and then translating it into code.
0

function makesum(value)
{
    return value.toString().split('').reduce((a,b) => a/1+b/1);              
    
}
function actualFunction(newvalue)
{
    if(newvalue.toString().split('').length < 2)
    {
   return makesum(newvalue);
    }
    else
    {
           return makesum(makesum(newvalue));
    }
}

console.log(actualFunction(55555))
console.log(actualFunction(1111111111))
console.log(actualFunction(99))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.