Created
January 10, 2016 18:49
-
-
Save mmloveaa/56fcf9eef02152738d2d to your computer and use it in GitHub Desktop.
Vampire Numbers
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
// 1/10/2016 | |
// Vampire Numbers | |
// Our loose definition of Vampire Numbers can be described as follows: | |
// 6 * 21 = 126 | |
// # 6 and 21 would be valid 'fangs' for a vampire number as the | |
// # digits 6, 1, and 2 are present in both the product and multiplicands | |
// 10 * 11 = 110 | |
// # 110 is not a vampire number since there are three 1's in the | |
// # multiplicands, but only two 1's in the product | |
// Create a function that can receive two 'fangs' and determine if the | |
// product of the two is a valid vampire number. | |
// Test.describe("Testing For Vampire Numbers", function(){ | |
// Test.it("should work for some static tests", function(){ | |
// Test.assertEquals(vampire_test(21,6), true, "Basic: 21 * 6 = 126 should return true") | |
// Test.assertEquals(vampire_test(204,615) , true, "Basic: 204 * 615 = 125460 should return true") | |
// Test.assertEquals(vampire_test(30,-51) , true, "One Negative: 30 * -51 = -1530 should return true") | |
// Test.assertEquals(vampire_test(-246,-510) , false, "Double Negatives: -246 * -510 = 125460 should return false (The negative signs aren't present on the product)") | |
// Test.assertEquals(vampire_test(2947050,8469153) , true, "Large: 2947050 * 8469153 = 24959017348650 should return true") | |
// Test.assertEquals(vampire_test(2947051,8469153) , false, "Large: 2947051 * 8469153 = 24959025817803 should return false") | |
// }); | |
// }); | |
var vampire_test = function(a, b){ | |
if (a<=0 && b<=0){ | |
return false | |
} | |
var product=a*b | |
var arr1=Math.abs(product).toString().split("") | |
var arrA=Math.abs(a).toString().split("") | |
var arrB=Math.abs(b).toString().split("") | |
var arrAB=arrA.concat(arrB) | |
arrAB= arrAB.sort(function(a,b){ | |
return a-b | |
}); | |
arr1=arr1.sort(function(a,b){ | |
return a-b | |
}); | |
console.log(arr1) | |
console.log(arrAB) | |
for (var i=0; i<arrAB.length; i++){ | |
if (!((arr1[i])===(arrAB[i]))){ | |
return false | |
} | |
} | |
return true; | |
} | |
vampire_test(10,1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment