-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumofdigits.js
More file actions
executable file
·59 lines (37 loc) · 1.6 KB
/
Copy pathsumofdigits.js
File metadata and controls
executable file
·59 lines (37 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*sum of digits of a number big numbers iike
144 what will the sum of the digits individual like :
1+4+4 = 9 ; this will be the digits of the sum okay ? */
function hishab_kitab(num){
let sum =0;
while(num>0){
sum+=num%10;
num=Math.floor(num/10);
}
return sum;
}
console.log(hishab_kitab(264));
/* Understanding Function Return in JavaScript
Here I created a function where the parameter is `num`. Then I declared a variable `sum = 0`, which is 0 at the beginning. I used a `while` loop because I don’t know how many times it will run.
Inside the loop, I check if `num > 0`. Then I take the last digit using `num % 10` and add it to `sum`. After that, I remove the last digit using `Math.floor(num / 10)`. This process keeps going until `num` becomes 0.
After finishing the loop, I wrote:
```
return sum;
```
This means the function is sending back the final value of `sum`.
Now if I call the function like this:
```
hishab_kitab(144);
```
it will calculate everything correctly, but I won’t see anything. That’s because the function is just returning the value, not showing it.
If I want to see the output, I need to do:
```
console.log(hishab_kitab(144));
```
So what happens here is the function runs with `num = 144`, it calculates the sum which is 9, then `return sum` gives that value back, and `console.log` shows it.
I can also store it like this:
```
let result = hishab_kitab(144);
```
Now `result` is 9, and I can use it anywhere in my code.
The main idea is the function does the calculation and returns the value. If I want to see it, I have to print it or store it somewhere.
*/