samedi 13 avril 2019

Creating a state diagram for a simple loop function

I have a simple function just returning the mirror image of the string passed. The code looks as follows:

function reverse(str){
        let reversed = "";    
        for (var i = str.length - 1; i >= 0; i--){        
            reversed += str[i];
        }    
        return reversed;
 }

The above system starts with an empty reversed value and goes on updating itself inside the loop. How would you enumerate the states in the above system? The states that I could figure out are empty --> updated. This is it (Is this even correct?)

I am trying to understand the importance of state diagrams and how it could lead to finding out bugs with corner cases. So a state diagram with a simple system as above was something I wanted to start with.

Aucun commentaire:

Enregistrer un commentaire