-1

why when i paste my code in playcode.io it works like this

function Nums(args) {
  let n = Number(args[0]);

  let p1 = 0;
  let p2 = 0;
  let p3 = 0;
  let p4 = 0;
  let p5 = 0;

  for (let i = 1; i < n; i++) {
    let currentNum = Number(args[i])
    if (currentNum < 200) {
      p1++;
    } else if (currentNum < 400) {
      p2++;
    } else if (currentNum < 600) {
      p3++;
    } else if (currentNum < 800) {
      p4++;
    } else if (currentNum <= 1000) {
      p5++;
    }
  }
  console.log(p1);
  console.log(p2);
  console.log(p3);
  console.log(p4);
  console.log(p5);
}

Nums(["4", "1", "3", "999"]);

I want to sort some numbers but aren't arrays starting from 0,why when i type 4 as first number calcs it correct? if i type 2 it places my 1 and 3 if fist variable and the last varibale is empty

vahdet
  • 6,357
  • 9
  • 51
  • 106
Kirito
  • 13
  • 3
  • 2
    Its not clear which array you want to sort, and why your code is relevant for the question. By the way your code ignore the first parameter, you have to loop on *for(i=0; i – iguypouf Jul 22 '20 at 16:43
  • Can you describe what you expect the output to be? you mention `aren't arrays starting from 0` but your for-loop starts at 1 instead of 0. also can you clarify what you mean by `if i type 2 it places my 1 and 3 if fist variable and the last varibale is empty`? i can't understand it – Klaycon Jul 22 '20 at 16:45
  • Want to sort the Nums array sir in the variables p1 to p5 depending of the size of the number , sorry for not typing it. – Kirito Jul 22 '20 at 16:51

2 Answers2

0

You could take an array for counting the number in a certain slot.

function nums(values) {
    let counts = [0, 0, 0, 0, 0];

    values.forEach(v => counts[Math.min(Math.floor(v / 200), 4)]++);
    return counts;
}

console.log(...nums([4, 1, 3, 999, 1000]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • This is only relevant for this array (with proportional steps), I recommend to look at array.reduce : https://stackoverflow.com/questions/17313268/idiomatically-find-the-number-of-occurrences-a-given-value-has-in-an-array – iguypouf Jul 22 '20 at 16:54
0

Hello friends i got it working the way i wanna. I don't explain clear enough in the first place sorry.Here is the correction :

function Histogram(args) {

let n = Number(arguments[0]);

    let v1 = 0.0;
    let v2 = 0.0;
    let v3 = 0.0;
    let v4 = 0.0;
    let v5 = 0.0;

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

        if (currentNum < 200) {
            v1++;
        }
        else if (currentNum < 400) {
            v2++;
        }
        else if (currentNum < 600) {
            v3++;
        }
        else if (currentNum < 800) {
            v4++;
        }
        else if (currentNum < 1000) {

            v5++;
        }
    }

    
    console.log(v1);
    console.log(v2);
    console.log(v3);
    console.log(v4);
    console.log(v5);

   

}

Histogram('3','1', '2', '999');

Now i'm wondering why when i insert more code it breaks again...

 let p1Percantage = 0;
    let p2Percantage = 0;
    let p3Percantage = 0;
    let p4Percantage = 0;
    let p5Percantage = 0;


    p1Percantage = (v1 / n * 100);
    p1Percantage = (v2 / n * 100);
    p1Percantage = (v3 / n * 100);
    p1Percantage = (v4 / n * 100);
    p1Percantage = (v5 / n * 100);

    console.log(p1Percantage.toFixed(2) + "%");
    console.log(p2Percantage.toFixed(2) + "%");
    console.log(p3Percantage.toFixed(2) + "%");
    console.log(p4Percantage.toFixed(2) + "%");
    console.log(p5Percantage.toFixed(2) + "%");

The variable that is suppost to show the last it shows as first... Expected output

66.67% 0.00% 0.00% 0.00% 33.33%

Main:

33.33% 0.00% 0.00% 0.00% 00.00%

The whole program supposed to sort the histogram array in the correct variables by their values and show the percentage of each variables p1Percantage = (v1 / n * 100); .

Kirito
  • 13
  • 3
  • Fixed the second problem too it was a syntax problm,i didn't change the name of the variables: p1Percantage = (v1 /n * 100); p2Percantage = (v2 / n * 100); p3Percantage = (v3 / n * 100); p4Percantage = (v4 / n * 100); p5Percantage = (v5 / n* 100); – Kirito Jul 26 '20 at 08:45