const checkAnagrams = (str1, str2) => {
const result1 = {};
const result2 = {};
const count = (target, str) => {
const len = str.length;
for (let i = 0; i < len; i++) {
const item = str[i].toLowerCase();
if (!target[item]) {
target[item] = 1;
} else {
target[item] += 1;
}
}
return target;
}
count(result1, str1);
count(result2, str2);
const len = Math.max(str1.length, str2.length);
for (let i = 0; i < len; i++) {
if (!str1[i]) {
return false;
}
const item = str1[i].toLowerCase();
if (result1[item] !== result2[item]) {
return false;
}
}
return true;
};
// console.log(checkAnagrams('feel', 'LeFe'));
const capitalizeStr = (str) => {
const len = str.length;
if (len < 1) {
return str;
}
const strArr = str.split(' ');
return strArr.map((item, index) => {
return item[0].toUpperCase() + item.slice(1)
}).join(' ');
}
// console.log(capitalizeStr('hello! my name is test.'))
const fibonacciSequence = (index) => {
const queue = [];
for (let i = 0; i < index; i++) {
if (i === 0 || i === 1) {
queue[i] = 1;
} else {
queue[i] = queue[i - 1] + queue[i - 2];
}
}
return queue[index - 1];
};
// console.log(fibonacciSequence(3));
const filterEvenNumber = (arr) => {
// const len = arr.length;
// const result = [];
// for (let i = 0; i < len; i++) {
// if (arr[i] % 2 !== 0) {
// result.push(arr[i]);
// }
// }
// return result;
// return arr.filter((item) => item % 2 !== 0);
// return arr.reduce((acc, item) => {
// if (item % 2 !== 0) {
// acc.push(item);
// }
// return acc;
// }, []);
// return arr.flatMap((item) => {
// if (item % 2 === 0) {
// return []
// }
// return [item];
// });
};
// console.log(filterEvenNumber([1, 2, 3, 4, 5]));
const maxCharacter = (str) => {
const map = {};
let max = 0;
let maxStr = '';
for (let item of str) {
if (!map[item]) {
map[item] = 1;
} else {
map[item] += 1;
}
if (map[item] > max) {
max = map[item];
maxStr = item;
}
}
return { [maxStr]: max };
};
// console.log(maxCharacter('apple 122231'));
const checkPalindromes = (str) => {
const len = str.length;
for (let i = 0, j = len - 1; i <= j; i++) {
if (str[i] !== str[j]) {
return false
}
j -= 1;
}
return true;
};
// console.log(checkPalindromes('abcrtcba'));
const checkPermutationPalindromes = (str) => {
const len = str.length;
let i = 0;
let j = len - 1;
while (i <= j) {
if (str[i] === str[i + 1]) {
i += 2;
continue;
}
if (str[j] === str[j - 1]) {
j -= 2;
continue;
}
if (str[i] !== str[j]) {
return false;
}
i += 1;
j -= 1;
}
return true;
}
// console.log(checkPermutationPalindromes('iviccc'));
const reverseInt = (num) => {
const result = [];
const float = num < 0;
let intNum = Math.abs(num | 0);
let index = 0;
while (intNum >= 10) {
const item = intNum % 10;
intNum = (intNum / 10) | 0;
if (item !== 0) {
index += 1;
}
if (index !== 0) {
result.push(item);
}
}
result.push(intNum);
const newInt = Number(result.join(''));
return float ? -newInt : newInt;
};
// console.log(reverseInt(-10234000))
const reverseStr = (str) => {
const len = str.length;
let result = '';
for (let i = len - 1; i >= 0; i--) {
result += str[i];
}
return result;
};
// console.log(reverseStr('abcde'));
const sumArray = (arr) => {
// const len = arr.length;
// let result = 0;
// for(let i = 0;i < len;i++) {
// result += arr[i];
// }
// return result;
return arr.reduce((acc, item) => acc + item, 0);
};
// console.log(sumArray([1, 2, 3, 4]));
const findVowels = (str) => {
const vowelsArr = ['a', 'e', 'i', 'o', 'u'];
let times = 0;
for (let item of str) {
if (vowelsArr.includes(item.toLowerCase())) {
times += 1;
}
}
return times;
};
// console.log(findVowels('Easy'));
console