Action

Tip Calculator

Posted by agiletortoise, Last update about 2 years ago

Type the total of your tab in a draft. This action will prompt you to select the number of people splitting the tab, and the percentage to tip, and display the tip, final total, and split amount due from each person.

Steps

  • script

    // get text of draft
    let s = draft.content;
    // string non-numerics
    s = s.replace(/[^\d\.]/g);
    // get number value
    let n = Number(s);
    
    
    // prompt for values
    let p = new Prompt();
    p.title = "Tip Calculator";
    p.message = `Total: ${n.toString()}`;
    
    p.addTextField("people", "Ways to Split", "1");
    p.addSelect("tip", "Tip", ["10", "15", "20", "25", "30"], ["20"], false);
    p.addButton("Calculate");
    
    if (p.show()) {
    	let people = parseInt(p.fieldValues["people"]);
    	let tip = parseInt(p.fieldValues["tip"][0]);
    
    	let tipAmount = (n * (tip / 100));
    	let total = n + tipAmount;
    	let split = total / people;
    	alert(`Tab Total: ${n.toLocaleString('en-US', {
      style: 'currency',
      currency: 'USD',
    })}
    	
    Tip: ${tipAmount.toLocaleString('en-US', {
      style: 'currency',
      currency: 'USD',
    })}
    Total: ${total.toLocaleString('en-US', {
      style: 'currency',
      currency: 'USD',
    })}
    
    Split: ${split.toLocaleString('en-US', {
      style: 'currency',
      currency: 'USD',
    })}
    	`);
    }
    else {
    	context.cancel();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.