Repeat a function in Javascript with .length

I have a long string.

var string = "This is a long string."

I also have a function to download the string to "path".

downloadfunction{... 'echo "'+string+'" >> '+path;}

How do I execute this function for every 2 letters of the string? Read somewhere about using ".length" but not sure how to implement it in this case. I also do not want to split the string into an array. The download function should help split the string to download 2 letters progressively.

I.e. I would like to download the string 2 letters at a time.

EDIT: To clarify, the string needs to be downloaded x characters at a time as the download would break if it exceeds that limit.

Answers


Here is an example that is commented on how to do this:

var string = 'a really really really long string \
    that I want to split by two letters at a time';

// this needs to be ceiling so that it always rounds up on odd numbers
// otherwise the last letter may be left out
var iterations = Math.ceil(string.length / 2);

for (var i = 0; i < iterations; i++) {

    // we are iterating over half of the length but want to go two letters at a time
    var j = i*2;

    // make a new string with the first letter
    var letters = string[j]

    // this is an if statement to check if there is a second letter
    // if there is concat it to the first letter
    // otherwise the last set of an odd length would concat `undefined` to it
    if (string[j+1]) { letters += string[j+1]; }

    // call your function here passing `letters` to it
    downloadfunction{... 'echo "' + letters + '" >> '+path;}
}

count string length divide by 2 round up or down what ever you like

then do a for loop of the amount after the devision.

Somethig like this

//Your string that is downloaded
var string = "This is a long string."

//calculate the amount of character in the string
var amount = string.length;

//divide the string by 2
var roundnr = amount/2;

for (var i=0;i<Math.round(roundnr);i++)
{ 
//do something here for every 2 characters in the string
}

Need Your Help

Setting SESSION elements inside php using data sent from Android

php android session http-headers

I am trying to send data from Android to php which has to save the data in SESSION. However, my session is still empty. On the other side, when I set SESSION from a website, it works. I assume it is

doesnot show any validation error messages in laravel5

php laravel laravel-5

I have used this function in controller to store the records of the users with validations. The data is not stored if the validation is not met but it doesnot show any validation error message.