# Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Returning every third odd number within a user specified range

• This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
• Author
Posts
• #43974
kgscott284
Participant

All I am trying to do is simply return every third odd number within a specified range.

This one is a bit weird for me, I tried a few different things but nothing even worth posting as my attempts yielded no results close to what i was looking for, I have a feeling it is out of the scope of my current capabilities, any help would be GREATLY appreciated. Thanks in advance.

#131085
pixelgrid
Participant

are the numbers consecutive? like 1-100 or 323-555?

#131086
kgscott284
Participant

Consecutive, Starting at 1….What i am going to do since it is for my personal use is make an html form with 2 text boxes for the range…the closest thing i have of the basic functionality is this, i just am not sure where to go from there…and i don;t think it is looping correctly i still have to check on that….like i said, i know crap about javascript…i can do every other with this method, not every third though…

perhaps using splice or push would be a better option?

var arr = [1,3,5,7,9,11,13,15,17,19,21,23,25];
for(var i = 1; i < arr.length; i+=2){
console.log(i);
}

#131088
pixelgrid
Participant

get the value of the end of the range the second input and change arr.length with that value, no need to store all the odd numbers to an array

#131089
kgscott284
Participant

I am going to expand on this so using an array will make it easier for me to work with when i get that far…

Here is my thought on using the splice idea, I think this one works just fine actually for my purpose:

var arr = [1,3,5,7,9,11,13,15,17,19,21,23,25];

for(var i = 0; i < arr.length; i++) {
arr.splice(i+1,2);
}

console.log(arr);

#131091
pixelgrid
Participant

you dont know how big the range will be so storing the array before that its wrong , have a look at this code
http://codepen.io/anon/pen/Gtflb also checks if the input is a number larger than zero

Viewing 6 posts - 1 through 6 (of 6 total)
• The forum ‘JavaScript’ is closed to new topics and replies.