How to create nutrition app with ocr scanner?

I know that you want a simple fix for everything but there are a lot of problems with the way you set up your code. I can’t solve it all for you and some of this is just taking the time to learn to program. The reality of coding is that if you change one little thing, it then affects everything else. So you have fixed one or two problems from earlier but then added new problems that you didn’t have before because you moved things around or added blocks.

Some of this is basic programming like the fact that the “else if” section of a block only happens if the condition is false but the condition itself has to be a true/false statement; or realizing that a variable does not have a value until you give it a value using the “set” block. Those are things you learn over time by starting with a very simple programming task and lots of trial and error.

It seems like you’re in a hurry to get everything done by this weekend which is somewhat unfortunate because you’ve picked one of the absolute hardest features of Thunkable (Web APIs) to begin with and it’s not quick to learn. At all. But I’m a broken record at this point.

Perhaps this diagram will be helpful to point you in some directions to get the problems resolved:

4 Likes

Okay. Thank you for helping me out! I’ll fix those mistakes.

I had a quick question. I know I would set the variable carbs to get the json response, but what would I set for the variable “message”?

1 Like

I think @muneer’s suggestion was to use labels to display intermediate steps in your code. So instead of running the entire set of blocks and wondering why it doesn’t work, you can debug your code by adding “set label’s text to” blocks every so often and checking values that you are getting from blocks (e.g. from JSON, etc.). You can even put Wait blocks and/or Vibrate blocks in so that you know where you are in the code when something does/does not work.

This is what I tend to do in my own projects:

2 Likes

Is there still an error with the way this is set up? When I live test it says there is something wrong with the blocks.

1 Like

Yes, it’s still not correct. Please watch my API/JSON tutorial video to see how to set up the “get object from JSON” and “get property of object” blocks and how my if/else blocks are structured.

2 Likes

You can see an example of proper JSON parsing (just one way to do it but a good way) here:

3 Likes

Many thanks @tatiang

I highly appreciate your praise to my work.

3 Likes

Thank you for all the help! I’m going to work on it and I’ll show you what I come up with in a couple of hours.

Should it be something like this?

Can someone please help me fix these errors, I need to finish this app today?

1 Like

You are using a variable carb and you have assigned it the object of the API response.

Then you try to compare this object with simple numbers. This will never yield true.

The other issue, you need to keep in mind that else if condition evaluation is progressive which means it will check the first condition and if it happens to be true then no more conditions will be checked or tested. However, if the first condition comes up false, the next else if will be tested and again the next condition will only be tested when the current condition is false.

The way you listed the condition in the code does not follow this role and therefore will not work properly.

1 Like

What should I replace for the object of the api response instead of the variable carbs?

1 Like

Also is this how I should keep my if/else blocks?
image

1 Like

Let’s do what they call it dry run.

Assume the number you have from your API is 19.

What will happen with the blocks?

The first block is testing if the number is greater than or equal 12.5 so this will be true. And the label Very health will have the text “Very healthy” showing on it.

Let’s see the next condition, is the number (19) greater than or equal18, the answer is yes and the label Somewhat healthy will show the text “Somewhat healthy” on it.

Let’s proceed to the third block, is the number less than or equal 19, the answer is no and the label will not get updated.

Now the app is confusing the user. It has two labels with text in them. One saying “Very healthy” and the other saying “Somewhat healthy”.

This is not good.

1 Like

I changed the if/else statements, but it still shows an error with the blocks. Are the get properties parsed wrong? Or is there another error?

You’ve written nutriments instead of nutrients.
image

1 Like

They called it “nutriments”.
image

1 Like

Sorry, my bad. Didn’t notice.

1 Like

That’s okay! Thanks for helping!

So basically, the food is healthy if the carbs are less than 12.5?