-3

I've looked at similar questions but not seeing something that directly answers my question. I'm looking for the most efficient way to print odd numbers 1-100, without using any extra conditional statements (using JavaScript).

jwally
  • 1
  • 1
  • 1
  • 2
    Start with 1 and just keep adding 2 – Patrick Evans Nov 03 '18 at 23:12
  • 20 years of development, 7 standards and still no `range()` in javascript. What a shame! – georg Nov 03 '18 at 23:23
  • While the answers give you an...."answer" to your question, they fail to say why it answers your question. You need to supply more information such as what you have tried, the questions you have looked at. As it stands, you're just looking for someone to give you the code to do something, which isn't productive. – Cjmarkham Nov 03 '18 at 23:27

4 Answers4

2

For the sake of overcomplication:

The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object.
...
Array.from() lets you create Arrays from:

  • array-like objects (objects with a length property and indexed elements) or
  • iterable objects (objects where you can get its elements, such as Map and Set).

- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

You can call Array.from against an object with a length property and no other properties to iterate n times where n is the value of the length property. The second argument of Array.from is a callback that is called each iteration with the element (undefined) and the index. The return value of the callback is the value corresponding index on the newly created array.

We can multiple the index by 2 then subtract one to get only odd values, which means we can start with a length of 50 instead of iterating 100 times then iterating the newly created array another 100 times to return another array with only 50 elements as suggested in other answers.

console.log(Array.from({ length: 50 }, (e, i) => (i * 2) + 1).join(' '))
1

This is an alternative using the function filter.

console.log(Array.from({length: 100}, (_, i) => i).filter(n => n % 2));
Ele
  • 33,468
  • 7
  • 37
  • 75
0

Just do this:

for (var i = 1; i < 100; i += 2) {
    console.log(i);
}

You can, of course, replace console.log(i) with whatever you want "print out" to mean (it's a bit unclear). You can write them to the HTML page if you want:

for (var i = 1; i < 100; i += 2) {
    document.write(i + "<br>");
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can do it by:

for (let i = 1; i < 100; i+=2) {
 console.log(i);
}

It's a for loop.

You could do: console.log(1); console.log(3); .... console.log(99);

That's why we use for loop. For loop needs to know what is the first index (1), last index (99. that's why I did: '< 100), and what are the steps (2). So it starts with i=1, execute the inner code in the for loop. When finish, add 2 (steps) to i. so i is 3. Execute the code with i=3. When finish, add 2 (steps) to i. so i is 5. Execute the code with i=5. etc.

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138
  • You could at least explain what this code is doing. – Cjmarkham Nov 03 '18 at 23:23
  • 1
    @CarlMarkham if someone needs an explanation as to what a for loop is and what it's doing maybe this is not a good place to be asking questions in – apokryfos Nov 03 '18 at 23:35
  • @apokryfos SO is a place to ask any question. What may seem simple to you may not be as simple to others. You should always take that in to account when answering and provide a description of why your code works or why it is best. Simply providing code does nothing to teach the OP. – Cjmarkham Nov 04 '18 at 00:30
  • @CarlMarkham it's a for loop. It's like on page 3 of any basic tutorial on any language – apokryfos Nov 04 '18 at 00:43