Add Two Digits

Add Two Digitis

Outside of the values we give border with the style of *


##For Example For n = 29 the output should be 2+9 = return 11

Should return true
##


let n = 29;

function addTwoDigits(n){
  const nums = n.toString().split('');
  
  return nums.reduce((x, y) => {
    return parseInt(x)+ parseInt(y)
  })
}

console.log(addTwoDigits(n))

We create a function addTwoDigits with parameter n and local varable nums that is equal to n.toString().split(‘’)

We convert n toString() which is build in JavScript method for converting to string and if we chain .split() we turn that string into an array.
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call. -MDN

Lastly we return nums.reduce((x ,y) => { return parseInt(x) + parseInt(y) })

comments powered by Disqus