Ad

Insure that both arguments are number. For example sum("1", 1) returns "11"

Code
Diff
  • function sum(a,b) {
      return Number(a)+Number(b); // wrong returning
    }
    • function sum(a,b) {
    • return a+b; // wrong returning
    • return Number(a)+Number(b); // wrong returning
    • }

Implement a function, which gets string, splits it onto numbers and words. After it wraps numbers in div tag, words in span and remove spaces at the end and begining.

Example:

  1. transformText("Two 2"):<span>Two</span><div>2</div>
  2. transformText('12 people in 2 rooms'): <div>12</div><span>people in</span><div>2</div><span>rooms</span>
function transformText(string) {
  const regExp = /[\D]+|([\d+\.]+)/g
  const numsRegExp = /[0-9\.]+/g
  
  const splited = string.match(regExp)
  
  const res = splited.map((item) => {
    if (numsRegExp.test(item)) {
      return `<div>${item}</div>`
    } else {
      return `<span>${item.replace(/^\s|\s$/g, "")}</span>`
    }
  })
  
  return res.join("")
}
Code
Diff
  • const findMax = arr => Math.max(...arr);
    
    • const findMax = arr => arr.reduce((a, c) => c > a ? c : a, -Infinity);
    • const findMax = arr => Math.max(...arr);