Advertisement
KingAesthetic

Example 1

May 12th, 2024
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.31 KB | Source Code | 0 0
  1. // function that prints a random number from 1 - 50:
  2. function getRndNumber(min, max) {
  3.     return Math.floor(Math.random() * (max - min + 1)+ min);
  4. }
  5. const randomNum = getRndNumber(1, 50);
  6. console.log(randomNum);
  7.  
  8. // function that drills a random material out from the ground:
  9. const material = ["Diamond", "Platinum", "Gold", "Emerald", "Ruby", "Silver"];
  10. function RndMaterial(arr) {
  11.     const RndMaterial = Math.floor(Math.random() * arr.length);
  12.     return arr[RndMaterial];
  13. }
  14. const randomMat = RndMaterial(material);
  15. console.log(randomMat)
  16.  
  17. // function that calculates the area of a rectangle:
  18. function Areaofrectangle(length, width) {
  19.     return (length * width);
  20. }
  21. const length = 50
  22. const width = 30
  23. const area = length * width
  24. console.log(area);
  25.  
  26. // function to apply a speed boost
  27. function applySpeedBoost(currentSpeed, boostMultiplier) {
  28.     return currentSpeed * boostMultiplier;
  29. }
  30. const playerSpeed = 20;
  31. const boostMultiplier = 50;
  32. const boostedSpeed = applySpeedBoost(playerSpeed, boostMultiplier);
  33. console.log(boostedSpeed)
  34.  
  35. // function to simulate health regeneration
  36. function regenerateHealth(currentHealth, regenerationRate, timeInSeconds) {
  37.     const regeneratedHealth = currentHealth + regenerationRate * timeInSeconds;
  38.     return Math.min(regeneratedHealth, 100); // Cap health at 100
  39. }
  40. const playerHealth = 80;
  41. const regenerationRate = 2;
  42. const timeElapsed = 5;
  43. const newHealth = regenerateHealth(playerHealth, regenerationRate, timeElapsed);
  44. console.log(`New health: ${newHealth}`);
  45.  
  46. // function to calculate experience points (XP)
  47. function calculateXP(level, baseXP, multiplier) {
  48.     return baseXP * Math.pow(level, multiplier);
  49. }
  50. const playerLevel = 5;
  51. const baseXP = 100;
  52. const xpMultiplier = 1.5;
  53. const earnedXP = calculateXP(playerLevel, baseXP, xpMultiplier);
  54. console.log(earnedXP);
  55.  
  56. // inventory system integration:
  57. const playerInventory = {
  58.     credits: 52,
  59.     coins: 7,
  60.     keys: 4,
  61.     weapons: ["sword", "bow & arrow"],
  62. };
  63.  
  64. // function that adds an item to the inventory of the player:
  65. function addtoInventory(item, quantity) {
  66.     if (playerInventory[item]) {
  67.         playerInventory[item] += quantity;
  68.     } else {
  69.         playerInventory[item] = quantity;
  70.     }
  71. }
  72. addtoInventory("credits", 75);
  73. addtoInventory("coins", 25);
  74. console.log(playerInventory);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement