Eric's Advanced Web Scripting Workspace

Butler Community College Spring 2026

Code Explanation

This code sets up some inventory items and a buy() function checks if they're in stock and returns the remaining quantity.

let sport = 'football';
// let id = 5;
let id:number;
id = 5;
let confirmed: string | boolean = 'yes';
confirmed = true;

We are setting variables here by declaring the types before giving them values. The first line just simply assigns the string 'football' to the variable 'sport'. The second line is a comment. The third line declares id to be a number type before declaring id = 5. The next line declares 'confirmed' as both string and boolean, initially assigning the string 'yes' to it. The final line redeclares 'confirmed' to be true which is a boolean.

type myAlias = string | number;

This line creates a reusable data type called an alias. 'myAlias' now represents either a string or a number.

type itemObj = {
    title: string;
    serial: myAlias;
    inventory: number;
}

This creates an object template. We're declaring the data types we want in our item objects before declaring the actual data.

const itemA: itemObj = {
    title: 'Box',
    serial: 234,
    inventory: 4
};

const itemB: itemObj = {
    title: 'Bowl',
    serial: '2er45',
    inventory: 3
};

This creates our actual objects. If we declare data that doesn't follow our object template then the code wouldn't work. The template saves time from debugging by using strict type rules.

function buy(cart: number, item: string) {
    let theMess: string;
    if (item == 'a' || item == 'A') {
        if (cart > itemA.inventory) {
            theMess = `You can't buy that many of the ${itemA.title}`;
        } else {
            theMess = `There are ${itemA.inventory-cart} of ${itemA.title}`;
        }
    } else {
        if (cart > itemB.inventory) {
            theMess = `You can't buy that many of the ${itemB.title}`;
        } else {
            theMess = `There are ${itemB.inventory-cart} of ${itemB.title}`;
        }
    }
    return theMess;
};
console.log(buy(2, 'B'));

This function checks whether you can buy a certain number of items and returns a message about the remaining inventory. It looks at the item parameter to decide between item A or B, compares the requested cart quantity and either says you can't buy that many or tell you how many would be left.

let person = [false, 'John', 'Plumber'];
person[0] = true;
console.log(`${person[1]} is a ${person[2]}`);

This creates an array called person with three values: a boolean, a name, and a job. Then it changes the first value from false to true, and logs a sentence using the second and third values.

console.log(id);

This simply logs to the console our id variable above. So, it would display the number 5 to the console.

let origValue = {val01: 1, val02: 2};
let refPoint = origValue.val02;

This creates an object origValue with two properties, val01 and val02. Then it reads the value of val02 and stores it in a new variable called refPoint.

if (origValue.val01 == refPoint) {
    console.log('They\'re equal.');
} else {
    console.log('Not even close.');
}

This code compares origValue.val01 with refPoint. If they have the same value, it prints "They're equal."; otherwise, it prints "Not even close.". Since we declared earlier that origValue.val01 = 1 and refPoint = 2, the condition is false so the output is going to be "Not even close.".