Skip to content

Commit

Permalink
ewrfesr
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshh123 committed Oct 11, 2020
1 parent 5e45f5e commit 3bc4d44
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 4-largest_palindrome_product.js
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))
19 changes: 19 additions & 0 deletions package.json
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"
}

0 comments on commit 3bc4d44

Please sign in to comment.