Welcome to a simple programming skills test. The exercises are in JavaScript. There are
no grades yet and this is trivially simple to hack, but in the future I hope to package this
as SCORM, plug it into Moodle, and make job applicants pass it before they can be interviewed.
Have fun!
Take a look at the calls that will be made to your code. Fill in
the code so that it functions according to the comments.
It Worked!
Incorrect.
The above code illustrates one possible solution to the stated problem.
There is an exception in your code.
Your code must consist of a matched pair of braces that contains several function declarations.
You can click 'START OVER' to restore the function stubs, though you will lose all your work.
Your numbers are not correct.
{
// do any initial setup here
initialize : function()
{
},
// this method will be called for every bean
dropBean : function( color )
{
},
// return the number of beans of a given color
countByColor : function( color )
{
},
// return the total number of beans
countBeans : function()
{
}
};
{
// do any initial setup here
initialize : function()
{
this.total = 0;
this.counts = { };
},
// this method will be called for every bean
dropBean : function( color )
{
this.total ++;
if (this.counts[color] == undefined)
this.counts[color] = 1;
else
this.counts[color] ++;
},
// return the number of beans of a given color
countByColor : function( color )
{
return this.counts[color];
},
// return the total number of beans
countBeans : function()
{
return this.total;
}
};
{
// do any initial setup here
initialize : function()
{
},
// return the value of the correct property
heresTheBag : function( bag )
{
},
};
{
// do any initial setup here
initialize : function()
{
},
// return the value of the correct property
heresTheBag : function( bag )
{
for (var i in bag)
{
if (confirm("Is '" + i + "' the right choice?"))
return bag[i];
}
},
};
{
// do any initial setup here
initialize : function()
{
},
// return the value of the correct property
findNeedle : function( haystack )
{
},
};
{
// do any initial setup here
initialize : function()
{
},
// return the value of the correct property
findNeedle : function( haystack )
{
return this.recurser( haystack, "" );
},
// this function recurses to find the needle
recurser : function( haystack, nameStart )
{
for (var i in haystack)
{
if (haystack[i] == "needle")
return nameStart + i;
if (haystack[i] == "")
continue;
var name = this.recurser( haystack[i], nameStart + i + "." );
if (name != "")
return name;
}
return "";
}
};
{
// do any initial setup here
initialize : function()
{
},
// find mapping between parent and child DNA
match : function( parents, children )
{
}
};
{
// do any initial setup here
initialize : function()
{
},
// find mapping between parent and child DNA
match : function( parents, children )
{
var answer = { };
for (var p in parents)
{
var pBase = parents[p];
var bestName;
var bestDiff = 1000;
for (var c in children)
{
var cBase = children[c];
var diff = this.countDifferences( pBase, cBase );
if (diff < bestDiff)
{
bestName = c;
bestDiff = diff;
}
}
answer[p] = bestName;
}
return answer;
},
countDifferences : function( base1, base2 )
{
var different = 0;
for (var n=0; n < base1.length; n++)
{
if (base1.charAt( n ) != base2.charAt( n ))
different ++;
}
return different;
}
};
{
// do any initial setup here
initialize : function()
{
},
// return number of words in word find
countWords : function( arr )
{
}
};
{
// do any initial setup here
initialize : function()
{
},
// return number of words in word find
countWords : function( arr )
{
var nWords = 0;
var word = [ "W", "O", "R", "D" ];
// measure depth (dimensions)
var nDims = 1;
for (var a = arr; a instanceof Array; a = a[0])
nDims ++;
// iterate through each dimension that the word could occur in
for (var axis=0; axis < nDims; axis++)
{
// iterate through all possible starting points
var start = [ ];
for (var n=0; n < nDims; n++)
start.push( 0 );
var loops = 0;
for (;;)
{
loops ++;
// check for a word here
var match = true;
for (var chk=0; chk < 4; chk++)
{
start[axis] = chk;
var a = arr;
for (var i=0; i < nDims; i++)
a = a[ start[i] ];
if (a != word[chk])
{
match = false;
break;
}
}
if (match)
nWords ++;
// increment to next starting point (skipping the axis being tested)
var more = false;
for (var pos=0; pos < nDims; pos++)
{
if (pos == axis)
continue;
if (start[pos] < 3)
{
more = true;
start[pos] ++;
break;
}
start[pos] = 0;
}
if (! more)
break;
}
alert( 'axis=' + axis + ', loops=' + loops + ', nWords=' + nWords );
}
return nWords;
}
};