Ad

Two roman numbers are passed as strings.

You need to compare them and return -1, if the first is greater, 0 if they are equal and 1 otherwise

Code
Diff
  • function fromRoman(roman) {
      const letters = roman.split('');
      const keys = { M: 1_000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };
    
      return letters.reduce((n, key, i) => {
        keys[letters[i + 1]] > keys[key] ? n -= keys[key] : n += keys[key]
        return n;
      }, 0);
    }
    
    function compareRoman(r1, r2) {
      const [n1, n2] = [r1, r2].map(fromRoman);
      if (n1 === n2) return 0;
      return n1 > n2 ? 1 : -1;
    }
    
    • def from_roman(num):
    • roman_numerals = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
    • result = 0
    • for i, c in enumerate(num):
    • if (i+1) == len(num) or roman_numerals[c] >= roman_numerals[num[i+1]]:
    • result += roman_numerals[c]
    • else:
    • result -= roman_numerals[c]
    • return result
    • function fromRoman(roman) {
    • const letters = roman.split('');
    • const keys = { M: 1_000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };
    • def compare_roman(a, b):
    • return letters.reduce((n, key, i) => {
    • keys[letters[i + 1]] > keys[key] ? n -= keys[key] : n += keys[key]
    • return n;
    • }, 0);
    • }
    • a = from_roman(a)
    • b = from_roman(b)
    • if a < b:
    • return -1
    • elif a == b:
    • return 0
    • else:
    • return 1
    • function compareRoman(r1, r2) {
    • const [n1, n2] = [r1, r2].map(fromRoman);
    • if (n1 === n2) return 0;
    • return n1 > n2 ? 1 : -1;
    • }
Code
Diff
  • function prime_checker(num) {
      if (num <= 1) return false;
      if (num == 2) return true;
      if (num % 2 == 0) return false;
      const boundary = Math.floor(Math.sqrt(num));
    
      for (let i = 3; i <= boundary; i += 2)
        if (num % i === 0) return false;
      return true;   
    }
    • function prime_checker (n) {
    • // Write a javascript function to check if N is a prime number
    • function prime_checker(num) {
    • if (num <= 1) return false;
    • if (num == 2) return true;
    • if (num % 2 == 0) return false;
    • const boundary = Math.floor(Math.sqrt(num));
    • for (let i = 3; i <= boundary; i += 2)
    • if (num % i === 0) return false;
    • return true;
    • }