I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
as the variable initialization is inside the loop, it will be created anew at each iteration. Also, b is not accessible outside of loop.
You need a variable created at function level, and then updated inside the loop.
when you initialise a variable inside a loop, then it will exist only in the single iteration. It will be created anew each time. If you want instead to change the variable, you need to declare it outside the loop…