Reputation:
I have a function that doesn't work:
function gapAll( ary2, ary )//perform gap() on all of the values of ary2
{
for( i = 0; i < ary2.length; i++ )
{
gap( ary2[ i ], ary );
}
}
If I perform this manually, it works:
function gapAll( ary2, ary )//perform gap() on all of the values of ary2
{
gap( 0, ary );
gap( 1, ary );
gap( 2, ary );
gap( 3, ary );
gap( 4, ary );
gap( 5, ary );
gap( 6, ary );
gap( 7, ary );
gap( 8, ary );
gap( 9, ary );
}
My goal is to do this with a loop, not manually. below is all of my code...I cant figure this out...I just want to loop through ary2's indexes and use them for gap. I've shortened my code to only items used by this function:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>test</title>
<style>
html
{
background-color: #0f0f0f;
color: #ccc;
font-family:Arial;
font-size:90%;
}
</style>
</head>
<body>
<header>
</header>
<section id = "body">
<h3>result</h3>
<p id = "trg">...</p>
</section>
<footer>
</footer>
<script>
/*|||||||||||||||||||||||||||||||||||||| BASE ARRAYS ||||||||||||||||||||||||||||||||||||||*/
digits = [];//digits 1 - 9
for( i = 0; i < 10; i++ )
{
digits.push( i );
window[ 'digit' + i ] = 0;
} digitsCpy = digits.slice( 0 );
/*||||||||||||||||||||||||||||||||||||||| FUNCTIONS |||||||||||||||||||||||||||||||||||||||*/
function gapAll( ary2, ary )//gap
{
for( i = 0; i < ary2.length; i++ )
{
gap( ary2[ i ], ary );
}
}
function gap( v, ary )//gap
{
v = v.toString();
places = [];
if( v.toString().length == 1 )
{
var counter = 0;
var identifier = new RegExp( v );
for( i = 0; i < ary.length; i++ )
{
if( identifier.test( ary[ i ] ) == true )
{ places.push( i ) }
}
var gaps = places.slice( 0 );
for( i = 0; i < places.length; i++ )
{
if( i > 0 )
{
j = i - 1;
gaps[ i ] = places[ i ] - places[ j ] - 1;
}
}
window[ 'digit' + v ] = gaps.slice( 0 );
if( window[ 'digit' + v ] == '' )
{ window[ 'digit' + v ] = 0; }
}
else if( v.toString().length == 2 )
{
x = v.slice( 0, 1 );
y = v.slice( 1, 2 );
var counter = 0;
var identifier = new RegExp( x + ".*" + y + "|" + y + ".*" + x );
for( i = 0; i < ary.length; i++ )
{
if( identifier.test( ary[ i ] ) == true )
{ places.push( i ) }
}
var gaps = places.slice( 0 );
for( i = 0; i < places.length; i++ )
{
if( i > 0 )
{
j = i - 1;
gaps[ i ] = places[ i ] - places[ j ] - 1;
}
}
window[ 'pair' + v ] = gaps.slice( 0 );
if( window[ 'pair' + v ] == '' )
{ window[ 'pair' + v ] = 0; }
}
else if( v.toString().length == 3 )
{
x = v.slice( 0, 1 );
y = v.slice( 1, 2 );
z = v.slice( 2, 3 );
var counter = 0;
var identifier = new RegExp
(
x + ".*" + y + ".*" + z + "|" + x + ".*" + z + ".*" + y + "|" +
y + ".*" + x + ".*" + z + "|" + y + ".*" + z + ".*" + x + "|" +
z + ".*" + x + ".*" + y + "|" + z + ".*" + y + ".*" + x
);
for( i = 0; i < ary.length; i++ )
{
if( identifier.test( ary[ i ] ) == true )
{ places.push( i ) }
}
var gaps = places.slice( 0 );
for( i = 0; i < places.length; i++ )
{
if( i > 0 )
{
j = i - 1;
gaps[ i ] = places[ i ] - places[ j ] - 1;
}
}
window[ 'set' + v ] = gaps.slice( 0 );
if( window[ 'set' + v ] == '' )
{ window[ 'set' + v ] = 0; }
}
}
function addDim( m )//add dimension to m
{
if( Array.isArray( m ) )
{
for( i = 0; i < m.length; i++ )
{
if( m[ i ].length > 1 )
{
m[ i ] = m[ i ].split( '' );
}
}
return m;
}
else
{
ary = m.split( ' ' );
return ary;
}
}
/*||||||||||||||||||||||||||||||||||||||| MAIN ||||||||||||||||||||||||||||||||||||||||*/
ary = [ 409,879,483,465,907,154,838,847,432,434,842,401 ];
gapAll( digits, ary );
document.getElementById( 'trg' ).innerHTML = digit3;
</script>
</body>
</html>
Just to make this easier to understand the whole idea of the gap( v, ary )
function is to find "gaps" or the space in between values in arrays. so if i was looking for 5 in the array [ 5, 2, 5 ]. gap( 5, ary
) would return ( 0, 1 ). because 5 was at the first index( 0 ), and then it skipped 1 index (2) before repeating at the 3rd index.
This may be something simple as I am a newbie. Thanks for any help provided.
Upvotes: 0
Views: 98
Reputation: 201447
Your two functions aren't doing the same thing. First, your should scope the variable i
and then your manual version would be more like
for (var i = 0; i < ary2.length; i++) {
gap(i, ary);
}
Edit
If it was coincidental to your unrolled version, then use
for (var i = 0; i < ary2.length; i++) {
gap(ary2[i], ary);
}
Upvotes: 4
Reputation: 43852
All your i
variables are globally-scoped! This means that all your for
loops are using the exact same counter variable, and when you use different loops in different functions, it messes with the global counter state. You can fix this by using the var
keyword whenever you declare local variables, like so:
function gapAll(ary2, ary) {
for(var i = 0; i < ary2.length; i++) {
gap(ary2[i], ary);
}
}
You should similarly update all your other variable declarations to use var
, otherwise they'll continue to pollute the global scope and can cause serious problems like you've already observed. This includes both loop counter variables and function-local declarations.
See also: What is the function of the var keyword and when to use it (or omit it)?
Upvotes: 7