Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Create a function in JavaScript that prints the numbers from 1 to n. For multiples of 3, print "Fizz" instead of the number. For multiples of 5, print "Buzz" instead of the number. For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.(Leetcode-412)
- // function fizzBuzz(n) {
- // const resultArr = []
- // for (let i = 1; i <= n; i++) {
- // if (i % 3 === 0 && i % 5 === 0) {
- // resultArr.push('FizzBuzz')
- // } else if (i % 3 === 0) {
- // resultArr.push('Fizz')
- // } else if (i % 5 === 0) {
- // resultArr.push('Buzz')
- // } else {
- // resultArr.push(i.toString())
- // }
- // }
- // return resultArr
- // }
- // fizzBuzz(3) // ['1', '2', 'Fizz']
- // fizzBuzz(5)// ['1', '2', 'Fizz', 4, 'Buzz']
- fizzBuzz(15) //["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
- // Create a function in JavaScript that counts the number of vowels in a given string.
- //O(n)
- // const countVowels = (str) => {
- // const vowels = 'aeoiouAEOIU'
- // let count = 0
- // for (let char of str) {
- // if (vowels.includes(char)) {
- // count++
- // }
- // }
- // return count
- // }
- // console.log(countVowels('hello')) // Output: 2
- // console.log(countVowels('world')) // Output: 1
- // console.log(countVowels('aeiouAEIOU')) // Output: 10
- // console.log(countVowels('bcdfghjklmnp')) // Output: 0
- // console.log(countVowels('')) // Output: 0
- // console.log(countVowels('xyz')) // Output: 0
- //Leetcode-2586
- //TIme and space complexity O(n)
- // var vowelStrings = function (words, left, right) {
- // let count = 0
- // const vowels = 'aeiou'
- // for (let i = left; i <= right; i++) {
- // console.log(words[i][0], words[i][words[i].length - 1])
- // if (
- // vowels.includes(words[i][0]) &&
- // vowels.includes(words[i][words[i].length - 1])
- // ) {
- // count++
- // }
- // }
- // return count
- // }
- // console.log(vowelStrings(['are', 'amy', 'u'], 0, 2))
- //Time complexity - O(n)
- //space complexity - O(n)
- // Leetcode-2129
- // var capitalizeTitle = function (title) {
- // split the title into words and make it lowercase
- // const words = title.toLowerCase().split(' ')
- // const outputStrArr = []
- // for (let word of words) {
- // if (word.length > 2) {
- // outputStrArr.push(word[0].toUpperCase() + word.slice(1))
- // } else {
- // outputStrArr.push(word)
- // }
- // }
- // return outputStrArr.join(' ')
- // }
- // console.log(capitalizeTitle('capiTalIze tHe titLe'))
- // console.log(capitalizeTitle('i lOve leetcode'))
- // Create a function in JavaScript that checks whether a given number is a prime number.
- // A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
- // The function should return false for numbers less than 2.
- // The function should efficiently check for divisors up to the square root of num.
- //O(√n)
- //space complexity O(1)
- // function isPrime(n) {
- // if (n <= 1) return false //0, 1 are not prime numbers
- // if(n <= 3) return true //2, 3 are prime numbers
- // for(let i = 2; i <= Math.sqrt(n); i++){
- // if(n % i === 0) return false
- // console.log(n % i)
- // }
- // return true
- // }
- //5, 7, 11, 13
- //6k + 1 = 7
- //6k - 1 = 5
- //optimization
- //time complexity O(√n).
- //space complexity O(1)
- // function isPrime(n) {
- // if (n <= 1) return false
- // if (n <= 3) return true
- // if (n % 2 === 0 || n % 3 === 0) return false
- // for (let i = 5 ; i <= Math.sqrt(n); i+= 6 ){
- // if(n % i === 0 || n % (i + 2) === 0) return false
- // }
- // return true
- // }
- // // Test cases
- // console.log(isPrime(2)) // true
- // console.log(isPrime(3)) // true
- // console.log(isPrime(4)) // false
- // console.log(isPrime(5)) // true
- // console.log(isPrime(29)) // true
- // console.log(isPrime(97)) // true
- // console.log(isPrime(100)) // false
- // Create a function in JavaScript that removes duplicate values from an array and returns a new array containing only unique values
- // function removeDuplicates() {}
- // console.log(removeDuplicates([1, 2, 2, 3, 4, 4])) // Output: [1, 2, 3, 4]
- // console.log(removeDuplicates(['a', 'b', 'a', 'c', 'b'])) // Output: ['a', 'b', 'c']
- // console.log(removeDuplicates([true, false, true, true])) // Output: [true, false]
- // console.log(removeDuplicates([1, '1', 2, '2', 2])) // Output: [1, '1', 2, '2']
- // console.log(removeDuplicates([])) // Output: []
- // console.log(removeDuplicates([n ull, null, undefined, undefined])) // Output: [null, undefined]
- //Leetcode-217
- // var containsDuplicate = function (nums) {
- // const set = new Set()
- // for (num of nums) {
- // if (set.has(num)) {
- // return true
- // } else {
- // set.add(num)
- // }
- // }
- // return false
- // }
- // Write a function to reverse a string(Leetcode-344)
- //Create a function in JavaScript that takes a string as input and returns a new string with the characters in reverse order.
- // const reverseString = (str) => {}
- // Example of usage:
- // console.log(reverseString("hello")); // Output: "olleh"
- // console.log(reverseString("world")); // Output: "dlrow"
- // console.log(reverseString("!dlroW ,olleH")); // Output: "Hello, World!"
- // console.log(reverseString("")); // Output: ""
- // console.log(reverseString("12345")); // Output: "54321"
- // console.log(reverseString("racecar")); // Output: "racecar"
- // Create a function in JavaScript that finds the intersection of two arrays, returning a new array containing elements that are present in both input arrays. (Leetcode-349)
- var intersection = function (nums1, nums2) {
- const arr1Set = Array.from(new Set(nums1))
- const arr2Set = new Set(nums2)
- const resultArr = []
- for (num of arr1Set) {
- if (arr2Set.has(num)) {
- resultArr.push(num)
- }
- }
- return resultArr
- }
- console.log(intersection([1, 2, 2, 1], [2, 2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement