Skip to content

Instantly share code, notes, and snippets.

@aryapreetam
Last active May 11, 2021 11:38
Show Gist options
  • Save aryapreetam/6ea61200c5c0d04b6705fd7238f5080d to your computer and use it in GitHub Desktop.
Save aryapreetam/6ea61200c5c0d04b6705fd7238f5080d to your computer and use it in GitHub Desktop.
const s = "Dealing with failure is easy: Work hard to improve. Success is also easy to handle: You’ve solved the wrong problem. Work hard to improve"
function sumOfSentence(input: string){
const vowels = new Set(['a','e','i','o','u'])
let sum = 0;
for(let i=0; i<input.length; i++){
const character = input.charAt(i);
const asciiValue = input.charCodeAt(i)
if(asciiValue >= 65 && asciiValue <= 90 || asciiValue >= 97 && asciiValue <= 122){
if(vowels.has(character.toLowerCase())){
sum -= asciiValue;
}else{
sum += asciiValue
}
}
}
return sum;
}
console.log(sumOfSentence(s))
function sumOfAllOddNumbersInFibonacciLessThan(threshold: number){
let first = 0;
let second = 1;
let next = 0;
let sum = first + second;
while(next < threshold){
if(next % 2 !== 0){
sum += next;
}
next = first + second;
first = second;
second = next;
console.log(next)
}
return sum;
}
console.log(sumOfAllOddNumbersInFibonacciLessThan(13))
function getXCount(num: number){
let integerToRomanMap = new Map<number, string>([
[90, "XC"], [80, "LXXX"], [70, "LXX"], [60, "LX"],
[50, "L"], [40, "XL"], [30, "XXX"], [20, "XX"],
[10, "X"], [9, "IX"], [8, "VIII"], [7, "VII"], [6, "VI"],
[5, "V"], [4, "IV"], [3, "III"], [2, "II"], [1, "I"]
])
let countX = 0;
for(let i=10; i<=num; i++){
let num = i%100;
while(num > 0){
let len = String(num).length;
let divisor = Math.pow(10, len - 1)
let quotient = Math.floor(num/divisor)
num = num % divisor
if(integerToRomanMap.has(divisor * quotient)){
let value = integerToRomanMap.get(divisor * quotient)
countX += getCount(value)
}else{
while(quotient > 0){
let value = integerToRomanMap.get(divisor)
quotient--;
countX += getCount(value)
}
}
}
}
return countX;
}
function getCount(value: string){
let countX = 0;
for(let j=0; j<value.length; j++){
if(value.charAt(j) === 'X'){
countX++;
}
}
return countX;
}
console.log(convertToRoman(10), getXCount(10))
console.log(convertToRoman(20), getXCount(20))
console.log(convertToRoman(2660),getXCount(2660))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment