-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e45f5e
commit 3bc4d44
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* Checks if a given String is a Palindrome */ | ||
let isPalindrome = (string) => { | ||
|
||
let backwardString = string.split('').reverse().join('') | ||
|
||
if(string === backwardString){ | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
/* Solution Function */ | ||
let largestPalindromeProduct = (numberOfdigits) => { | ||
|
||
/* Generates Array of Numbers Available to Multiply */ | ||
let arrayOfNumbers = [] | ||
let current = 0 | ||
while(!(current.toString().length > numberOfdigits)){ | ||
if(current.toString().length === numberOfdigits){ | ||
arrayOfNumbers.push(current) | ||
} | ||
current = current + 1 | ||
} | ||
|
||
let largestProduct = 0 | ||
let i | ||
let j | ||
/* Iterate through Array and Multiply all Numbers with each other */ | ||
for(i = 0; i < arrayOfNumbers.length; i ++){ | ||
for(j = i; j < arrayOfNumbers.length; j ++){ | ||
let product = arrayOfNumbers[i] * arrayOfNumbers[j] | ||
// If product is a palindrome and larger than the largest | ||
// product discovered so far, update it | ||
if(isPalindrome(product.toString()) && product > largestProduct){ | ||
largestProduct = product | ||
} | ||
} | ||
} | ||
|
||
return largestProduct | ||
|
||
} | ||
|
||
|
||
/* Check Solution */ | ||
console.log(largestPalindromeProduct(3)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "project-euler-js", | ||
"version": "1.0.0", | ||
"description": "JavaScript Solutions for Project Euler problems.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ganeshh123/project-euler-js.git" | ||
}, | ||
"author": "Ganesh H", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/ganeshh123/project-euler-js/issues" | ||
}, | ||
"homepage": "https://github.com/ganeshh123/project-euler-js#readme" | ||
} |