Help With Coding Minigame

Twine Version: 2.10.0.0 Harlowe Version 3.3.9
Hello everyone thanks for taking the time to help and read my post. So basically I am trying to create a minigame which involves a mechanical lock that the player must enter the correct code in order to move on to the next passage. I made a code for it and made the necessary passages. However, when I enter the correct code and press enter instead of sending me to my passage titled “Door” it sends me to a new tab in my browser with a white background with plain black text saying (goto: “Door”) can anyone please help me fix this code so it functions correctly? If you need more details I’ll be happy to provide them but here is my code in question:

(set: $code to "")
(set: $correctCode to "12169125")

<style>
.lock-container {
    width: 300px;
    margin: 20px auto;
    background: #000;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
}

.display {
    background: #222;
    color: #0f0;
    font-family: 'Courier New', monospace;
    padding: 10px;
    margin-bottom: 15px;
    text-align: center;
    font-size: 24px;
    letter-spacing: 5px;
    border: 2px solid #333;
    height: 40px;
    line-height: 40px;
}

.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    padding: 10px;
}

.key {
    background: #222;
    color: #0f0;
    border: none;
    padding: 15px;
    font-size: 20px;
    cursor: pointer;
    transition: all 0.2s;
    border-radius: 5px;
}

.key:hover {
    background: #333;
    transform: scale(0.95);
}

.key:active {
    background: #444;
}

.error-message {
    color: red;
    text-align: center;
    margin-top: 10px;
    opacity: 0;
    transition: opacity 0.5s;
    font-weight: bold;
    font-size: 18px;
}

.error-message.show {
    opacity: 1;
}
</style>

<div class="lock-container">
    <div class="display" id="codeDisplay">--------</div>
    <div class="keypad">
        <button class="key" data-value="1">1</button>
        <button class="key" data-value="2">2</button>
        <button class="key" data-value="3">3</button>
        <button class="key" data-value="4">4</button>
        <button class="key" data-value="5">5</button>
        <button class="key" data-value="6">6</button>
        <button class="key" data-value="7">7</button>
        <button class="key" data-value="8">8</button>
        <button class="key" data-value="9">9</button>
        <button class="key" data-value="clear">C</button>
        <button class="key" data-value="0">0</button>
        <button class="key" data-value="enter">E</button>
    </div>
    <div class="error-message" id="errorMessage">Nope...</div>
</div>

<script>
$(document).ready(function() {
    const display = document.getElementById('codeDisplay'); // The display for the entered code
    const errorMessage = document.getElementById('errorMessage'); // The error message element

    // Local variable to handle code input
    let enteredCode = "";

    $('.key').on('click', function() {
        const value = $(this).data('value'); // Get the value of the button pressed

        if (value === 'clear') {
            // Clear the entered code
            enteredCode = "";
            display.textContent = "--------"; // Reset the display
            return;
        }

        if (value === 'enter') {
            // Check the entered code against the correct code
            if (enteredCode === "12169125") {
                // Navigate to the next passage using Harlowe's link system
                window.location.href = "javascript:void(0)";
                document.write('(goto: "Door")');
            } else {
                // Show an error message if the code is incorrect
                errorMessage.classList.add('show');
                setTimeout(() => errorMessage.classList.remove('show'), 2000);
                enteredCode = ""; // Reset the code
                setTimeout(() => display.textContent = "--------", 500);
            }
            return;
        }

        // Append the entered key to the code if it's less than 8 characters
        if (enteredCode.length < 8) {
            enteredCode += value;
            display.textContent = enteredCode.padEnd(8, "-"); // Pad with dashes
        }
    });
});
</script>

Harlowe notoriously doesn’t play well with JavaScript, and, unlike SugarCube, doesn’t have APIs to be used in JavaScript code to change how the story goes/behaves.
Buuuuut there are some hacks available to help a bit (the solution is taken from there).

For your case, add the following in the JavaScript section of your project:

window.hacks = window.hacks || {
    _engine : Engine
};

hacks.goto = hacks._engine.showPassage;

And replace

document.write('(goto: "Door")');

with

hacks.goto('Door');

It’s not that Harlowe doesn’t play well with JavaScript, the Story Format has been deliberately designed by its Developer to limit an Author’s ability to use JavaScript to extending the function of their project or that of the Harlowe runtime engine itself.

Only recently(ish) has the Developer altered the engine so Story & Temporary Variables are accessible within HTML <script> elements that are placed within the contents of a Passage.

The Developer seems to believe that an Author who is inexperienced with software development shouldn’t need to learn four different programming languages (1) to be able to create a Twine project.

WARNING: Harlowe’s Developer often alters the internals of the runtime engine when they release a new version of the Story Format. And because the Developer considers those internals for their use only, they generally give no public warning when they do so. So don’t be surprised, like some other Author’s have been, if your JavaScript code stops working the next time you update the Twine 2.x application.

NOTE: The document event that the ready() method is waiting for has already occurred before the contents of the Passage started being processed, so using with a Passage like you are serves no real purpose.

(1) the four languages being: the engines Macro language; HTML; CSS; and JavaScript. And yes, I’m aware that technically HTML & CSS aren’t considered programming languages. However CSS can now semi-manipulate data, so maybe soon…

THANK YOU SO MUCH! I have been struggling with this problem for a while now trying to fix it by myself but thanks to you it is now working flawlessly. Even all these words can’t describe how grateful I am to you. Have an amazing life! :slight_smile: