Just a Little Puzzle
There is a company that ask their candidates to answer a puzzle before applying for their opening job. You can find the puzzle here.
Well, I just want to trying to have a nostalgia with my past time when I was in college. So, I wrote 3 type of the solutions using JavaScript, here they are:
<script class="Solution1" language="javascript">
var agroup = ""
var nextCounterToPrint = 5;
var currentCounter = 0; //just for increasing speed
for(var i = 1; i <= 1000; i++){
agroup += i.toString();
currentCounter++;
if(currentCounter == nextCounterToPrint){
nextCounterToPrint = (nextCounterToPrint == 5) ? 3 : 5;
currentCounter = 0;
if(!isFirstTime(i)){
agroup = "*" + agroup;
}
document.write(agroup);
agroup = ""
}
}
function isFirstTime(i){
return (i == 5);
}
</script >
<script class="LineBreak" language="javascript">
document.write("<BR>");
</script >
<script class="Solution2" language="javascript">
var printed = 0;
var toggled = false;
for(var i = 1; i <= 1000; i++){
document.write(i);
printed++;
if(printed == 5 && !toggled)
{
document.write("*");
printed = 0;
toggled = true;
}
else if(printed == 3 && toggled && i < 1000){
document.write("*");
printed = 0;
toggled = false;
}
}
</script >
<script class="LineBreak" language="javascript">
document.write("<BR>");
</script >
<script class="Solution3" language="javascript">
var lists = [5,8,13,16,21,24,29,32,37,40,45,48,53,56,61,64,69,72,77,80,85,88,93,96,101,104,109,112,117,120,125,128,133,136,141,144,149,152,157,160,165,168,173,176,181,184,189,192,197,200,205,208,213,216,221,224,229,232,237,240,245,248,253,256,261,264,269,272,277,280,285,288,293,296,301,304,309,312,317,320,325,328,333,336,341,344,349,352,357,360,365,368,373,376,381,384,389,392,397,400,405,408,413,416,421,424,429,432,437,440,445,448,453,456,461,464,469,472,477,480,485,488,493,496,501,504,509,512,517,520,525,528,533,536,541,544,549,552,557,560,565,568,573,576,581,584,589,592,597,600,605,608,613,616,621,624,629,632,637,640,645,648,653,656,661,664,669,672,677,680,685,688,693,696,701,704,709,712,717,720,725,728,733,736,741,744,749,752,757,760,765,768,773,776,781,784,789,792,797,800,805,808,813,816,821,824,829,832,837,840,845,848,853,856,861,864,869,872,877,880,885,888,893,896,901,904,909,912,917,920,925,928,933,936,941,944,949,952,957,960,965,968,973,976,981,984,989,992,997];
for(var i = 1; i <= 1000; i++){
document.write(i);
if(i == lists[0]){
lists.shift();
document.write("*");
}
}
</script >
Which one do you think is the best one? Or maybe you have another solution, please share!