0

I am trying to solve a problem where a user gives a text input then I have to convert the text into given numeric values, add them together unless the answer is a single digit number.

So far, I have been able to get input, transform them into given numbers and store them in an array. But I cannot figure out what I will do next. How I will add the values together until it is left a single digit number.

var values = {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10,k:11,l:12,m:13,n:14,o:15,p:16,q:17,r:18,s:19,t:20,u:21,v:22,w:23,x:24,y:25,z:26};
var someText = prompt('');

someText = someText.trim();
someText = someText.match(/\S/g);

var result = "";
for (i = 0; i < someText.length; i++) {
  result += values[someText[i]];
}

alert(result);

For Example if input is "action" then numeric values will be =>1.3.20.9.15.14. -Add these values together. 1+3+20+9+15+14 answer will be 62. -As the Answer is a 2 digit number, we add the numbers again e.g 62 = 6+2= 8.

Note: If answer is more then 1 digits: e.g 199 => 1+9+9 => 19 => 1+9 => 10 = 1+0 = 1. Answer must always be a single digit number.

adiga
  • 34,372
  • 9
  • 61
  • 83

3 Answers3

0

You can use reduce() to get the sum of all the digits of string. If the result is single digit then return the the number otherwise call the function recursively.

You also don't need to hard code the object with letter values. Just use reduce() and chatCodeAt to make a string of numbers.

var someText = prompt('').trim().toLowerCase().match(/\S/g)

let result = someText.reduce((ac,a) => ac + (a.charCodeAt(0) - 96),'');
function findSum(num){
  if(num.length === 1) return num;
  else return findSum(String(num.split('').reduce((ac,a) => ac + +a,0)))
}

console.log(findSum(result))
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
0

Here's how you can do it:

var values = {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10,k:11,l:12,m:13,n:14,o:15,p:16,q:17,r:18,s:19,t:20,u:21,v:22,w:23,x:24,y:25,z:26};
var someText = prompt('');

someText = someText.trim();
someText = someText.match(/\S/g);

var result = "";
for (i = 0; i < someText.length; i++) {
  result += values[someText[i]];
}

// While the result is >9, which means that there's more than 1 digit
while(result > 9) {
   // Cast result as String, so we can use charAt()
   var stringedResult = String(result);

   // Reset our result var, because we're going to recalculate it
   result = 0;

   // For each int in our result, we add them up (ex. 621 = 6+2+1)
   for(var i=0; i<stringedResult.length; i++) {
       // Cast the stringed result back to a number, and add it to our result variable
       result += Number(stringedResult.charAt(i));
   }
}

alert(result);
marcoverbeek
  • 74
  • 1
  • 8
0

You should use a function to sum the values and return the value when it's one digit or call itself otherwise:

var values = {a:1,b:2,c:3,d:4,e:5,f:6,g:7,h:8,i:9,j:10,k:11,l:12,m:13,n:14,o:15,p:16,q:17,r:18,s:19,t:20,u:21,v:22,w:23,x:24,y:25,z:26};
var someText = prompt('');

someText = someText.match(/\S/g);

var result = 0;

function getSum(string) {
    for (i = 0; i < string.length; i++) {
        if (isNaN(string[i])) {
            result += values[string[i]];
        } else {
            result += parseInt(string[i]);
        }
    }
    if (result.toString().length > 1) {
        var aux = result.toString();
        result = 0;
        return getSum(aux);
    } else {
        return result;
    }
}

alert(getSum(someText));
virgiliogm
  • 946
  • 7
  • 15