skip to Main Content

so ive been stuck on this assignment for the past 2 days and i dont know what im doing wrongquestion
so i did this which in my head should pass the question but according to the custom website we have to do it on the answer is wrong and i dont know what else to do so any help would be appreciated

//writes the parameters 1-10                                        
var result = "";                                        
                                        
function count(num1, num2)                                      
{                                       
  for (var i = num1 + 1; i <= num2; i++)                                        
  {                                     
    result += i;                                        
                                        
    if (i < num2)                                       
    {                                       
      result += ",";                                        
    }                                       
  }                                     
  return result;                                        
                                        
}                                       

i attempted this with the help of my friends and with chatgpt to assist me in what i could be doing wrong however it doesnt really give the answer i was looking for

3

Answers


  1. From what I understood, the only thing that seems to be missing is the starting number:

    function count(num1, num2)                                      
    {        
      let result = ""; // Always initialise in function to ensure it always sends the current result only.                                       
      let i = num1;
      for (i = num1; i <= num2; i++) // Set i = num1 instead of num+1
      {        
        result += i;
        if (i < num2) { result += ","; }                      
      }
      return result;                                     
    }
    
    Login or Signup to reply.
  2. You can greatly simplify the code by incrementing the number in a loop and pushing the resulting number into an array. Once all of the numbers are in the array, you can use the Array.prototype.toString method to convert the array into a comma-separated string:

    Source

    const count = (start, end) => {
      if (start > end) return "";
      let out = [];
      let idx = start - 1;
      while (++idx <= end) out.push(idx);
      return out.toString();
    };
    
    exports.count = count;
    

    Tests

    const { strict: assert } = require('node:assert');
    const { count } = require('./index');
    
    assert.equal(count(0, 10), "0,1,2,3,4,5,6,7,8,9,10");
    assert.equal(count(2, 5), "2,3,4,5");
    assert.equal(count(5, 2), "");
    
    Login or Signup to reply.
  3. You could iterate with start and iterate until end is found.

    function count(start, end) {
        let result = start.toString();
        while (start++ < end) result += ',' + start;
        return result;
    }
    
    console.log(count(3, 3));
    console.log(count(3, 7));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search