-3

I need to create a sequence of numbers using while or for that consists of the sum of the symbols of the number.

For example, I have a sequence from 1 to 10. In console (if I've already written a code) will go just 1, 2,3,4,5,6,7,8,9,1. If I take it from 30 to 40 in the console would be 3,4,5,6,7,8,9,10,11,12,13.

I need to create a code that displays a sum that goes from 1 to 100. I don't know how to do it but in console I need to see:

1
2
3
4
5
5
6
7
8
9
1
2
3
4

etc.

I've got some code but I got only NaN. I don't know why. Could you explain this to me?

for (let i = '1'; i <= 99; i++) { 
    let a = Number(i[0]);
    let b = Number(i[1])
    let b1 = Boolean(b)

    if (b1 == false) {
        console.log ('b false', a)
    }
    else {
        console.log ('b true', a + b)
    }
}

I hope you get what I was speaking about.

Jsowa
  • 9,104
  • 5
  • 56
  • 60
Allegium
  • 11
  • 3
  • 1
    maybe have a look in advance to this answer: https://stackoverflow.com/questions/38334652/sum-all-the-digits-of-a-number-javascript/38336308#38336308 – Nina Scholz Oct 03 '20 at 16:42
  • Does this answer your question? [Sum all the digits of a number Javascript](https://stackoverflow.com/questions/38334652/sum-all-the-digits-of-a-number-javascript) – FluffyKitten Oct 03 '20 at 17:16

3 Answers3

1

Although I like the accepted answer however from question I gather you were asking something else, that is;

30 become 3+0=3
31 become 3+1=4
37 becomes 3+7=10

Why are we checking for boolean is beyond the scope of the question

Here is simple snnipet does exactly what you ask for

for (let i = 30; i <= 40; i++) {
  let x=i.toString();
console.log( 'numbers from ' +i + ' are added together to become '+ (Number(x[0])+Number((x[1])||0)))
 }

what er are doing is exactly what Maskin stated begin with for loop then in each increment convert it to string so we can split it, this takes care of NAN issue. you don't need to call to string just do it once as in let x then simply call the split as x[0] and so on.

within second number we have created a self computation (x[1])||0) that is if there is second value if not then zero. following would work like charm

for (let i = 1; i <= 10; i++) {
  let x=i.toString();
console.log( 'numbers from ' +i + ' are added together to become '+ (Number(x[0])+Number((x[1])||0)))
 }
Did you observe what happens to ten

here is my real question and solution what if you Don't know the length of the digits in number or for what ever reason you are to go about staring from 100 on wards. We need some form of AI into the code

for (let i = 110; i <= 120; i++) {
let x= Array.from(String(i), Number);

console.log(
x.reduce(function(a, b){ return a + b;})
);
};

You simply make an array with Array.from function then use simple Array.reduce function to run custom functions that adds up all the values as sum, finally run that in console.

Nice, simple and AI

Syed
  • 696
  • 1
  • 5
  • 11
0

So the way a for loop works is that you declare a variable to loop, then state the loop condition and then you ask what happens at the end of the loop, normally you increment (which means take the variable and add one to it).

When you say let i = '1', what you're actually doing, is creating a new string, which when you ask for i[0], it gives you the first character in the string.

You should look up the modulo operator. You want to add the number of units, which you can get by dividing by 10 and then casting to an int, to the number in the tens, which you get with the modulo.

As an aside, when you ask a question on StackOverflow, you should ask in a way that means people who have similar questions to you can find their answers.

0

You got NaN because of "i[0]". You need to add toString() call.

 for (let i = '1'; i <= 99; i++) {
   let a = Number(i.toString()[0]);
   let b = Number(i.toString()[1])
   let b1 = Boolean(b)
   if (b1 == false) {
     console.log('b false', a)
   } else {
     console.log('b true', a + b)

   }
 }