2

I am trying to create a function that builds an array up to a number set by the function parameter, with an if condition on being included based on whether the remainder is zero. The last number in the array should be no higher than the parameter. Here's what I came up with so far --

function justThreesUpTo(num) {  
var array = [];
array.length = num; 
for (i = 1; i < array.length; i++) {
  if(i % 3 === 0){
    array.push(i);
  }
    else i;
  }
  array.splice(0, num);
  return array;
}

When I console log this, with justThreesUpTo(20), I get --

// [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42 ]

I see the issue being setting the limiter at array.length, which maxes out the number of items that can be in the array, but I can't figure out what else to call to make sure the last number in the array goes no higher than the "num" parameter specified by the function call. Any ideas?

1
  • what is the purpose of the function? what is the else part doing? Commented May 10, 2018 at 7:29

6 Answers 6

3

Setting an array's length to something before the array is populated isn't a great idea - better to just iterate over the num itself. For example

for (var i = 1; i < num; i++) {
  // push to array if i % 3 === 0

Your else i won't do anything - you can just leave it off completely.

You could make your code a whole lot shorter and cleaner if you wanted:

function justThreesUpTo(num) {
  const length = Math.floor(num / 3);
  return Array.from({ length }, (_, i) => (i + 1) * 3);
}
console.log(justThreesUpTo(20));

Sign up to request clarification or add additional context in comments.

1 Comment

Much better. I realized an hour soon thereafter this was not a great solution, but also want the function take inputs for both the divider (in 2's, 3's, etc) and the limiter. So in that case, (num, div) and change "/ 3" in the length constant to " / div" - seems to work fine.
3

Modifying an array while looping over it (or its indices, which is what you’re doing with i < array.length) is a recipe for confusion. Start with an empty array and compare with num instead:

function justThreesUpTo(num) {
    var array = [];

    for (var i = 1; i < num; i++) {
        if (i % 3 === 0) {
            array.push(i);
        }
    }

    return array;
}

Now you can optimize the check out of that entirely by moving up the appropriate amount each time.

function justThreesUpTo(num) {
    var array = [];

    for (var i = 3; i < num; i += 3) {
        array.push(i);
    }

    return array;
}

(In your original code, the entire first num holes created by array.length = num; are unused and get spliced off, and else i does nothing.)

1 Comment

Much better too. Very helpful way to conceptualize it. Could also turn the += amount into a parameter, and have it return steps specified by input up to the limiter. repl.it/@davedub/Array-ranges-filled-with-numbers // console.log(moreJustDivsUpTo(60, 5));
1

You can try with a simple while loop

function justThreesUpTo(num) {  
   var array = [];
   var i = 0;

   while (i < num) {
     if(i % 3 === 0){
       array.push(i);
     }
     i++;
   }
   return array;
}

console.log(justThreesUpTo(20));

Comments

1

You can use map method and spread syntax in order to write a clean solution.

function justThreesUpTo(num) {  
   return [ ...Array(Math.floor(num/3)).keys() ].map((_,i)=> (i+1) * 3);
}
console.log(justThreesUpTo(20));

Comments

0

Hmm. Looks like it was a pretty simple solution. Changed the limiter from "array.length" to "num", and it worked fine.

function justThreesUpTo(num) {
var array = [];
array.length = num; 
for (i = 1; i < num; i++) {
  if(i % 3 === 0){
      array.push(i);
  }
  else i;
  }
  array.splice(0, num);
  return array;
}

Never mind!

Comments

0

Use while with i+=3; inside the while loop:

function justThreesUpTo(num) {  
  var array = [];
  var i = 0;
  while(i<num){
    array.push(i);
    i+=3;
  }
  return array;
}

console.log(justThreesUpTo(20));

2 Comments

@Ry- very true. Edited the answer. Thanks for the suggestion
Yet another way to do it better than my initial try. Works just as easily with two parameters as well, which is where I was trying to wind up. See repl.it in prior comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.