How do I add stamina correctly?

I am new-ish to JavaScript, and I was using it to add a stamina feature. I am not COMPLETELY new to JavaScript, as I know a bit about it. Can someone help fix my errors?

var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float; // distance to ground
function Start(){
    chMotor = GetComponent(CharacterMotor);
    tr = transform;
    var ch:CharacterController = GetComponent(CharacterController);
    dist = ch.height/2; // calculate distance to ground
function Update(){
    var vScale = 1.0;
    var speed = walkSpeed;
    if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
        speed = runSpeed;
    if (Input.GetKey("c")){ // press C to crouch
        vScale = 0.5;
        speed = crchSpeed; // slow down when crouching
    chMotor.movement.maxForwardSpeed = speed; // set max speed
    var ultScale = tr.localScale.y; // crouch/stand up smoothly 
    tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
    tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
    var stamina = 100;
    if (speed > 13 && Input.GetKey("left shift"))
    var myVars=setInterval(function(){decreaseStamina(x);},1000);
    if (stamina < 100){
    function decreaseStamina(x) = { x -= 5;}
    {var decreaseStamina = setInterval(function() {decreaseStamina(stamina);}, 2000);
    if (chMotor.grounded && !Input.GetKey("left shift"))
    var myVar=setInterval(function(){coolDown(x)},1000);
    function coolDown(x) = {if (x < 100) x = x + 5;}

The stamina portion is from line 32 on. From 32 on is completely original. I know this is someone else’s code. I am using it to test my JS knowledge. I do not know much about functions, so that is most likely where the problem lies. I get these error codes:

Assets/RunAndCrouch.js(37,14): BCE0044: expecting (, found ‘decreaseStamina’.


Assets/RunAndCrouch.js(37,39): BCE0044: expecting :, found ‘-=’.

If anyone could help, that would be nice.

I don’t know a lot about JavaScript, but it looks like maybe you aren’t setting up the function correctly.

Specifically, this in-line declaration that the compiler is complaining about:

function decreaseStamina(x) = { x -= 5;}

In my experience, functions are normally not included inside other functions. I would expect the decreaseStamina function to be included below the end bracket ( } ) for the Update function and to look like this:

    function decreaseStamina(x)
        x -= 5;

I would expect there to be further issues like scoping on ‘x’, but hopefully that’s at least a start for you!