Skip to content

Instantly share code, notes, and snippets.

@jonathas
Created September 29, 2021 12:11
Show Gist options
  • Save jonathas/b36d18a2d0eb8ac5731b43838d614934 to your computer and use it in GitHub Desktop.
Save jonathas/b36d18a2d0eb8ac5731b43838d614934 to your computer and use it in GitHub Desktop.
Palindrome, Fibonacci, First non repeating index
function isPalindrome1(word) {
const len = word.length;
for (let i = 0; i < len / 2; i++) {
if (word[i] !== word[len-1-i]) {
return false;
}
}
return true;
}
function isPalindrome2(word) {
const values = word.split("");
const reverse = values.reverse();
const reverseValues = reverse.join('');
return word == reverseValues;
}
console.log(isPalindrome2("krk"));
console.log(isPalindrome2("petr"));
function getFibonacci(num) {
let n1 = 0, n2 = 1, nextNumber;
let fibonacci = [];
for (let i = 1; i <= num; i++) {
fibonacci.push(n1);
nextNumber = n1+n2;
n1 = n2;
n2 = nextNumber;
}
return fibonacci;
}
function firstNonRepeatingIndex(input) {
const setSize = new Set(input).size;
if (setSize == input.length || setSize === 1) {
return -1;
}
const characters = input.split("");
for (let x = 0; x < characters.length; x++) {
let char = input.charAt(x);
if (input.indexOf(char) == x && input.indexOf(char, x+1) == -1) {
return input.indexOf(char);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment