JSFiddle - React, Tailwind, and code Playground

by rushtonmd

HTML

<button id="gogo">GO!</button>
<span id="message"></span>

JavaScript

// There is the possibility that this function
// will be unable to perform it's special purpose

function firstItemInArray(value) {
    
    if(value instanceof Array) {
        
        if (value.length < 1) throw new Error("Empty Array!");
        
        return value[0];        
    }
    
    throw new Error("Not an array!");
   
}

// Wirin' up stuff - do you really need a try/catch??
$('#gogo').on('click', function (event) {
    try {
        var returnMessage = firstItemInArray(["Audi","Porsche","DeLorean"]);
        //var returnMessage = firstItemInArray([]);
        //var returnMessage = firstItemInArray({firstname:"Rad",lastname:"Butoni",age:97});
        $('#message').html(returnMessage);
    } catch (error) {
        alert("WARNING! " + error.message);
    }
});