Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 4
- const reverseArray = arr => {
- let newArr = [];
- for (let number of arr) {
- newArr.unshift(number);
- }
- return newArr;
- };
- console.log(reverseArray([1, 2, 1, 0]));
- // 5
- const palindrome = input => {
- const textInputArr = input
- .toLowerCase()
- .split('')
- .filter(sign => sign.match(/[a-z]/i));
- for (let i = 0, j = textInputArr.length - 1; i < j; i++, j--) {
- if (textInputArr[i] !== textInputArr[j]) return console.log('NO');
- }
- console.log('YES');
- };
- palindrome('4ABabA%');
- // 6
- const permutation = (arr1, arr2) => {
- const newArr1 = [...arr1];
- if (arr1.length !== arr2.length) return console.log('NO');
- for (let number of arr2) {
- const index = newArr1.indexOf(number);
- if (index > -1) newArr1.splice(index, 1);
- }
- if (newArr1.length > 0) return console.log('NO');
- console.log('YES');
- };
- permutation([1, 2, 3, 5, 4], [2, 4, 3, 1, 5]);
- // 8
- const findingPrimes = (n, m) => {
- const isPrime = n => {
- if (n <= 3) return n > 1;
- else if (n % 2 === 0 || n % 3 === 0) return false;
- let i = 5;
- while (i * i <= n) {
- if (n % i === 0 || n % (i + 2) === 0) return false;
- i += 6;
- }
- return true;
- };
- let numberOfPrimes = 0;
- for(let i = n; i <= m; i++) {
- if (isPrime(i)) numberOfPrimes++;
- }
- return numberOfPrimes;
- };
- console.log(findingPrimes(1, 10));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement