Created
April 27, 2017 07:14
-
-
Save gupta-pratik/8300351a300457ca364fcc438ecc8555 to your computer and use it in GitHub Desktop.
Marco Polo Problem. (Frequently asked interview question)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//How about using array for this
var n =100;
var marcopolo = function(limit){ // return the desired output by calling the marcopolo() function
}
console.log(marcopolo(n));