For example var str = 2975; you need to multiply the first by the second 2*9 .

Update

For some reason, I can't sum up if you write it like this.

  var morth = 10; var date = 16; var morth1 = (morth).toString(); var date1 = (date).toString(); // Складываем цифры дня и месяца рождения var date_res = date1[0] + date1[1]; var morth_res = morth1[0] + morth1[1]; var resultat = date_res + morth_res; document.write(resultat); 
  • @Surfer, because the + operator not only adds numbers, but also concatenates strings, which is done primarily for strings. Use parseInt() to convert strings to integers. - etki

1 answer 1

So?

 var str = (2975).toString(); alert(str[0] * str[1]); 

Update

@Surfer , or parseInt, as suggested by @Etki , or so you can:

 var date_res = +date1[0] + +date1[1]; var morth_res = +morth1[0] + +morth1[1]; 

PS I could have done so:

 var morth = 10, date = 16, digits = morth + '' + date, sum = 0; for(var i = 0; i < digits.length; i++){ sum += +digits[i]; } console.log(sum); // 8 
  • Updated the question - Kill Noise
  • Thanks, but for some reason, it returns "undefined" when the variable "morth" is 1 or 01 - Kill Noise
  • @Surfer; Because indexes that you specify manually may not be. Pay attention to my latest postScript code. - Deonis