Niall Gallagher
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(my label).val ue = string.slice(startSlice, splitSize).toString();
var startSlice = (startSlice + splitSize);
};
}
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(my
var startSlice = (startSlice + splitSize);
};
}
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(my label).val ue = string.slice(startSlice, splitSize).toString();
startSlice = (startSlice + splitSize);
};
}
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(my
startSlice = (startSlice + splitSize);
};
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
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.