New to Js I need Help.



  • I have this issue with my code. When I use return it displays 1 value, but when I change it to console. It displays all values.
    Here are the codes.

    function divisors(integer) {
      var i;
      let array = [];
      for( i = 2; i < integer; i++){
        if(integer % i === 0) 
          return i
        //console.log(i) gives all values
          ;
      }
      return integer + " is prime";
    };
    console.log(divisors(15))
    

    Any help?



  • const divisors = integer => {
    	const array = [];
    	const numbers = [];
    
    	for (let i = 2; i < integer; i++) {
    		if (integer % i === 0) numbers.push(i);
    	}
    
    	return numbers.length > 0 ? `${numbers} \n ${integer} is prime.` : `${integer} is prime.`;
    };
    
    console.log(divisors(10));
    

Log in to reply
 

Suggested Topics

| |