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.