Loop do while javascript

2

I wanted to know if someone could help me, I'm doing an exercise in javascript in which I collect cookies I leave the code:

window.onload = function(){
            var contador = 0;
            do{
                contador=contador+1;
                var cookieLeida=getCookie("carrito"+contador);
                //alert(cookieLeida);
                var datos = cookieLeida.split("&");
                //alert(datos);
                insertaCookie(datos[0],datos[1],datos[2],datos[3],datos[4]);
            }while(cookieLeida!="");
        }

What I do is assign an accountant because my cookies store them with the name cart and an accumulated one, that is, my cookies are cart1, cart2, etc, so until they are stored everything works well for me and picks up the cookies without problems, but also returns a last cookie in white for the structure of the while, my question is if someone can tell me how to prevent me from returning that blank cookie or ignore it so that it does not show it on the screen when adding the data of the cookie in an array. Thanks in advance

    
asked by dgomez 28.04.2017 в 04:12
source

3 answers

4

Another possibility:

window.onload = function(){
        for (var contador = 1; ; contador++ ) {
            var cookieLeida=getCookie("carrito"+contador);
            if(cookieLeida == '') break;
            var datos = cookieLeida.split("&");
            insertaCookie(datos[0],datos[1],datos[2],datos[3],datos[4]);
        }
    }
    
answered by 28.04.2017 / 04:27
source
1

A third alternative, without using loops:

(function testCookies(contador = 0) {
  var cookieLeida = getCookie("carrito" + contador);
  if (cookieLeida === '') { return; }
  var datos = cookieLeida.split("&");
  insertaCookie(datos[0], datos[1], datos[2], datos[3], datos[4]);
  testCookies(contador + 1);
})();

It's a simple IIFE that will continue running as long as there are cookies.

    
answered by 28.04.2017 в 04:41
0

We changed the do while, for a while:

window.onload = function(){
            var contador = 0;
            var cookieLeida= "cookie";
            while(cookieLeida!=""){
                contador=contador+1;
                var cookieLeida=getCookie("carrito"+contador);
                //alert(cookieLeida);
                var datos = cookieLeida.split("&");
                //alert(datos);
                insertaCookie(datos[0],datos[1],datos[2],datos[3],datos[4]);
            }
        }

Method suggested by WC3SCHOOLS:

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
   }
    return "";
}

Explanation:

The cname parameter will be supplied by the name of the cookie.

The variable name will store the name of the cookie and its value. That's why we link it to you =

Cookies are a string, so we continue to decode it.

We create an array ca where we will save each piece of the decoded cookie

With the for cycle we traverse the array to obtain each value of the pieces of the cookie, we assign it to c

As long as the first character (chartAt) is equal to empty space, we are going to cut it (substring)

To continue we use the indexOf, if it returns 0, it is because there is an object in that position (false would be -1) that is, cookies were found, we proceed to return the value.

    
answered by 28.04.2017 в 04:18