0

Hello friends can you fix my code like when like now the enterend input is 12 divide2 ,divide3 and divide4 to get increased from 0 to 1. or when the the input is 3,2,9 divide3 to get to value of 2 and divide2 to get 1...

Here is my code

function Divider(args) {

    let n = Number(args[0]);

    let divide2 = 0.0;
    let divide3 = 0.0;
    let divide4 = 0.0;

    for (let i = 1; i < n; i++) {
        let currentNum = Number(args[i])

        if (currentNum % 2 == 0) {
            divide2++;

        }
        if (currentNum % 3 == 0) {
            divide3++;
        }
        if (currentNum % 4 == 0) {
            divide4++;
        }


    }

    console.log(divide2);
    console.log(divide3);
    console.log(divide4);


}

Divider(['1', '12']);
  

The idea is how many numbers are divided to 2 ,3,4 without reminder to be recorded in the variables.I need it done this way.

2
  • 1
    What is the point of let n = Number(args[0]);? Your loop never runs because of it. Commented Jul 25, 2020 at 9:07
  • for (let i = 1; i < n; i++) { should be for (let i = 1; i <= n; i++) { Commented Jul 25, 2020 at 9:08

3 Answers 3

1

A couple of things,

  1. Javascript provides arguments as a way to use the parameters passed into a function to be used as within it's scope as an array

  2. While passing the arguments while calling the function, you don't need to wrap the arguments in an array. Javascript will automatically do that for you and make it available as arguments.

  3. In the for loop, you need to iterate till i <= n and not for i < n. If there is only one number passed it at index 1, the loop won't execute.

  4. You can use integers for divide2, divide3 and divide4 instead of floating point numbers.

Here is the solution after fixing your code.

 function Divider(args) {

    let n = Number(arguments[0]);

    let divide2 = 0.0;
    let divide3 = 0.0;
    let divide4 = 0.0;

    for (let i = 1; i <= n; i++) {
        let currentNum = Number(arguments[i])

        if (currentNum % 2 == 0) {
            divide2++;
        }
        if (currentNum % 3 == 0) {
            divide3++;
        }
        if (currentNum % 4 == 0) {
            divide4++;
        }
    }

    console.log(divide2);
    console.log(divide3);
    console.log(divide4);
}

Divider('3','3','2','9') // divide2 = 1.0, divide3 = 2.0, divide4 = 0.0 ;
Sign up to request clarification or add additional context in comments.

Comments

0

Not sure what exactly you want, but this is what I came up with,

let divide2 = 0.0;
let divide3 = 0.0;
let divide4 = 0.0;

function Divider(args) {

     for (let i = 0; i < args.length; i++) {
        let currentNum = Number(args[i])
        

        if (currentNum % 2 === 0) {
            divide2++;
        }
        
        if (currentNum % 3 === 0) {
            divide3++;
            
        }
        
        if (currentNum % 4 === 0) {
            divide4++;        
       }
    }

}

 Divider(['1', '6', '9', '12', '24']);

 console.log('by2', divide2);
 console.log('by3', divide3);
 console.log('by4', divide4);

1 Comment

Thanks so much sir, i read that array.lenght is not recommended because of bad input and wanted to try by getting the first index args[0] but like this it works too.Thanks again!
0

This is what you want:

function divider(n) {
    let divide2 = 0;
    let divide3 = 0;
    let divide4 = 0;
    for (let i = 0; i < n.length; i++) {
        let currentNum = +n[i];
        if (currentNum % 2 == 0) {
            divide2++;
        }
        if (currentNum % 3 == 0) {
            divide3++;
        }
        if (currentNum % 4 == 0) {
            divide4++;
        }
    }
    return {
        divide2,
        divide3,
        divide4
    };
}

console.log(divider(['3', '2', '9']));

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.