Wow, That is too awesome. I am downloading the code on my sublime text, and try to understand it. Thank you so much for your prompt support and instant reply.
Hi There @coothead
Thank you so much for writing such a code, and your code is a great work to learn and acquire some new knowledge,
But sir irrespective of what I actually posted my agenda was to learn something as in GIF:
Write a character of a word one by one in the forwarding direction, and delete them in the backward direction.
Words will also change after the first word is written and deleted.
And the same process will be repeated for other words stored in an array or maybe some other way.
I was able to write it down for one word in one direction, but could not delete it and also could not further add an additional word to be written and deleted.
When you shift() you are removing the element from the array so after you have shifted them all there is nothing left to work with. The array is empty.
You then try to pop() an empty array. Even if you did get the last item in the array with pop() it would not rub out the last letter that was just placed. You would want everything in the array apart from the last one and output a smaller and smaller string each time (not just the last item). It may have been easier just to slice strings as in the example from @coothead but I guess you were practicing with arrays.
I made a small codepen trying to follow your rough logic and ended up with this.
It’s not meant as a solution but more to work out what’s happening. I’m sure it could be shortened considerably.
I was thinking that some other work would be impenetrable. Plugging away at it with enough solutions to different problems though has ended up surprising and scaring myself at the progress that can be made.
The real breakthrough came when I actively stopped procrastination, by using a new thread to guide others through the process, as an excuse to make progress on the matter
It’s much the same (but in reverse) as using the array.length number which gives you the number of items which is one more than the position in the array as the array starts from zero. When reducing the array zero will be the first item in the array so I had to check for -1 to know that it had completed its journey.
When the array reaches -1 we can’t pop any more because the array is empty and in order to reverse the process I refill the array with the original values and then it all starts again
Not very elegant I know but it does its job:) I’m sure there is an easier logic just waiting to jump out at me
I just checked the code and it seems that you are using i for when counting forward, and j for when counting backwards.
A possibly easier solution is to use only one variable called pos, and an increment variable that switches from +1 to -1 instead.
Step 1: Rename i to pos
Step 2: Use pos += increment and invert increment when bounds are reached
Step 3: Check if increment is >0 or <0 for each if statement
Step 4: Replace j with pos instead.
Bonus step: replace -1 checks with suitable 0 checks instead, for added clarity.
I tend to disagree about needing more energies. Whenever something starts to be complex, there are almost always techniques that can be used to make it simpler and easier.
Some call it lazy, others call it efficnentefficnent.<backspace fail> something else instead.
For me the real breakthrough came when I understood that debugging your own code is several more times difficult than it was to write the code.
That has a major impact when you write code at the limit of your ability. Debugging that code then becomes highly difficult indeed. The remedy is to use techniques that leave yourself plenty of headroom, so that debugging becomes a lot easier later on too.
Simple and easy tend to be frowned upon by people intent on pushing their muscles. There’s more going for it than at first thought.