Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Find the biggest item of a list. (Numbers not length)
- nijona
-
27 posts
Find the biggest item of a list. (Numbers not length)
My list adds keeps adding items and when it's done I want it to set a variable to the biggest number inside.
- deck26
-
1000+ posts
Find the biggest item of a list. (Numbers not length)
So set a variable to the first item and then loop through the list looking for a bigger value. Each time you find one update the variable.
- MegaApuTurkUltra
-
1000+ posts
Find the biggest item of a list. (Numbers not length)
So let's work through how you might solve this:
- what's the definition of the largest number in a list?
- how can you go through each item in the list to check if it's the largest?
- how can you check if one number is larger than another? Now how can you find the largest number out of three numbers? Four? Even more than that?
- now what steps could you take to make this check for largest number in scratch?
- gtoal
-
1000+ posts
Find the biggest item of a list. (Numbers not length)
Don't scan the list looking for the biggest number. What you should do is check if the number you are adding is bigger than the biggest you've seen before, and if it is, it then becomes the biggest number seen so far. I.e. do it *while you are adding*, not afterwards. (I assume you are not deleting numbers from the list.)
Last edited by gtoal (Sept. 25, 2016 04:39:54)
- rodriguezrrp
-
32 posts
Find the biggest item of a list. (Numbers not length)
Try something like this:
We start with a list that has some data in it, here named “list”:
Note: the variable (#) is used to iterate over the list's items.
Also note: code works only if the list just contains numbers. If there are one or more items in the list that contain letter characters (anything besides 0-9) it will probably return some wrong value.
We start with a list that has some data in it, here named “list”:
Now we will create the script which starts at the beginning of the list, thinking of that item as the “biggest” so far. It will then go through all of the list, looking at each item as it goes, and comparing it with the “biggest” number, and if it is bigger then that becomes the new “biggest”.
When the code is complete, the largest value in (list) should be contained in (biggest).
Note: the variable (#) is used to iterate over the list's items.
Also note: code works only if the list just contains numbers. If there are one or more items in the list that contain letter characters (anything besides 0-9) it will probably return some wrong value.
- nijona
-
27 posts
Find the biggest item of a list. (Numbers not length)
I found out how to do it already and it worked. No more posts are needed.
- rodriguezrrp
-
32 posts
Find the biggest item of a list. (Numbers not length)
Ok sorry about that.
- ScratchSafe2
-
13 posts
Find the biggest item of a list. (Numbers not length)
delete this post
Last edited by ScratchSafe2 (Feb. 1, 2025 14:45:06)
- Discussion Forums
- » Help with Scripts
-
» Find the biggest item of a list. (Numbers not length)