Skip to content

Instantly share code, notes, and snippets.

@eduardo-borges-goncalves
Created January 5, 2022 02:30
Show Gist options
  • Save eduardo-borges-goncalves/630a3fe7c372765b61ce178543db07db to your computer and use it in GitHub Desktop.
Save eduardo-borges-goncalves/630a3fe7c372765b61ce178543db07db to your computer and use it in GitHub Desktop.
const main = (params) => {
let array = [];
let index = 0
function run() {
let j = 0;
while (j < params + 1) {
randomString();
j++
}}
function randomString(){
let letters = 'abcdefghijklmnopqrstuvwxyz';
let random = '';
if (array == 0) {
for (var i = 0; i < 5 ; i++) {
var rnum = Math.floor(Math.random() * letters.length);
random += letters.substring(rnum, rnum + 1);
}
array.push(random)
return random
}
else if (array.length <= 1) {
for (var i = 0; i < 4 ; i++) {
var rnum = Math.floor(Math.random() * letters.length);
random += letters.substring(rnum, rnum + 1);
}
index = 1
array.push(random)
return random
}
else if (array[index].length == 4) {
for (var i = 0; i < 3 ; i++) {
var rnum = Math.floor(Math.random() * letters.length);
random += letters.substring(rnum, rnum + 1);
}
index = 0
array.push(random)
return random
}
else if (array[index].length < 5) {
for (var i = 0; i < 4 ; i++) {
var rnum = Math.floor(Math.random() * letters.length);
random += letters.substring(rnum, rnum + 1);
}
index = 1
array.push(random)
return random
}
else if (array[index].length < 6) {
for (var i = 0; i < 5 ; i++) {
var rnum = Math.floor(Math.random() * letters.length);
random += letters.substring(rnum, rnum + 1);
}
index = 2
array.push(random)
return random
}
}
run()
// PART TWO
let index2 = 0
let resultsArray = [];
function findPol (string) {
let stringCheck = string.split('').reverse().join('')
if (string == stringCheck ) {
resultsArray.push(string)
}
}
let t = 0
while (t < params + 1) {
findPol(array[index2])
t++
index2++
}
console.log(`Number of palindromes: ${resultsArray.length}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment