Newbie JavaScript/Scriptable coding issue

Any help would be appreciated. Trying to create a script that would run when attach to CarPlay and ask if the trip I am taking counts as a tax deduction. If so will log data to airtable. The CarPlay will be part a shortcut and airtable code hasn’t beeen created yet. I have a limited version working in shortcuts. To up my automation game I just completed code academy Intro to Javascript. This is the first semi real thing I have made for myself outside of the codeacademy class. I think I have an issue with my asynchronous portion of the code. In line 99 I call a function for calculating final mileage based upon input of odometer data earlier. Based upon the error in the attached image my a.Mileage is undefined. If have tried many iterations for what is likely a simple fix but likely some fundamental coding thing I must not fully understand. Would appreciate any guidance


//Generates the date
let a = new Date();
let day = a.getDate();
let month = a.getMonth();
let year = a.getFullYear();
let date = `${month}-${day}-${year}`;

console.log(`Date is  ${date}`);


let odometerAlert = new Alert();
odometerAlert.title = "Current Milage";
odometerAlert.addTextField("Odometer", null);
odometerAlert.addAction('Submit');
odometerAlert.addCancelAction('Cancel');

await odometerAlert.presentAlert();
let odometer = odometerAlert.textFieldValue(0);

//set up alert 
let travelTaxDeduction = new Alert();
travelTaxDeduction.title = "Pick from Options:";
travelTaxDeduction.addCancelAction('Cancel');
travelTaxDeduction.addAction("Durham Clinic <==> DRH");
travelTaxDeduction.addAction("Durham Clinic <==> Alamance");
travelTaxDeduction.addAction("Durham Clinic <==> Duke North");
travelTaxDeduction.addAction("Durham Clinic <==> Raleigh");
travelTaxDeduction.addAction("Home <==> Airport");
travelTaxDeduction.addAction("Durham Clinic <==> Airport");
travelTaxDeduction.addAction("Other");




let startStopLocations  = await travelTaxDeduction.presentAlert();
  
if  (startStopLocations === 0){
let a  = {};
a.Trip = "Durham Clinic <==> DRH";
a.Mileage = 0.4;
console.log(a);
//return a;
} else if (startStopLocations === 1) {
let a = {};
a.Trip = "Durham Clinic <==> Alamance";
a.Mileage = 24;
console.log(a);
//return a;
} else if (startStopLocations === 2) {
let a = {};
a.Trip = "Durham Clinic <==> Duke North";
a.Mileage = 4.3;
console.log(a);
//return a;
} else if (startStopLocations === 3) {
let a = {};
a.Trip = "Durham Clinic <==> Raleigh";
a.Mileage = 29;
console.log(a);
//return a;
} else if (startStopLocations === 4) {
let a = {};
a.Trip = "Home <==> Airport";
a.Mileage = 15;
console.log(a);
//return a;
} else if (startStopLocations === 5) {
let a = {};
a.Trip = "Durham Clinic <==> Airport";
a.Mileage = 16;
console.log(a);
//return a;
} else if (startStopLocations === 6) {

let otherAlert = new Alert();
otherAlert.title = "Trip Details:";
otherAlert.addTextField("Start Location");
otherAlert.addTextField("End Location");
otherAlert.addTextField("Distance");
otherAlert.addCancelAction('Cancel');
otherAlert.addAction("Submit");

let otherLocationPromise  = await otherAlert.presentAlert();
let enteredStartLocation = otherAlert.textFieldValue(0);
let enteredEndLocation = otherAlert.textFieldValue(1);

let a = {};

a.Trip = `${enteredStartLocation} <==> ${enteredEndLocation}`;
a.Mileage = otherAlert.textFieldValue(2);
console.log(a);
//return a;
} else {console.log("Cancelled")};




//function FinalOdometerFunc(odometer,a.Mileage) {
let finalOdometer = odometer + a.Mileage;
//return finalOdometer};



//FinalOdometerFunc(odometer, a.Mileage);
console.log(`Initial odometer reading is ${odometer}`);
console.log(`Final odometer reading is ${finalOdometer}`);


Script.complete();
![image|666x500](upload://7i7FqO75p4zGkRGThIjakGrcdnp.jpeg) 

It is not obvious at first glance, but a is undefined at the end, because you defined it with let in each if closure, but variables declared with let are only available in the same closure and closures within it. Because the end is outside of the closures, a is undefined.

A possible fix would be to move the lines let a = {}; before the first if.

1 Like

Makes perfect sense will give it a try. I figured it was something fairly simple

That worked. Spent forever trying to figure it out. Thanks