Skip to content

Instantly share code, notes, and snippets.

@bappi2097
Created August 27, 2024 16:44
Show Gist options
  • Save bappi2097/e5a457e523e9c130ee27450b8ff74544 to your computer and use it in GitHub Desktop.
Save bappi2097/e5a457e523e9c130ee27450b8ff74544 to your computer and use it in GitHub Desktop.
/**
* problem 01
*
* add(num)
*
* @param {number} nums
* @returns {number}
*/
function add(num) {
let temp = 0, tempNum = num;
if(tempNum>9){
while(tempNum>0){
temp += tempNum % 10;
tempNum = parseInt(tempNum / 10);
}
return add(temp)
}
return tempNum
}
console.log(add(38))
console.log(add(0))
/**
* problem 02
*
* hasDuplicate(nums)
*
* @param {Array} nums
* @returns {boolean}
*/
function hasDuplicate(nums) {
const maps = []
for(let i=0; i<nums.length; i++){
if(maps.includes(nums[i])){
return true
}
maps.push(nums[i])
}
return false
}
console.log(hasDuplicate([1,2,3,1]))
console.log(hasDuplicate([1,2,3,4]))
console.log(hasDuplicate([1,1,1,3,3,4,3,2,4,2]))
/**
* problem 03
*
* reverseVowels(str)
*
* @param {string} str
* @returns {string}
*/
function reverseVowels(str) {
let tempStr = str, strArr = [], vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'], count = 0;
for(let i=0; i<str.length; i++){
if(vowels.includes(str[i])){
strArr.push(str[i])
}
}
for(let i=0; i<str.length; i++){
if(vowels.includes(str[i])){
tempStr = tempStr.slice(0, i) + strArr[strArr.length - ++count] + tempStr.slice(i+1, tempStr.length)
}
}
return tempStr
}
console.log(reverseVowels("hello") == 'holle')
console.log(reverseVowels("algorithm") == 'ilgorathm')
console.log(reverseVowels("aA") == 'Aa')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment