Tuesday, July 14, 2026

List Functions Game - Guess What is Missing

You need to use the List Functions from our last post. Here's a game.

Tweak it. Devour it. Then write your own.

Copy and paste this into a cffiddle.org session.
Yep. Remember to wrap it with <cfscript> tags. Feel free to thank me later.


// ---------------------------------------------------------------------------------------
// GAME with a SMART Mission
// What does that look like?
//
// SMART: Specific, Measurable, Achievable, Revelant and Time-bound
// S: List Functions - Guess What is Missing from the List
// M: Read and digest within 5 minutes
// A: Anyone new/familiar with programing can use them immediately
// R: Use a data in a common ways anyone will mentally and emotionally connnect with
// T: Read within 3 min, Research more within seconds and Experiment with immediately.
// ---------------------------------------------------------------------------------------
// FUNCTIONS USED:
//  List: listLen, listGetAt, listDeleteAt
//  String: trim
// ---------------------------------------------------------------------------------------

// Add a CSS class to render the "Answer" to the bottom of the page
// ** This isn't a best practice, soldier.  
// ** Be a pro, put styles in an external stylesheet.
writeOutput("<style>.pushDownLower { margin-top: 22em; }</style>");

// Initialize the list.  You need your protein and it better be free-range and grass-fed.
originalList = "Beef, Chicken, Fish, Lamb, Pork, Turkey";

listLength = originalList.listLen();

// Pick a random index to delete (between 1 and total items)
randomIndex = RandRange(1, listLength);

// Get the exact item being removed for the game log
removedItem = originalList.listGetAt(randomIndex);

// Remove the random item from the list
updatedList = originalList.listDeleteAt(randomIndex);

// You'll need a humorous retort later.
// No one likes stuff taken away from them.
snappyComeBackArray = [
 "Moo! Moo! I like my shoes.  Give them back.",
 "I like my BBQ with sweet potatoes and deep-fried.",
 "Fresh sushi is calling my name, even now.",
 "Get in my belly with caramelized onions.",
 "Everything goes better with bacon.",
 "Oh how thankful I am.  I am."
];

// Capture the output inside a variable using savecontent
savecontent variable="gameOutput" {
  writeOutput("
    <h2>Guess What is Missing...</h2>
    <p><b>Original:</b> #originalList# (#listLength# items)</p>
    <p><b>Updated:</b> #updatedList#</p>
    <p class='pushDownLower'><hr>Randomly removed item <em>#randomIndex# (#removedItem.trim()#)...</em></p>
    <h1>#snappyComeBackArray[randomIndex]#</h1>
  ");
}

// Display the captured content to the screen
writeOutput(gameOutput);

If you have another game in mind, let me know in the comments.

No comments: