Ad
Code
Diff
  • export let reverseInt = (n: number)=>+[...""+n].reverse().join('')
    
    
    
    • export function reverseInt(n: number) {
    • return +String(n).replace(/\d/g, (_, i, a) => a.slice(a.length - i - 1, a.length - i))
    • }
    • export let reverseInt = (n: number)=>+[...""+n].reverse().join('')
Code
Diff
  • export function validateString(input: string, index: number): boolean {
      const validator = new BracketsValidator();
      return [...input].splice(index, input.length-index).every(e=>validator.isValidChar(e));
    }
    
    class BracketsValidator {
      private readonly bracketPairs: Record<string, string> = {
        '{': '}',
        '[': ']',
        '(': ')',
      };
      private readonly openBrackets: Set<string> = new Set(Object.keys(this.bracketPairs));
      private readonly closingBrackets: Set<string> = new Set(Object.values(this.bracketPairs));
      private stack: string[] = [];
      
      public isValidChar(char: string): boolean {
        if (this.isNotBracket(char)) {
          return true;
        }
        
        if (this.isOpeningBracket(char)) {
          this.push(char);
          return true;
        }
        
        if (this.hasMatchFor(char)) {
          this.pop();
          return true;
        }
        
        return false;
      }
      
      private push(bracket: string): void {
        this.stack.push(bracket);
      } 
      
      private pop(): string|undefined {
        return this.stack.pop();
      }
      
      private lastFromStack(): string|undefined {
        return this.stack.slice(-1)[0];
      }
      
      private hasMatchFor(bracket: string): boolean {
        const previousBracket: string|undefined = this.lastFromStack();
        
        if (undefined === previousBracket) {
          return false;
        }
        
        const match = this.matchFor(bracket);
        
        return previousBracket === match;
      }
      
      private isOpeningBracket(char: string): boolean {
          return this.openBrackets.has(char);
      }
      
      private isClosingBracket(char: string): boolean {
        return this.closingBrackets.has(char)
      }
      
      private isNotBracket(char: string): boolean {
      return !(this.isOpeningBracket(char)||this.isClosingBracket(char));
      }
      
      private matchFor(bracket: string): string {
        const index: number = Object.values(this.bracketPairs).indexOf(bracket);
        return Object.keys(this.bracketPairs)[index];
      }
    }
    
    • export function validateString(input: string, index: number): boolean {
    • const validator = new BracketsValidator();
    • for (let i = index; i < input.length; i++) {
    • const char = input[i];
    • if (!validator.isValidChar(char)) {
    • return false;
    • }
    • }
    • return true;
    • return [...input].splice(index, input.length-index).every(e=>validator.isValidChar(e));
    • }
    • class BracketsValidator {
    • private readonly bracketPairs: Record<string, string> = {
    • '{': '}',
    • '[': ']',
    • '(': ')',
    • };
    • private readonly openBrackets: Set<string> = new Set(Object.keys(this.bracketPairs));
    • private readonly closingBrackets: Set<string> = new Set(Object.values(this.bracketPairs));
    • private stack: string[] = [];
    • public isValidChar(char: string): boolean {
    • if (this.isNotBracket(char)) {
    • return true;
    • }
    • if (this.isOpeningBracket(char)) {
    • this.push(char);
    • return true;
    • }
    • if (this.hasMatchFor(char)) {
    • this.pop();
    • return true;
    • }
    • return false;
    • }
    • private push(bracket: string): void {
    • this.stack.push(bracket);
    • }
    • private pop(): string|undefined {
    • return this.stack.pop();
    • }
    • private lastFromStack(): string|undefined {
    • return this.stack.slice(-1)[0];
    • }
    • private hasMatchFor(bracket: string): boolean {
    • const previousBracket: string|undefined = this.lastFromStack();
    • if (undefined === previousBracket) {
    • return false;
    • }
    • const match = this.matchFor(bracket);
    • if (previousBracket !== match) {
    • return false;
    • }
    • return true;
    • return previousBracket === match;
    • }
    • private isOpeningBracket(char: string): boolean {
    • if (this.openBrackets.has(char)) {
    • return true;
    • }
    • return false;
    • return this.openBrackets.has(char);
    • }
    • private isClosingBracket(char: string): boolean {
    • if (this.closingBrackets.has(char)) {
    • return true;
    • }
    • return false;
    • return this.closingBrackets.has(char)
    • }
    • private isNotBracket(char: string): boolean {
    • if (this.isOpeningBracket(char)) {
    • return false;
    • }
    • if (this.isClosingBracket(char)) {
    • return false;
    • }
    • return true;
    • return !(this.isOpeningBracket(char)||this.isClosingBracket(char));
    • }
    • private matchFor(bracket: string): string {
    • const index: number = Object.values(this.bracketPairs).indexOf(bracket);
    • return Object.keys(this.bracketPairs)[index];
    • }
    • }
Code
Diff
  • power=(n,p)=> p == 0 ? 1 : n**p
    • power=(n,p)=> p === 0 ? 1 : Math.pow(n,p)
    • power=(n,p)=> p == 0 ? 1 : n**p
Code
Diff
  • const countVowel = s => s.split(/[aeiou]/i).length-1
      
    • const countVowel = s => s.match(/[aeiou]/gi)?.length | 0
    • const countVowel = s => s.split(/[aeiou]/i).length-1
Code
Diff
  • const countVowel = s => s.split(/[aeiou]/i).length-1
      
    • const countVowel = s => s.match(/[aeiou]/gi)?.length | 0
    • const countVowel = s => s.split(/[aeiou]/i).length-1