js calculate percentage between two numbers
JavaScript[Edit]
+
0
-
0
js calculate percentage between two numbers
1 2 3 4 5 6 7 8 9 10 11 12 13 14const x = 30; const y = 40; const result = (x / y) * 100; console.log(result + '%'); // Output: 75% (so x is 75% of y) // More examples: console.log((30 / 60) * 100 + '%'); // Output: 50% (30 is 50% of 60) console.log((10 / 50) * 100 + '%'); // Output: 20% (10 is 20% of 50) console.log((15 / 10) * 100 + '%'); // Output: 150% (15 is 150% of 10) console.log((60 / 30) * 100 + '%'); // Output: 200% (60 is 200% of 30)
[Edit]
+
0
-
0
js calculate percentage between two numbers
1 2 3 4 5 6 7 8 9 10function calculatePercentage(x,y){ return `${x / y * 100}%`; // so x is __% of y } // Usage example: console.log(calculatePercentage(30,60)); // 50% console.log(calculatePercentage(10,50)); // 20% console.log(calculatePercentage(15,10)); // 150% console.log(calculatePercentage(60,30)); // 200%