OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Make sum by going through a finite sequence of numbers

  • Thread starter Thread starter Marcelo Barganha
  • Start date Start date
M

Marcelo Barganha

Guest
Using only javascript, is it possible to add numbers within a specific sequence of numbers?

For example:

I have the numbers 1 to 12 (months of the year)

If I add 1 (January) + 3 (months), the result would be 4 (April).

But if I am in month 11 (November) and add 3, the result would be 14 (non-existent month in the sequence).

So I'm trying a solution so that when the sum reaches 12 (December), it returns to number 1 (January). So the sum of 11+3 would be 2 (February).

The example using dates is fictitious. I can't use Date() as a reference because I'm looking for a solution for any random sequence of numbers in a row that I can iterate through.

I thought about the possibility of having an array, as shown in the example below:

Code:
let myArr = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec '];

myArr[10] //nov

Add 3 and return to position myArr[2] (feb)

But I'm not aware of any way to do this in an automated way with javascript.

<p>Using only javascript, is it possible to add numbers within a specific sequence of numbers?</p>
<p>For example:</p>
<p>I have the numbers 1 to 12 (months of the year)</p>
<p>If I add 1 (January) + 3 (months), the result would be 4 (April).</p>
<p>But if I am in month 11 (November) and add 3, the result would be 14 (non-existent month in the sequence).</p>
<p>So I'm trying a solution so that when the sum reaches 12 (December), it returns to number 1 (January). So the sum of 11+3 would be 2 (February).</p>
<p>The example using dates is fictitious. I can't use Date() as a reference because I'm looking for a solution for any random sequence of numbers in a row that I can iterate through.</p>
<p>I thought about the possibility of having an array, as shown in the example below:</p>
<pre><code>let myArr = ['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec '];

myArr[10] //nov
</code></pre>
<p>Add 3 and return to position myArr[2] (feb)</p>
<p>But I'm not aware of any way to do this in an automated way with javascript.</p>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top