How to perform addition on multiple numeric attributes?

I see that there is a + function but I can only add two numeric attributes together. Is there a way to add 10 numeric attributes?

Do you want to add up exactly 10 numbers or sometimes you want to add up to N numbers?

Actually the function “+” allows the add-up of any number of numbers, but our current GUI just doesn’t allow users to add more than 2 now. I’m filing an issue to enable this in the near future.

In the meantime, you can use one of two ways to add up 10 numbers.

I. In a message, where you want to add up 10 numbers, just use the following expression (make sure you include the single back quotes to quote the function:
(+ 1 2 3 4 5 6 7 8 9 10)

II. Define your own add function that allows 10 variables. The benefit of this approach is that you can reuse this function. You can also easily insert attributes you want to use as arguments inside the function.

  1. Go to “Juji IDE” by clicking on the red icon, see below.

  2. Go to the chatbot you want to allow this. Click on the custom file, see below. Copy paste the function below into the custom file. MAKE SURE YOU SAVE IT by clicking on the “Save” button.

    (defn my-add [x1 x2 x3 x4 x5 x6 x7 x8 x9 x10]
         (+ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10))
  1. Now, go back to your chatbot where you want to use this function, click on “Add a function icon”

  2. Select “Add External Function”

  3. In the pop-up, fill in the info as below, and add all the 10 arguments, if you have attributes, you can use attributes as your arguments too. Make sure you choose the “Numeric” as the argument type. In this testing, it adds the 10 numbers from 1 to 10.

  1. Test this function.

1 Like

@mzhou That’s perfect! Thank you so much! :slight_smile:

@mzhou How will I be able to store the sum into an attribute so I can use it later for conditions?

You’re most welcome! My colleague just told me another way to do this if you want to have a more generic function. Just copy & paste the following function in the custom file instead of the function I mentioned above with 10 arguments. This function will allow you to add up ANY number of arguments.

(defn my-add [& ns]
  (apply + ns))
1 Like

@mzhou I will definitely try that. Thanks again! Also, is there a way to store the sum into an attribute?

Yes, you can store anything including a function results into an attribute.

  1. Select a topic (card) where you want to do the computation and store the value into an attribute.

  2. On the right panel, scroll down to find the section called “Customize Chatbot Actions”

  3. Decide whether you want to compute and store the attribute BEFORE or AFTER your chatbot sends a message or asks a question. If you want to do it BEFORE, select the block “Pre-Action”, otherwise “Post-Action”

  4. Inside such a block, click on the green “+” button to select “Store an attribute”
    Screen Shot 2020-11-23 at 10.52.08 PM

  5. Inside the pop-up window, you can define the attribute value using your “add” function. Note here, do NOT add the single back quotes

Now you can use this stored attribute somewhere else in your chat flow.

1 Like