top of page

# DataWeave programming challenge #8: Sum all digits to get a 1-digit number

Â

Other posts from this series:

Â

In this post:

Â

Try to solve this challenge on your own to maximize learning. We recommend you refer to the DataWeave documentation only. Try to avoid using Google or asking others so you can learn on your own and become a DataWeave expert!

## Input

Consider the following input payload (can be of txt format):

```2456
2
25235
2456
6
54
58795
3
456
76544
0```

## Explanation of the problem

Create a DataWeave script to sum all the digits from the input payload until there's only one digit left.

For example,

• 123 + 456 = 579

• 579 is broken down into 5 + 7 + 9 = 21

• 21 is broken down into 2 + 1 = 3

• The final result is 3 because it's a 1-digit number

## Expected output

In this case, the expected output would be:

`2`

## Clues

If you're stuck with your solution, feel free to check out some of these clues to give you ideas on how to solve it!

Clue #1

Clue #2

Clue #3

## Answer

If you haven't solved this challenge yet, we encourage you to keep trying! It's ok if it's taking longer than you thought. We all have to start somewhere âœ¨ Check out the clues and read the docs before giving up. You got this!! ðŸ’™

There are many ways to solve this challenge, but you can find some solutions we provide here so you can compare your result with us.

Solution #1

Solution #2

Feel free to comment your code below for others to see! ðŸ˜„

Subscribe to receive notifications as soon as new content is published âœ¨

Tags:

758 views17 comments

## 17 commentaires

%dw 2.0 output application/json fun solve(x) = if (sizeOf(x)==1) x else solve((x reduce ((item, accumulator ) -> accumulator + item))) --- solve(payload splitBy('\n'))

J'aime

after I viewed the simple reduce solution provided by Diksha, I updated my gist to add a "recursive reduce" function. Also, after realizing the "mod 9" solution (thanks Sai Teja) I wrote this simplistic solution: %dw 2.0 import lines from dw::core::Strings output application/json --- (lines(payload) joinBy "") as Number mod 9

J'aime
En rÃ©ponse Ã

https://math.stackexchange.com/questions/99725/every-integer-is-congruent-to-the-sum-of-its-digits-mod-9

J'aime

output application/json fun singleit(L:Array):Number=singleit(sum(L map singleit(\$ as Number default \$))) fun singleit(X:Null):Number =0 fun singleit(N: Number):Number = if (N==0) 0 else do{var res = (N mod 9) --- if(res==0) 9 else res} fun singleit(Str :Any) : Number =0 --- singleit(payload splitBy "\n")

J'aime
Alex Martinez
13 juil. 2023
En rÃ©ponse Ã

woohoo! thank you so much for sharing!

J'aime

%dw 2.0 output application/json import lines from dw::core::Strings fun reduceToOneDigit(inputValue) = inputValue match { case d if(d is Number and sizeOf(d) == 1) -> d else -> reduceToOneDigit(\$ reduce \$\$ + \$) } --- reduceToOneDigit(lines(payload))

J'aime
Alex Martinez
13 juil. 2023
En rÃ©ponse Ã

nice and simple! thank you for sharing!

J'aime

Diksha Dhanuka
12 juil. 2023

%dw 2.0 output application/json --- payload splitBy ("\n") reduce (\$+\$\$) reduce (\$+\$\$) reduce(\$+\$\$)

J'aime
Alex Martinez
13 juil. 2023
En rÃ©ponse Ã

haha simple as that ðŸ˜„

J'aime
bottom of page