0

I am trying to output an array that will give me the values of each count from the arr if the conditions hold true.

Example:

//tasksTypes([1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8], 1)  [2, 8, 2]

My code:

function tasksTypes(deadlines, day) {
    var list= [];
    var today = 0;
    var upcoming = 0;
    var later = 0;

    for(var i=0; i<deadlines.length; i++){
        if(deadlines[i] <= day){
            today += today
            list.push(today)
            if(deadlines[i] <= day + 7){
                upcoming += upcoming
                list.push(upcoming)
                if(deadlines[i] > day + 7){
                    later += later
                    list.push(later)
                }
            }
        }
    }
    return list
}   
4
  • what is your deadlines array? Commented Nov 17, 2016 at 22:05
  • What are your conditions? The code is not really helpful in this respect because the middle if condition is always true when executed, and the inner if condition is never true when executed. Commented Nov 17, 2016 at 22:08
  • Please learn to end statements with ;. You can get unexpected errors if you don't. Commented Nov 17, 2016 at 22:14
  • Please expand your description of what the code is supposed to do. Commented Nov 17, 2016 at 22:56

2 Answers 2

1

Reduce the deadlines array to the counts array:

function tasksTypes(deadlines, day) {
  return deadlines.reduce(function(counts, deadline) {
    var index = deadline <= day ? 0 : (deadline <= day + 7 ? 1 : 2);
    
    counts[index]++;
    
    return counts;
  }, [0, 0, 0]);
}

var result = tasksTypes([1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8], 1);

console.log(result);

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

Comments

0

I think this is what you're looking for. You just want to count the number of instances each value in deadlines meets a certain criteria based on day.

function tasksTypes(deadlines, day) {
  var today = 0;
  var upcoming = 0;
  var later = 0;
  
  for(var i = 0; i < deadlines.length; ++i) {
    if(deadlines[i] <= day)
        today++;
    else if(deadlines[i] <= day + 7)
        upcoming++;
    else
        later++;
   }
  
  return [today, upcoming, later];
}

console.log(tasksTypes([1, 2, 4, 2, 10, 3, 1, 4, 5, 4, 9, 8], 1));

Comments

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.