Link to home
Start Free TrialLog in
Avatar of Niall Gallagher
Niall GallagherFlag for Ireland

asked on

String.slice not working in array

This is probably a school boy question but I can't figure it out, this is the whole function.
when I run the function it repeats like it should but it returns data from the string.slice the first time only

function sliceString() {
    const newLocal = 0;
    var i = newLocal;
    var startSlice = newLocal;
    var string = 'This is another string';
    var totstr = string.length;
    var splitSize = 6;  
    while (startSlice < totstr) {        
        ++i;
     var mylabel = ("label" + i);
        document.getElementById(mylabel).value = string.slice(startSlice, splitSize).toString();      
        var startSlice = (startSlice + splitSize);    
  };
}
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Why are you redefining the startSlice variable in the while loop just assign to it like :
function sliceString() {
    const newLocal = 0;
    var i = newLocal;
    var startSlice = newLocal;
    var string = 'This is another string';
    var totstr = string.length;
    var splitSize = 6;   
    while (startSlice < totstr) {        
        ++i;
     var mylabel = ("label" + i);
        document.getElementById(mylabel).value = string.slice(startSlice, splitSize).toString();       
        startSlice = (startSlice + splitSize);     
  };
} 

Open in new window

I think if you use the var keyword in the while loop rather than just assigning to the current variable you are actually creating a new variable local to the while loop even though they have the same name they are in a different scope.
Avatar of Niall Gallagher

ASKER

When I looked at your response and I thought you were probably right but unfortunately not. I put a watch on "string.slice(startSlice, splitSize).toString()" and the first time it runs it brings back "this i" but then in following runs it brings back nothing.

This how my code looks now

function sliceString() {
    "use strict"
    const newLocal = 0;
    var i = newLocal;
    var startSlice = newLocal;
    var string = 'This is another string';
    var totstr = string.length;
    var splitSize = 6;      
    var mylabel;    
    while (startSlice < totstr) {        
        ++i;      
        mylabel = ("label" + i);
        document.getElementById(mylabel).value = string.slice(startSlice, splitSize).toString();      
        startSlice = (startSlice + splitSize);        
     };
}
ASKER CERTIFIED SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Paul,
Thank you and I have learnt something, I always thought the second parameter was the amount you want to go ( in my project 6 characters) not where you wanted to go to, (start at 6 and 6 characters, 12).

Thank you
No problem, glad I could help, got to admit I made the same assumption when I first looked at your code.
It sounds like you may be remembering substr(), which is only not officially deprecated due to backwards compatibility concerns.