DEV Community

Cover image for #7DaysJS: myElement + n
Lautaro Lobo
Lautaro Lobo

Posted on • Originally published at lautarolobo.xyz on

2 2

#7DaysJS: myElement + n

Welcome to day 6 of 7 Days of JavaScript!

Let’s see, I got a nice one for today.

Given an array and a number n, return the array with each element summed to n.

Here’s an example:

sumToArray(5,[1,2,3,4])
...
[6,7,8,9]
Enter fullscreen mode Exit fullscreen mode

If you want, you can also return the output as a string - If that makes it easier for you, it’s OK.

You can check out the solution algorithm here.

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay