본문 바로가기
알고리즘/Codewars

[Codewars] Invert values (8 kyu) / JavaScript

by fluss 2023. 1. 16.

https://www.codewars.com/kata/5899dc03bc95b1bf1b0000ad

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Given a set of numbers, return the additive inverse of each. Each positive becomes negatives, and the negatives become positives.

invert([1,2,3,4,5]) == [-1,-2,-3,-4,-5]
invert([1,-2,3,-4,5]) == [-1,2,-3,4,-5]
invert([]) == []

You can assume that all values are integers. Do not mutate the input array/list.

 

코드

function invert(array) {
   return array.map(el => el * -1);
}

댓글