Code Lab

Modo entrevista. Cronômetro de 7 min, sem dicas e sem autocompletar

Pairs by character

FácilMap and Set7 min

You get two arrays of objects like { c, a }. The field c is one character, the field a is an integer. Within one array a character occurs at most once.

Write a function sumOfProducts(first, second). For every character that is in both arrays, multiply their a numbers. Return the sum of these products.

Exemplos

sumOfProducts([{ c: 'a', a: 5 }, { c: 'b', a: 6 }], [{ c: 'b', a: 4 }, { c: 'a', a: 2 }])

Retorna 34

That is 5 * 2 + 6 * 4.
sumOfProducts([{ c: 'x', a: 3 }], [{ c: 'y', a: 7 }])

Retorna 0

There are no common characters.

Cobrada em Google · Fonte

Carregando o editor...
  1. EntradasumOfProducts([ { c: 'a', a: 5 }, { c: 'b', a: 6 } ], [ { c: 'b', a: 4 }, { c: 'a', a: 2 } ])
    Esperado34
  2. EntradasumOfProducts([ { c: 'x', a: 3 } ], [ { c: 'y', a: 7 } ])
    Esperado0
  3. EntradasumOfProducts([ { c: 'a', a: 1 }, { c: 'b', a: 2 }, { c: 'c', a: 3 } ], [ { c: 'c', a: 10 }, { c: 'd', a: 4 } ])
    Esperado30

Os testes ocultos rodam no envio