0

I have the following code, it takes user input string value, assigns alphabetic characters with specific numbers and later on sums them up. Problem is that my code won't work, I wonder where did I make mistake.

<script type="text/javascript">

function addalldata() {

a = j = s = 1;
b = k = t = 2;
c = l = u = 3;
d = m = v = 4;
e = n = w = 5;
f = o = x = 6;
g = p = y = 7;
h = q = z = 8;
i = r = 9;

let userinputname = document.getElementById("fullusernameinput").value;
let userinputnamelow = userinputname.toLowerCase();
let userinputnamearray = userinputnamelow.split('');
let pirvelijami1 = userinputnamearray.reduce((a, b) => +a + +b, 0);


alert(pirvelijami1);

}

</script> 

It returns NaN

SoulFly
  • 63
  • 2
  • 7
  • Use an object instead of individual variable names, then use bracket notation to look up the right property on the object inside the `reduce` callback – CertainPerformance Apr 07 '20 at 07:07
  • ``` ``` Followed your advice, I still somehow get error – SoulFly Apr 07 '20 at 07:53
  • `.reduce((a, char) => a + ricxvebidaasoebi[char], 0)` Bracket notation. Also consider using sensible variable names – CertainPerformance Apr 07 '20 at 07:54

0 Answers0