Discuss Scratch
- Nighylol
-
11 posts
help building a simple script
I tried to quote but something went wrong
here is the link that I tried a different answer other than @1guy44's which was perfect
just to get a better understanding on this
https://scratch.mit.edu/projects/488695814
here is the link that I tried a different answer other than @1guy44's which was perfect
just to get a better understanding on this
https://scratch.mit.edu/projects/488695814
- 1guy444
-
92 posts
help building a simple script
@Oumuamua's script is good, but it has a flaw in it that causes it to misfunction. This is the corrected version.
Explanation below.
when green flag clickedI only changed one thing, and I commented on it.
set [minor v] to ((1)/(0)) //1/0 works because the result is infinity according to Scratch. And any number is smaller than infinity.
set [i v] to (0)
repeat (len of [list v] :: list)
change [i v] by (1)
if <(item (i) of [list v] :: list) < (minor)> then
set [minor v] to (item (i) of [list v] :: list)
end
end
set [SmallestProduct v] to ((1)/(0))
set [i v] to (0)
repeat until ((i) = (length of [list v] :: list)) //change is here; instead of using repeat (n) times use repeat until (condition)
change [i v] by (1)
if <(item (i) of [list v] :: list) = (minor)> then //part 1
change [i v] by (1)
end
if <((item (i) of [list v] :: list) * (minor)) < (SmallestProduct)> then //part 2
set [SmallestProduct v] to ((item (i) of [list v] :: list)*(minor))
end
end
say (SmallestProduct)
The reason for using
repeat until <(i) = (len of [list v] :: list)>
end
instead of
repeat (len of [list v] :: list)
end
is because the repeat until block checks the variable i to see if it is equal to list length. If it is equal, it stops.
When the repeat until block reaches the number that is equal to the variable minor (part 1), it adds i by 1 in addition to the 1 that is added at the start of each round(skips the same number). Then, after skipping to the next number, it checks if the number times the smallest number is less than the current SmallestProduct and changes it accordingly, in the same loop.
When using the repeat () times block, for each skip there will be an extra loop at the end, resulting in results of (nothing). That is because the index is pointing to the position of an item on the list that doesn't exist.
Because the repeat () times block doesn't care if i goes over the length of the list, its only job is to execute the script inside of it a specific number of times. When 3 in this case gets skipped, there are only six items left in the list. But the block will still run 7 times.
The results that have no value are automatically regarded by Scratch as less than any real number, so they pass the if condition (part 2) and multiplies by the minor number, resulting in a SmallestProduct of 0.
But, when the same script is used with the repeat until (condition) block, it checks at the start of every loop whether i has reached the last item of the list yet.
If it has, it stops right away, so it prevents the index from pointing to a place in the list that isn't real.
When 3 gets skipped, there are 6 other items in the list, but i is also increased by 1 so i is 1 loop closer to the length of the list (7), preventing an extra loop.
I tried to explain as simply as I could, I hope you understand. But it's okay if you don't understand right now, you'll understand eventually

Hope this helps!
Last edited by 1guy444 (Feb. 17, 2021 04:26:08)
- Oumuamua
-
1000+ posts
help building a simple script
I did a mistake, the second loop must repeat ((length of list)-1)


Last edited by Oumuamua (Feb. 17, 2021 06:56:11)
- deck26
-
1000+ posts
help building a simple script
https://scratch.mit.edu/projects/488952709/ is my version which works as long as the numbers are all non-negative and copes with duplicate numbers. It only needs one loop.
Last edited by deck26 (Feb. 17, 2021 09:39:14)
- 1guy444
-
92 posts
help building a simple script
I did a mistake, the second loop must repeat ((length of list)-1)
Yeah, but that won't work if there are is more than one duplicate of the minimum. The fix I posted works regardless of how many duplicates there are.
I think @Nighylol has enough examples to learn from now

Last edited by 1guy444 (Feb. 17, 2021 11:48:16)
- Oumuamua
-
1000+ posts
help building a simple script
I think that it is now fixed for good.
Greetings.
Greetings.
Last edited by Oumuamua (Feb. 17, 2021 14:34:05)
- Oumuamua
-
1000+ posts
help building a simple script
I did a mistake, the second loop must repeat ((length of list)-1)
Yeah, but that won't work if there are is more than one duplicate of the minimum. The fix I posted works regardless of how many duplicates there are.
I think @Nighylol has enough examples to learn from now
LOL??? Uhmm… ead my lips little troll
Last edited by Oumuamua (Feb. 19, 2021 10:34:44)
- Nighylol
-
11 posts
help building a simple script
Yeah, but that won't work if there are is more than one duplicate of the minimum. The fix I posted works regardless of how many duplicates there are.
I think @Nighylol has enough examples to learn from now
LOL, yeah I'm reading through everything and try other methods
thanks to all of you I know know the repeat/if condition and lists clearly