Jump to content
MakeWebGames

Recommended Posts

Posted

Coded from pure html and jquery i only made it on jsfiddle thinking of implementing it into base of staff crimes file for easier viewing.

For users who do not understand the formula for crime chance success rates.

Enter users level and will and click calculate 

image.png

Posted (edited)

I really like the ability to visualise how the crime formulae may result; so I took your idea and added the ability to select from an existing crime, or assume the default formula ((WILL*0.8)/2.5)+(LEVEL/4) and calculate as desired.
I also stripped the jQuery dependence; pure vanilla JS, woo!
Fair note to our more sensitive-eyed programmers; there's no dark mode by default.

Note: Written in a PHP8.4 environment. Older versions may need to change the match() call to a switch() equivalent

 

crime-formula.php

<?php
declare(strict_types=1);
$_GET['action'] ??= null;
$_GET['id']     = array_key_exists('id', $_GET) && is_numeric($_GET['id']) && (int)$_GET['id'] > 0 ? (int)$_GET['id'] : null;

class CrimeFormula
{
    private static ?self $inst = null;
    private ?database    $db;
    private ?array       $settings;

    /**
     * @param database $db
     * @param array    $settings
     */
    public function __construct(database $db, array $settings)
    {
        $this->db       = $db;
        $this->settings = $settings;
        $this->run();
    }

    /**
     * @return void
     */
    private function run(): void
    {
        $response = match ($_GET['action']) {
            'get-crime-by-id' => $this->getCrimeById($_GET['id']),
            default           => [
                'type'    => 'error',
                'message' => 'No action given',
            ],
        };
        if ($this->isAjax()) {
            header('Content-type: application/json');
            echo json_encode($response);
            exit;
        }
        if (array_key_exists('location', $response)) {
            header('Location: ' . $response['location']);
            exit;
        }
        echo $this->template('crime-select', [
            '%id%'               => $_GET['id'],
            '%menu.opts:crimes%' => $this->renderMenuOptsCrimes($_GET['id']),
        ]);
    }

    /**
     * @param int|null $id
     *
     * @return array|null
     */
    private function getCrimeById(?int $id): ?array
    {
        if (empty($id)) {
            return null;
        }
        $get_crime = $this->db->query(
            'SELECT * FROM crimes WHERE crimeID = ' . $id . ' LIMIT 1',
        );
        $row       = $this->db->fetch_row($get_crime);
        $this->db->free_result($get_crime);
        return $row ?? null;
    }

    /**
     * @return bool
     */
    private function isAjax(): bool
    {
        return array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
    }

    /**
     * @param string $fileName
     * @param array  $replacements
     *
     * @return string|null
     */
    private function template(string $fileName, array $replacements = []): ?string
    {
        $path = str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/' . $fileName . '.html');
        if (!file_exists($path)) {
            return null;
        }
        $content = file_get_contents($path);
        return strtr($content, $replacements);
    }

    /**
     * @param int|null $selected
     *
     * @return string
     */
    private function renderMenuOptsCrimes(?int $selected = null): string
    {
        $ret  = '';
        $rows = $this->db->query(
            'SELECT crimeID, crimeNAME, crimeBRAVE FROM crimes ORDER BY crimeBRAVE',
        );
        while ($row = $this->db->fetch_row($rows)) {
            $ret .= sprintf(
                '<option value="%u" %s>%s [%s brave]</option>%s',
                $row['crimeID'],
                (int)$row['crimeID'] === $selected ? 'selected' : '',
                stripslashes(htmlspecialchars($row['crimeNAME'])),
                number_format((int)$row['crimeBRAVE']),
                PHP_EOL,
            );
        }
        $this->db->free_result($rows);
        return $ret;
    }

    /**
     * @param database $db
     * @param array    $settings
     *
     * @return self|null
     */
    public static function getInstance(database $db, array $settings): ?self
    {
        if (self::$inst === null) {
            self::$inst = new self($db, $settings);
        }
        return self::$inst;
    }

}

global $db, $set;
require_once __DIR__ . '/globals_nonauth.php';
$module = CrimeFormula::getInstance($db, $set);


crime-select.html
 

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Crime Formula Tests</title>
</head>
<body>
<div class="container" style="margin: 0 auto;">
    <form id="crime-selection-form" method="get">
        <div class="row py-2">
            <div class="col">
                <div class="form-group">
                    <label for="crime">Select Crime</label>
                    <select name="crime" id="crime" class="form-control">
                        <option value="0" selected>--- None ---</option>
                        %menu.opts:crimes%
                    </select>
                </div>
            </div>
        </div>
        <div class="row py-2">
            <div class="col-lg-6 col-md">
                <div class="form-group">
                    <label for="level">Level</label>
                    <input type="number" name="level" id="level" class="form-control" value="1" step="1">
                </div>
            </div>
            <div class="col-lg-6 col-md">
                <div class="form-group">
                    <label for="will">Will</label>
                    <input type="number" name="will" id="will" class="form-control" value="100" step="1">
                </div>
            </div>
        </div>
    </form>
</div>
<div class="container">
    <span class="d-block m-1" id="crime-formula-raw"></span>
    <span class="d-block m-1" id="crime-formula-formatted"></span>
    <span class="d-block m-1" id="crime-response"></span>
</div>
<script>
  const apiCall = (path) => {
    return fetch(path, {
      headers: {
        'credentials': 'same-origin',
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json'
      },
      signal: AbortSignal.timeout(5000)
    });
  };
  const getCrime = (id, callback) => {
    apiCall(`crime-formula.php?action=get-crime-by-id&id=${id}`)
      .then(response => response.json())
      .then(data => callback(data));
  };
  const responseElem = document.getElementById("crime-response");
  const formulaRawElem = document.getElementById("crime-formula-raw");
  const formulaFormattedElem = document.getElementById("crime-formula-formatted");
  const updateCrimeInfo = (formula, will, level) => {
    let formulaFormatted = formula.replace('WILL', will).replace('LEVEL', level);
    formulaRawElem.innerText = formula;
    formulaFormattedElem.innerText = formulaFormatted;
    /* Dirty. Don't do this if you can avoid it. And certainly *never* trust user input with it. */
    let amnt = eval(formulaFormatted);
    responseElem.innerText = amnt.toLocaleString(undefined, {maximumFractionDigits: 3}) + "%";
  };
  const getCrimeFullEvent = (e) => {
    e.stopPropagation();
    e.preventDefault();
    if ([undefined, null].includes(responseElem) || [undefined, null].includes(formulaRawElem) || [undefined, null].includes(formulaFormattedElem)) {
      console.error("Form element missing: crime-response/crime-formula-raw/crime-formula-formatted");
      return;
    }
    let will = document.getElementById("will").value;
    let level = document.getElementById("level").value;
    let id = document.getElementById("crime").value;
    if ([undefined, null].includes(will)) {
      will = 100;
    }
    if ([undefined, null].includes(level)) {
      level = 1;
    }
    if ([undefined, null].includes(id)) {
      id = 0;
    }
    let formula = '((WILL*0.8)/2.5)+(LEVEL/4)';
    if (id > 0) {
      getCrime(id, (data) => {
        if ([undefined, null].includes(data)) {
          console.error("Blank crime data response");
          return false;
        }
        if (data.hasOwnProperty("type") && data.type !== "success") {
          console.error(data);
          return false;
        }
        updateCrimeInfo(data.crimePERCFORM, will, level);
        return true;
      });
    } else {
      updateCrimeInfo(formula, will, level);
    }
  };

  window.addEventListener("DOMContentLoaded", () => {
    const formElem = document.getElementById("crime-selection-form");
    if ([undefined, null].includes(formElem)) {
      console.error("Form element missing: crime-selection-form");
      return;
    }
    formElem.addEventListener("keyup", (e) => getCrimeFullEvent(e));
    formElem.addEventListener("mouseup", (e) => getCrimeFullEvent(e));
  });
</script>

</body>
</html>

 

Edited by Magictallguy
"my" to "our". I don't own you.
  • Like 1
Posted
1 hour ago, Magictallguy said:

I really like the ability to visualise how the crime formulae may result; so I took your idea and added the ability to select from an existing crime, or assume the default formula ((WILL*0.8)/2.5)+(LEVEL/4) and calculate as desired.
I also stripped the jQuery dependence; pure vanilla JS, woo!
Fair note to our more sensitive-eyed programmers; there's no dark mode by default.

Note: Written in a PHP8.4 environment. Older versions may need to change the match() call to a switch() equivalent

 

crime-formula.php

<?php declare(strict_types=1); $_GET['action'] ??= null; $_GET['id'] = array_key_exists('id', $_GET) && is_numeric($_GET['id']) && (int)$_GET['id'] > 0 ? (int)$_GET['id'] : null; class CrimeFormula { private static ?self $inst = null; private ?database $db; private ?array $settings; /** * @param database $db * @param array $settings */ public function __construct(database $db, array $settings) { $this->db = $db; $this->settings = $settings; $this->run(); } /** * @return void */ private function run(): void { $response = match ($_GET['action']) { 'get-crime-by-id' => $this->getCrimeById($_GET['id']), default => [ 'type' => 'error', 'message' => 'No action given', ], }; if ($this->isAjax()) { header('Content-type: application/json'); echo json_encode($response); exit; } if (array_key_exists('location', $response)) { header('Location: ' . $response['location']); exit; } echo $this->template('crime-select', [ '%id%' => $_GET['id'], '%menu.opts:crimes%' => $this->renderMenuOptsCrimes($_GET['id']), ]); } /** * @param int|null $id * * @return array|null */ private function getCrimeById(?int $id): ?array { if (empty($id)) { return null; } $get_crime = $this->db->query( 'SELECT * FROM crimes WHERE crimeID = ' . $id . ' LIMIT 1', ); $row = $this->db->fetch_row($get_crime); $this->db->free_result($get_crime); return $row ?? null; } /** * @return bool */ private function isAjax(): bool { return array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'; } /** * @param string $fileName * @param array $replacements * * @return string|null */ private function template(string $fileName, array $replacements = []): ?string { $path = str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/' . $fileName . '.html'); if (!file_exists($path)) { return null; } $content = file_get_contents($path); return strtr($content, $replacements); } /** * @param int|null $selected * * @return string */ private function renderMenuOptsCrimes(?int $selected = null): string { $ret = ''; $rows = $this->db->query( 'SELECT crimeID, crimeNAME, crimeBRAVE FROM crimes ORDER BY crimeBRAVE', ); while ($row = $this->db->fetch_row($rows)) { $ret .= sprintf( '<option value="%u" %s>%s [%s brave]</option>%s', $row['crimeID'], (int)$row['crimeID'] === $selected ? 'selected' : '', stripslashes(htmlspecialchars($row['crimeNAME'])), number_format((int)$row['crimeBRAVE']), PHP_EOL, ); } $this->db->free_result($rows); return $ret; } /** * @param database $db * @param array $settings * * @return self|null */ public static function getInstance(database $db, array $settings): ?self { if (self::$inst === null) { self::$inst = new self($db, $settings); } return self::$inst; } } global $db, $set; require_once __DIR__ . '/globals_nonauth.php'; $module = CrimeFormula::getInstance($db, $set);

<?php
declare(strict_types=1);
$_GET['action'] ??= null;
$_GET['id']     = array_key_exists('id', $_GET) && is_numeric($_GET['id']) && (int)$_GET['id'] > 0 ? (int)$_GET['id'] : null;

class CrimeFormula
{
    private static ?self $inst = null;
    private ?database    $db;
    private ?array       $settings;

    /**
     * @param database $db
     * @param array    $settings
     */
    public function __construct(database $db, array $settings)
    {
        $this->db       = $db;
        $this->settings = $settings;
        $this->run();
    }

    /**
     * @return void
     */
    private function run(): void
    {
        $response = match ($_GET['action']) {
            'get-crime-by-id' => $this->getCrimeById($_GET['id']),
            default           => [
                'type'    => 'error',
                'message' => 'No action given',
            ],
        };
        if ($this->isAjax()) {
            header('Content-type: application/json');
            echo json_encode($response);
            exit;
        }
        if (array_key_exists('location', $response)) {
            header('Location: ' . $response['location']);
            exit;
        }
        echo $this->template('crime-select', [
            '%id%'               => $_GET['id'],
            '%menu.opts:crimes%' => $this->renderMenuOptsCrimes($_GET['id']),
        ]);
    }

    /**
     * @param int|null $id
     *
     * @return array|null
     */
    private function getCrimeById(?int $id): ?array
    {
        if (empty($id)) {
            return null;
        }
        $get_crime = $this->db->query(
            'SELECT * FROM crimes WHERE crimeID = ' . $id . ' LIMIT 1',
        );
        $row       = $this->db->fetch_row($get_crime);
        $this->db->free_result($get_crime);
        return $row ?? null;
    }

    /**
     * @return bool
     */
    private function isAjax(): bool
    {
        return array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
    }

    /**
     * @param string $fileName
     * @param array  $replacements
     *
     * @return string|null
     */
    private function template(string $fileName, array $replacements = []): ?string
    {
        $path = str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/' . $fileName . '.html');
        if (!file_exists($path)) {
            return null;
        }
        $content = file_get_contents($path);
        return strtr($content, $replacements);
    }

    /**
     * @param int|null $selected
     *
     * @return string
     */
    private function renderMenuOptsCrimes(?int $selected = null): string
    {
        $ret  = '';
        $rows = $this->db->query(
            'SELECT crimeID, crimeNAME, crimeBRAVE FROM crimes ORDER BY crimeBRAVE',
        );
        while ($row = $this->db->fetch_row($rows)) {
            $ret .= sprintf(
                '<option value="%u" %s>%s [%s brave]</option>%s',
                $row['crimeID'],
                (int)$row['crimeID'] === $selected ? 'selected' : '',
                stripslashes(htmlspecialchars($row['crimeNAME'])),
                number_format((int)$row['crimeBRAVE']),
                PHP_EOL,
            );
        }
        $this->db->free_result($rows);
        return $ret;
    }

    /**
     * @param database $db
     * @param array    $settings
     *
     * @return self|null
     */
    public static function getInstance(database $db, array $settings): ?self
    {
        if (self::$inst === null) {
            self::$inst = new self($db, $settings);
        }
        return self::$inst;
    }

}

global $db, $set;
require_once __DIR__ . '/globals_nonauth.php';
$module = CrimeFormula::getInstance($db, $set);


crime-select.html
 

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Crime Formula Tests</title> </head> <body> <div class="container" style="margin: 0 auto;"> <form id="crime-selection-form" method="get"> <div class="row py-2"> <div class="col"> <div class="form-group"> <label for="crime">Select Crime</label> <select name="crime" id="crime" class="form-control"> <option value="0" selected>--- None ---</option> %menu.opts:crimes% </select> </div> </div> </div> <div class="row py-2"> <div class="col-lg-6 col-md"> <div class="form-group"> <label for="level">Level</label> <input type="number" name="level" id="level" class="form-control" value="1" step="1"> </div> </div> <div class="col-lg-6 col-md"> <div class="form-group"> <label for="will">Will</label> <input type="number" name="will" id="will" class="form-control" value="100" step="1"> </div> </div> </div> </form> </div> <div class="container"> <span class="d-block m-1" id="crime-formula-raw"></span> <span class="d-block m-1" id="crime-formula-formatted"></span> <span class="d-block m-1" id="crime-response"></span> </div> <script> const apiCall = (path) => { return fetch(path, { headers: { 'credentials': 'same-origin', 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json' }, signal: AbortSignal.timeout(5000) }); }; const getCrime = (id, callback) => { apiCall(`crime-formula.php?action=get-crime-by-id&id=${id}`) .then(response => response.json()) .then(data => callback(data)); }; const responseElem = document.getElementById("crime-response"); const formulaRawElem = document.getElementById("crime-formula-raw"); const formulaFormattedElem = document.getElementById("crime-formula-formatted"); const updateCrimeInfo = (formula, will, level) => { let formulaFormatted = formula.replace('WILL', will).replace('LEVEL', level); formulaRawElem.innerText = formula; formulaFormattedElem.innerText = formulaFormatted; /* Dirty. Don't do this if you can avoid it. And certainly *never* trust user input with it. */ let amnt = eval(formulaFormatted); responseElem.innerText = amnt.toLocaleString(undefined, {maximumFractionDigits: 3}) + "%"; }; const getCrimeFullEvent = (e) => { e.stopPropagation(); e.preventDefault(); if ([undefined, null].includes(responseElem) || [undefined, null].includes(formulaRawElem) || [undefined, null].includes(formulaFormattedElem)) { console.error("Form element missing: crime-response/crime-formula-raw/crime-formula-formatted"); return; } let will = document.getElementById("will").value; let level = document.getElementById("level").value; let id = document.getElementById("crime").value; if ([undefined, null].includes(will)) { will = 100; } if ([undefined, null].includes(level)) { level = 1; } if ([undefined, null].includes(id)) { id = 0; } let formula = '((WILL*0.8)/2.5)+(LEVEL/4)'; if (id > 0) { getCrime(id, (data) => { if ([undefined, null].includes(data)) { console.error("Blank crime data response"); return false; } if (data.hasOwnProperty("type") && data.type !== "success") { console.error(data); return false; } updateCrimeInfo(data.crimePERCFORM, will, level); return true; }); } else { updateCrimeInfo(formula, will, level); } }; window.addEventListener("DOMContentLoaded", () => { const formElem = document.getElementById("crime-selection-form"); if ([undefined, null].includes(formElem)) { console.error("Form element missing: crime-selection-form"); return; } formElem.addEventListener("keyup", (e) => getCrimeFullEvent(e)); formElem.addEventListener("mouseup", (e) => getCrimeFullEvent(e)); }); </script> </body> </html>

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Crime Formula Tests</title>
</head>
<body>
<div class="container" style="margin: 0 auto;">
    <form id="crime-selection-form" method="get">
        <div class="row py-2">
            <div class="col">
                <div class="form-group">
                    <label for="crime">Select Crime</label>
                    <select name="crime" id="crime" class="form-control">
                        <option value="0" selected>--- None ---</option>
                        %menu.opts:crimes%
                    </select>
                </div>
            </div>
        </div>
        <div class="row py-2">
            <div class="col-lg-6 col-md">
                <div class="form-group">
                    <label for="level">Level</label>
                    <input type="number" name="level" id="level" class="form-control" value="1" step="1">
                </div>
            </div>
            <div class="col-lg-6 col-md">
                <div class="form-group">
                    <label for="will">Will</label>
                    <input type="number" name="will" id="will" class="form-control" value="100" step="1">
                </div>
            </div>
        </div>
    </form>
</div>
<div class="container">
    <span class="d-block m-1" id="crime-formula-raw"></span>
    <span class="d-block m-1" id="crime-formula-formatted"></span>
    <span class="d-block m-1" id="crime-response"></span>
</div>
<script>
  const apiCall = (path) => {
    return fetch(path, {
      headers: {
        'credentials': 'same-origin',
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/json'
      },
      signal: AbortSignal.timeout(5000)
    });
  };
  const getCrime = (id, callback) => {
    apiCall(`crime-formula.php?action=get-crime-by-id&id=${id}`)
      .then(response => response.json())
      .then(data => callback(data));
  };
  const responseElem = document.getElementById("crime-response");
  const formulaRawElem = document.getElementById("crime-formula-raw");
  const formulaFormattedElem = document.getElementById("crime-formula-formatted");
  const updateCrimeInfo = (formula, will, level) => {
    let formulaFormatted = formula.replace('WILL', will).replace('LEVEL', level);
    formulaRawElem.innerText = formula;
    formulaFormattedElem.innerText = formulaFormatted;
    /* Dirty. Don't do this if you can avoid it. And certainly *never* trust user input with it. */
    let amnt = eval(formulaFormatted);
    responseElem.innerText = amnt.toLocaleString(undefined, {maximumFractionDigits: 3}) + "%";
  };
  const getCrimeFullEvent = (e) => {
    e.stopPropagation();
    e.preventDefault();
    if ([undefined, null].includes(responseElem) || [undefined, null].includes(formulaRawElem) || [undefined, null].includes(formulaFormattedElem)) {
      console.error("Form element missing: crime-response/crime-formula-raw/crime-formula-formatted");
      return;
    }
    let will = document.getElementById("will").value;
    let level = document.getElementById("level").value;
    let id = document.getElementById("crime").value;
    if ([undefined, null].includes(will)) {
      will = 100;
    }
    if ([undefined, null].includes(level)) {
      level = 1;
    }
    if ([undefined, null].includes(id)) {
      id = 0;
    }
    let formula = '((WILL*0.8)/2.5)+(LEVEL/4)';
    if (id > 0) {
      getCrime(id, (data) => {
        if ([undefined, null].includes(data)) {
          console.error("Blank crime data response");
          return false;
        }
        if (data.hasOwnProperty("type") && data.type !== "success") {
          console.error(data);
          return false;
        }
        updateCrimeInfo(data.crimePERCFORM, will, level);
        return true;
      });
    } else {
      updateCrimeInfo(formula, will, level);
    }
  };

  window.addEventListener("DOMContentLoaded", () => {
    const formElem = document.getElementById("crime-selection-form");
    if ([undefined, null].includes(formElem)) {
      console.error("Form element missing: crime-selection-form");
      return;
    }
    formElem.addEventListener("keyup", (e) => getCrimeFullEvent(e));
    formElem.addEventListener("mouseup", (e) => getCrimeFullEvent(e));
  });
</script>

</body>
</html>

 

 

That was my next step but damn you went all out that looks amazing i just did mine raw html with a jquery function but your kicked it up a whole lot 😄

whatever.html

<h1>Mccodes Crime Chance Calculator</h1>
<p>
  This was written in html css and jquery to calculate the success rates based
  on the users level and there will.
</p>

<table class='table' style='text-align: center; width: 40%;' cellspacin='1' cellpadding='1'>
<tr>
  <th>Users Level</th>
  <th>Users Will</th>
</tr>
<tr>
  <td><label for='level'>Level:</label></td> 
  <td><input type='number' min='1' value='1' max='100' id='level' required /></td>
</tr>
<tr>
  <td><label for='will'>Will:</label></td> 
  <td><input type='number' min='1' value='100' max='99999' id='will' required /></td>
</tr>
<tr><td colspan='2'><input type='sumbit' id='btnclick' value='Find Percentage' /></td></tr>
</table>

<table class='table' style='margin-top: 10px; text-align: center; width: 40%; height: auto;'>
  <tr><th>Result</th></tr>
  <tr><td><span id='result'></span></td></tr>
</table>

I wrote a little bit extra that i was planning on implentnting into if people were to use on there game. so if enter over 1000 into the box it just jumps back to max level which is currently set in the jquery but i will test yours as soon as get a server running again 😞

$(document).ready(function() {
  $('#btnclick').click(function() {
    let level = $('#level').val()
    let will = $('#will').val() 
    let crimeSuccess = calcChance(will, level)
    crimeSuccess = crimeSuccess > 100 ? 100 : crimeSuccess
    console.log('logged chance rate at ' + crimeSuccess)
    $('#result').text(crimeSuccess)
  })

  $('#level').keyup(function() {
    const maxLevel = 100;
    let levelcheck = $('#level').val()
    if (levelcheck > 100) {
      $('#level').val(maxLevel)
    }
  })
  $('#will').keyup(function() {
    const maxWill = 99999;
    let willcheck = $('#will').val()
    if (willcheck > 100) {
      $('#will').val(maxWill)
    }
  })

  // Value 1 is there level and value2 is there will
  function calcChance(value1, value2) {
    if (value1 && value2) {
      let result = ((value2 * 0.8) / 2.5) + (value1 / 4)
      result = result > 100 ? 100 : result
      return result + '%'
    }
  }
})

 

also i added checks to make sure not over 100% will deffo play around with yours 😛

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...