Skip to content

Instantly share code, notes, and snippets.

@gupta-pratik
Created April 27, 2017 07:14
Show Gist options
  • Save gupta-pratik/8300351a300457ca364fcc438ecc8555 to your computer and use it in GitHub Desktop.
Save gupta-pratik/8300351a300457ca364fcc438ecc8555 to your computer and use it in GitHub Desktop.
Marco Polo Problem. (Frequently asked interview question)
// This function solves the Marco Polo Problem
// Input is number till which you want to print values
// Returns a single string which performs thee logic of Marco Polo.
function marcoPoloFunc(number){
var retVal='';
for(var i=1; i<=number;i++){
if(i%4==0 && i%7==0) // checks if number is divisible by both 4 and 7
retVal+='marcopolo'
else if(i%4==0) // checks if number is divisible by 4
retVal+='marco'
else if(i%7==0) // checks if number is divisible by 7
retVal+='polo'
else
retVal+=i;
retVal+=',';
}
retVal =retVal.substring(0,retVal.length-1);
console.log(retVal); // print to console.
return retVal; // returns the single string with required output
}
@dev-socialcurry
Copy link

//How about using array for this

var n =100;

var marcopolo = function(limit){ // return the desired output by calling the marcopolo() function

for(var i=1; i<=limit; i++){
	if(((i%4)==0) && ((i%7)==0)){
		outputArray.push("marcopolo");
	}else if((i%4)==0){
		outputArray.push("marco");
	}else if((i%7)==0){
		outputArray.push("polo");
	}else{
		outputArray.push(i);
	}
}

var output = outputArray.toString(); //Desired output printed as string

return output;	//Returning the output

}

console.log(marcopolo(n));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment