Prompt:
/* Given a list of numbers, return the sum of squares of the numbers
  in the list that are odd. Ignore numbers that are negative or not integers.
  doubleTheDifference([1, 3, 2, 0]) == 1 + 9 + 0 + 0 = 10
  doubleTheDifference([-1, -2, 0]) == 0
  doubleTheDifference([9, -2]) == 81
  doubleTheDifference([0]) == 0
  If the input list is empty, return 0.
  */
const doubleTheDifference = (lst) => {

  let p = 0
  for (let i = 0; i < lst.length; i++) {
    if (lst[i] % 2 == 1 && lst[i] > 0) {
      p += lst[i] * lst[i]
    }
  }
  return p
}

const testDoubleTheDifference = () => {
  console.assert(doubleTheDifference([]) === 0)
  console.assert(doubleTheDifference([5, 4]) === 25)
  console.assert(doubleTheDifference([0.1, 0.2, 0.3]) === 0)
  console.assert(doubleTheDifference([-10, -20, -30]) === 0)
  console.assert(doubleTheDifference([-1, -2, 8]) === 0)
  console.assert(doubleTheDifference([0.2, 3, 5]) === 34)
  let lst = []
  let odd_sum = 0
  for (let i = -99; i < 100; i += 2) {
    if (i % 2 != 0 && i > 0) { odd_sum += i * i }
    lst.push(i)
  }
  console.assert(doubleTheDifference(lst) === odd_sum)
}
testDoubleTheDifference()

const testDoubleTheDifference = () => {
  console.assert(doubleTheDifference([1,3,2,0]) === 10)
  console.assert(doubleTheDifference([-1,-2,0]) === 0)
  console.assert(doubleTheDifference([9,-2]) === 81)
  console.assert(doubleTheDifference([0]) === 0)
}
testDoubleTheDifference()


Prompt:
/*I think we all remember that feeling when the result of some long-awaited
  event is finally known. The feelings and thoughts you have at that moment are
  definitely worth noting down and comparing.
  Your task is to determine if a person correctly guessed the results of a number of matches.
  You are given two arrays of scores and guesses of equal length, where each index shows a match.
  Return an array of the same length denoting how far off each guess was. If they have guessed correctly,
  the value is 0, and if not, the value is the absolute difference between the guess and the score.

  example:

  compare([1,2,3,4,5,1],[1,2,3,4,2,-2]) -> [0,0,0,0,3,3]
  compare([0,5,0,0,0,4],[4,1,1,0,0,-2]) -> [4,4,1,0,0,6]
  */
const compare = (game, guess) => {

  for (let i = 0; i < guess.length; i++) {
    game[i] -= guess[i]
 if (game[i]<0)
 game[i]=-game[i];  }
  return game
}

const testCompare = () => {
  console.assert(
    JSON.stringify(compare([1, 2, 3, 4, 5, 1], [1, 2, 3, 4, 2, -2])) ===
    JSON.stringify([0, 0, 0, 0, 3, 3])
  )
  console.assert(
    JSON.stringify(compare([0,5,0,0,0,4],[4,1,1,0,0,-2])) ===
    JSON.stringify([4,4,1,0,0,6])
  )
}
testCompare()


Prompt:
/*You will be given the name of a class (a string) and a list of extensions.
  The extensions are to be used to load additional classes to the class. The
  strength of the extension is as follows: Let CAP be the number of the uppercase
  letters in the extension's name, and let SM be the number of lowercase letters
  in the extension's name, the strength is given by the fraction CAP - SM.
  You should find the strongest extension and return a string in this
  format: ClassName.StrongestExtensionName.
  If there are two or more extensions with the same strength, you should
  choose the one that comes first in the list.
  For example, if you are given "Slices" as the class and a list of the
  extensions: ['SErviNGSliCes', 'Cheese', 'StuFfed'] then you should
  return 'Slices.SErviNGSliCes' since 'SErviNGSliCes' is the strongest extension
  (its strength is -1).
  Example:
  for strongestExtension('my_class', ['AA', 'Be', 'CC']) == 'my_class.AA'
  */
const strongestExtension = (class_name, extensions) => {

  let u = 0
  let s = -Infinity
  for (let i = extensions.length - 1; i >= 0; i--) {
    let y = 0
    for (let j = 0; j < extensions[i].length; j++) {
      let k = extensions[i][j].charCodeAt()
      if (k >= 65 && k <= 90) { y += 1 }
      if (k >= 97 && k <= 122) { y -= 1 }
    }
    if (y >= s) {
      s = y;
      u = i;
    }
  }
  return class_name + '.' + extensions[u]
}

const testStrongestExtension = () => {
  console.assert(
    strongestExtension('Watashi', ['tEN', 'niNE', 'eIGHt8OKe']) ===
    'Watashi.eIGHt8OKe'
  )
  console.assert(
    strongestExtension('Boku123', [
      'nani',
      'NazeDa',
      'YEs.WeCaNe',
      '32145tggg',
    ]) === 'Boku123.YEs.WeCaNe'
  )
  console.assert(
    strongestExtension('__YESIMHERE', [
      't',
      'eMptY',
      'nothing',
      'zeR00',
      'NuLl__',
      '123NoooneB321',
    ]) === '__YESIMHERE.NuLl__'
  )
  console.assert(
    strongestExtension('K', ['Ta', 'TAR', 't234An', 'cosSo']) === 'K.TAR'
  )
  console.assert(
    strongestExtension('__HAHA', ['Tab', '123', '781345', '-_-']) ===
    '__HAHA.123'
  )
  console.assert(
    strongestExtension('YameRore', [
      'HhAas',
      'okIWILL123',
      'WorkOut',
      'Fails',
      '-_-',
    ]) === 'YameRore.okIWILL123'
  )
  console.assert(
    strongestExtension('finNNalLLly', ['Die', 'NowW', 'Wow', 'WoW']) ===
    'finNNalLLly.WoW'
  )
  console.assert(strongestExtension('_', ['Bb', '91245']) === '_.Bb')
  console.assert(strongestExtension('Sp', ['671235', 'Bb']) === 'Sp.671235')
}

testStrongestExtension()

const testStrongestExtension = () => {
  console.assert(
    strongestExtension('my_class', ['AA', 'Be', 'CC']) ===
    'my_class.AA'
  )
}
testStrongestExtension()


Prompt:
/*You are given 2 words. You need to return true if the second word or any of its rotations is a substring in the first word
  cycpatternCheck("abcd","abd") => false
  cycpatternCheck("hello","ell") => true
  cycpatternCheck("whassup","psus") => false
  cycpatternCheck("abab","baa") => true
  cycpatternCheck("efef","eeff") => false
  cycpatternCheck("himenss","simen") => true
  */
const cycpatternCheck = (a, b) => {

  let l = b.length
  let pat = b + b
  for (let i = 0; i < a.length - l + 1; i++) {
    for (let j = 0; j < l + 1; j++) {
      let y = 1
      for (let k = 0; k < l; k++) {
        if (a[i + k] != pat[j + k]) { y = 0 }
      }
      if (y == 1) {
        return true
      }
    }
  }
  return false
}

const testCycpatternCheck = () => {
  console.assert(cycpatternCheck('xyzw', 'xyw') === false)
  console.assert(cycpatternCheck('yello', 'ell') === true)
  console.assert(cycpatternCheck('whattup', 'ptut') === false)
  console.assert(cycpatternCheck('efef', 'fee') === true)
  console.assert(cycpatternCheck('abab', 'aabb') === false)
  console.assert(cycpatternCheck('winemtt', 'tinem') === true)
}

testCycpatternCheck()

const testCycpatternCheck = () => {
  console.assert(cycpatternCheck('abcd', 'abd') === false)
  console.assert(cycpatternCheck('hello', 'ell') === true)
  console.assert(cycpatternCheck('whassup', 'psus') === false)
  console.assert(cycpatternCheck('abab', 'baa') === true)
  console.assert(cycpatternCheck('efef', 'eeff') === false)
  console.assert(cycpatternCheck('himenss', 'simen') === true)
}
testCycpatternCheck()


Prompt:
/*Given an integer. return a list that has the number of even and odd digits respectively.

   Example:
      evenOddCount(-12) ==> [1, 1]
      evenOddCount(123) ==> [1, 2]
  */
const evenOddCount = (num) => {

  let o = 0
  let e = 0
  if (num < 0) { num = -num }
  while (num > 0) {
    if (num % 2 == 0) { e++ }
    else { o++ }
    num = (num - num % 10) / 10
  }
  return [e, o]
}

const testEvenOddCount = () => {
  console.assert(JSON.stringify(evenOddCount(7)) === JSON.stringify([0, 1]))
  console.assert(JSON.stringify(evenOddCount(-78)) === JSON.stringify([1, 1]))
  console.assert(JSON.stringify(evenOddCount(3452)) === JSON.stringify([2, 2]))
  console.assert(
    JSON.stringify(evenOddCount(346211)) === JSON.stringify([3, 3])
  )
  console.assert(
    JSON.stringify(evenOddCount(-345821)) === JSON.stringify([3, 3])
  )
  console.assert(JSON.stringify(evenOddCount(-2)) === JSON.stringify([1, 0]))
  console.assert(
    JSON.stringify(evenOddCount(-45347)) === JSON.stringify([2, 3])
  )
  console.assert(JSON.stringify(evenOddCount(0)) === JSON.stringify([1, 0]))
}

testEvenOddCount()

const testEvenOddCount = () => {
  console.assert(JSON.stringify(evenOddCount(-12)) === JSON.stringify([1, 1]))
  console.assert(JSON.stringify(evenOddCount(123)) === JSON.stringify([1, 2]))
}
testEvenOddCount()


Prompt:
/*
  Given a positive integer, obtain its roman numeral equivalent as a string,
  and return it in lowercase.
  Restrictions: 1 <= num <= 1000

  Examples:
  >>> intToMiniRoman(19) == 'xix'
  >>> intToMiniRoman(152) == 'clii'
  >>> intToMiniRoman(426) == 'cdxxvi'
  */
const intToMiniRoman = (number) => {

const testIntToMiniRoman = () => {
  console.assert(intToMiniRoman(19) === 'xix')
  console.assert(intToMiniRoman(152) === 'clii')
  console.assert(intToMiniRoman(251) === 'ccli')
  console.assert(intToMiniRoman(426) === 'cdxxvi')
  console.assert(intToMiniRoman(500) === 'd')
  console.assert(intToMiniRoman(1) === 'i')
  console.assert(intToMiniRoman(4) === 'iv')
  console.assert(intToMiniRoman(43) === 'xliii')
  console.assert(intToMiniRoman(90) === 'xc')
  console.assert(intToMiniRoman(94) === 'xciv')
  console.assert(intToMiniRoman(532) === 'dxxxii')
  console.assert(intToMiniRoman(900) === 'cm')
  console.assert(intToMiniRoman(994) === 'cmxciv')
  console.assert(intToMiniRoman(1000) === 'm')
}

testIntToMiniRoman()

const testIntToMiniRoman = () => {
  console.assert(intToMiniRoman(19) === 'xix')
  console.assert(intToMiniRoman(152) === 'clii')
  console.assert(intToMiniRoman(426) === 'cdxxvi')
}
testIntToMiniRoman()


Prompt:
/*
 Given the lengths of the three sides of a triangle. Return true if the three
  sides form a right-angled triangle, false otherwise.
  A right-angled triangle is a triangle in which one angle is right angle or
  90 degree.
  Example:
  rightAngleTriangle(3, 4, 5) == true
  rightAngleTriangle(1, 2, 3) == false
  */
const rightAngleTriangle = (a, b, c) => {

const testRightAngleTriangle = () => {
  console.assert(rightAngleTriangle(3, 4, 5) === true)
  console.assert(rightAngleTriangle(1, 2, 3) === false)
  console.assert(rightAngleTriangle(10, 6, 8) === true)
  console.assert(rightAngleTriangle(2, 2, 2) === false)
  console.assert(rightAngleTriangle(7, 24, 25) === true)
  console.assert(rightAngleTriangle(10, 5, 7) === false)
  console.assert(rightAngleTriangle(5, 12, 13) === true)
  console.assert(rightAngleTriangle(15, 8, 17) === true)
  console.assert(rightAngleTriangle(48, 55, 73) === true)
  console.assert(rightAngleTriangle(1, 1, 1) === false)
  console.assert(rightAngleTriangle(2, 2, 10) === false)
}

testRightAngleTriangle()

const testRightAngleTriangle = () => {
  console.assert(rightAngleTriangle(3, 4, 5) === true)
  console.assert(rightAngleTriangle(1, 2, 3) === false)
}
testRightAngleTriangle()


Prompt:
/*Write a function that accepts a list of strings.
  The list contains different words. Return the word with maximum number
  of unique characters. If multiple strings have maximum number of unique
  characters, return the one which comes first in lexicographical order.

  findMax(["name", "of", "string"]) === "string"
  findMax(["name", "enam", "game"]) === "enam"
  findMax(["aaaaaaa", "bb" ,"cc"]) === ""aaaaaaa"
  */
const findMax = (words) => {

  let s = -1
  let u = -1
  if (words.length == 0) { return '' }
  for (let i = 0; i < words.length; i++) {
    let p = 0
    for (let j = 0; j < words[i].length; j++) {
      let y = 1
      for (let k = 0; k < j; k++) {
        if (words[i][j] == words[i][k]) { y = 0 }
      }
      if (y == 1) { p++ }
    }
    if (p > s || (p == s && words[i] < words[u])) {
      u = i;
      s = p;
    }
  }
  return words[u]
}

const testFindMax = () => {
  console.assert(findMax(['name', 'of', 'string']) === 'string')
  console.assert(findMax(['name', 'enam', 'game']) === 'enam')
  console.assert(findMax(['aaaaaaa', 'bb', 'cc']) === 'aaaaaaa')
  console.assert(findMax(['abc', 'cba']) === 'abc')
  console.assert(
    findMax(['play', 'this', 'game', 'of', 'footbott']) === 'footbott'
  )
  console.assert(findMax(['we', 'are', 'gonna', 'rock']) === 'gonna')
  console.assert(findMax(['we', 'are', 'a', 'mad', 'nation']) === 'nation')
  console.assert(findMax(['this', 'is', 'a', 'prrk']) === 'this')
  console.assert(findMax(['b']) === 'b')
  console.assert(findMax(['play', 'play', 'play']) === 'play')
}

testFindMax()

const testFindMax = () => {
  console.assert(findMax(['name', 'of', 'string']) === 'string')
  console.assert(findMax(['name', 'enam', 'game']) === 'enam')
  console.assert(findMax(['aaaaaaa', 'bb', 'cc']) === 'aaaaaaa')
}
testFindMax()


Prompt:
/*
  You're a hungry rabbit, and you already have eaten a certain number of carrots,
  but now you need to eat more carrots to complete the day's meals.
  you should return an array of [ total number of eaten carrots after your meals,
                                  the number of carrots left after your meals ]
  if there are not enough remaining carrots, you will eat all remaining carrots, but will still be hungry.

  Example:
  * eat(5, 6, 10) -> [11, 4]
  * eat(4, 8, 9) -> [12, 1]
  * eat(1, 10, 10) -> [11, 0]
  * eat(2, 11, 5) -> [7, 0]

  Variables:
  @number : integer
      the number of carrots that you have eaten.
  @need : integer
      the number of carrots that you need to eat.
  @remaining : integer
      the number of remaining carrots thet exist in stock

  Constrain:
  * 0 <= number <= 1000
  * 0 <= need <= 1000
  * 0 <= remaining <= 1000

  Have fun :)
  */
const eat = (number, need, remaining) => {

  if (need <= remaining) {
    return [need + number, remaining - need]
  }
  return [remaining + number, 0]
}

const testEat = () => {
  console.assert(JSON.stringify(eat(5, 6, 10)) === JSON.stringify([11, 4]))
  console.assert(JSON.stringify(eat(4, 8, 9)) === JSON.stringify([12, 1]))
  console.assert(JSON.stringify(eat(1, 10, 10)) === JSON.stringify([11, 0]))
  console.assert(JSON.stringify(eat(2, 11, 5)) === JSON.stringify([7, 0]))
  console.assert(JSON.stringify(eat(4, 5, 7)) === JSON.stringify([9, 2]))
  console.assert(JSON.stringify(eat(4, 5, 1)) === JSON.stringify([5, 0]))
}

testEat()

const testEat = () => {
  console.assert(JSON.stringify(eat(5, 6, 10)) === JSON.stringify([11, 4]))
  console.assert(JSON.stringify(eat(4, 8, 9)) === JSON.stringify([12, 1]))
  console.assert(JSON.stringify(eat(1, 10, 10)) === JSON.stringify([11, 0]))
  console.assert(JSON.stringify(eat(2, 11, 5)) === JSON.stringify([7, 0]))
}
testEat()


Prompt:
/*
  Given two lists operator, and operand. The first list has basic algebra operations, and
  the second list is a list of integers. Use the two given lists to build the algebric
  expression and return the evaluation of this expression.

  The basic algebra operations:
  Addition ( + )
  Subtraction ( - )
  Multiplication ( * )
  Floor division ( // )
  Exponentiation ( ** )

  Example:
  operator['+', '*', '-']
  array = [2, 3, 4, 5]
  result = 2 + 3 * 4 - 5
  => result = 9

  Note:
      The length of operator list is equal to the length of operand list minus one.
      Operand is a list of of non-negative integers.
      Operator list has at least one operator, and operand list has at least two operands.

  */
const doAlgebra = (operator, operand) => {

  while (operator.length > 0) {
    let y = 0
    for (let i = operator.length - 1; i >= 0; i--) {
      if (operator[i] == '**') {
        let u = operand[i]
        while (operand[i + 1] > 1) {
          operand[i + 1]--;
          operand[i] *= u;
        }
        operand.splice(i + 1, 1)
        operator.splice(i, 1)
        y = 1;
        break;
      }
    }
    if (y == 1) { continue }
    for (let i = 0; i < operator.length; i++) {
      if (operator[i] == '*') {
        operand[i] *= operand[i + 1]
        operand.splice(i + 1, 1)
        operator.splice(i, 1)
        y = 1;
        break;
      }
      else if (operator[i] == '//') {
        operand[i] = (operand[i] - operand[i] % operand[i + 1]) / operand[i + 1]
        operand.splice(i + 1, 1)
        operator.splice(i, 1)
        y = 1;
        break;
      }
    }
    if (y == 1) { continue }
    for (let i = 0; i < operator.length; i++) {
      if (operator[i] == '+') {
        operand[i] += operand[i + 1]
        operand.splice(i + 1, 1)
        operator.splice(i, 1)
        y = 1;
        break;
      }
      else if (operator[i] == '-') {
        operand[i] -= operand[i + 1]
        operand.splice(i + 1, 1)
        operator.splice(i, 1)
        y = 1;
        break;
      }
    }
    if (y == 1) { continue }
  }
  return operand[0]
}


Prompt:
/*You are given a string s.
  if s[i] is a letter, reverse its case from lower to upper or vise versa,
  otherwise keep it as it is.
  If the string contains no letters, reverse the string.
  The function should return the resulted string.
  Examples
  solve("1234") = "4321"
  solve("ab") = "AB"
  solve("#a@C") = "#A@c"
  */
const solve = (s) => {

  let t = 0
  let p = ''
  for (let i = 0; i < s.length; i++) {
    let y = s[i].charCodeAt()
    if (y >= 65 && y <= 90) {
      y += 32;
      t = 1;
    } else if (y >= 97 && y <= 122) {
      y -= 32;
      t = 1;
    }
    p += String.fromCharCode(y)
  }
  if (t == 1) { return p }
  let u = ''
  for (let i = 0; i < p.length; i++) {
    u += p[p.length - i - 1]
  }
  return u
}

const testSolve = () => {
  console.assert(solve('AsDf') === 'aSdF')
  console.assert(solve('1234') === '4321')
  console.assert(solve('ab') === 'AB')
  console.assert(solve('#a@C') === '#A@c')
  console.assert(solve('#AsdfW^45') === '#aSDFw^45')
  console.assert(solve('#6@2') === '2@6#')
  console.assert(solve('#$a^D') === '#$A^d')
  console.assert(solve('#ccc') === '#CCC')
}

testSolve()

const testSolve = () => {
  console.assert(solve('1234') === '4321')
  console.assert(solve('ab') === 'AB')
  console.assert(solve('#a@C') === '#A@c')
}
testSolve()


Prompt:
/*
  Given a string 'text', return its md5 hash equivalent string.
  If 'text' is an empty string, return null.

  >>> stringToMd5('Hello world') == '3e25960a79dbc69b674cd4ec67a72c62'
  */
const stringToMd5 = (text) => {

  if (text == '') { return null }
  var md5 = require('js-md5')
  return md5(text)
}

const testStringToMd5 = () => {
  console.assert(
    stringToMd5('Hello world') === '3e25960a79dbc69b674cd4ec67a72c62'
  )
  console.assert(stringToMd5('') === null)
  console.assert(stringToMd5('A B C') === '0ef78513b0cb8cef12743f5aeb35f888')
  console.assert(stringToMd5('password') === '5f4dcc3b5aa765d61d8327deb882cf99')
}

testStringToMd5()

const testStringToMd5 = () => {
  console.assert(
    stringToMd5('Hello world') === '3e25960a79dbc69b674cd4ec67a72c62'
  )
}
testStringToMd5()


Prompt:
/*
  Given two positive integers a and b, return the even digits between a
  and b, in ascending order.

  For example:
  generateIntegers(2, 8) => [2, 4, 6, 8]
  generateIntegers(8, 2) => [2, 4, 6, 8]
  generateIntegers(10, 14) => []
  */
const generateIntegers = (a, b) => {

  if (a > b) {
    let tmp = a;
    a = b;
    b = tmp;
  }
  let y = []
  for (let i = a; i <= b; i++) {
    if (i == 2 || i == 4 || i == 6 || i == 8) { y.push(i) }
  }
  return y
}

const testGenerateIntegers = () => {
  console.assert(
    JSON.stringify(generateIntegers(2, 10)) === JSON.stringify([2, 4, 6, 8])
  )
  console.assert(
    JSON.stringify(generateIntegers(10, 2)) === JSON.stringify([2, 4, 6, 8])
  )
  console.assert(
    JSON.stringify(generateIntegers(132, 2)) === JSON.stringify([2, 4, 6, 8])
  )
  console.assert(
    JSON.stringify(generateIntegers(17, 89)) === JSON.stringify([])
  )
}

testGenerateIntegers()

const testGenerateIntegers = () => {
  console.assert(
    JSON.stringify(generateIntegers(2, 8)) === JSON.stringify([2, 4, 6, 8])
  )
  console.assert(
    JSON.stringify(generateIntegers(8, 2)) === JSON.stringify([2, 4, 6, 8])
  )
  console.assert(
    JSON.stringify(generateIntegers(10, 14)) === JSON.stringify([])
  )
}
testGenerateIntegers()