how to stop examplecodes/text within my html being recognized as code/activating

I’m currently working on a hackertyper, and my fake codes (the script that you "type out" is currently being read as real code, thus messing up my program. I also have commands within the fake code/script that need to trigger, which is br, because if they wont trigger, the entire program will be written on one line when someone is using the hackertyper.

Is there a command that lets me exclude the scriptcode, but leaves room for functions (I want to use the € sign as a function for br) so that my program will stop crashing? The solution to this is probably obvious but I can’t seem to find it, thanks in advance!

// pre defined variables
let num;
let counter;
counter = true;
num =0;

// this shows which text will go on the screen
let text = String("//right here is the problem. code that's put inside these brackets will be recognized as "real code" and at the same time <br>(or €)'s need to be recognized also// ");
// splits the text into an array after every character
let result = text.split("");

// start of the hackertyper
function keyPress(){
    music();
    document.onkeypress = function() {
        // where text will be shown
        num++;
        document.write(result[num]);
        // a loop
        keyPress();
    }
}

23 thoughts on “how to stop examplecodes/text within my html being recognized as code/activating”

  1. There are a few issues with your code :

    • You should avoid using document.write because it erases your original document. That’s why you had to reset the onkeypress event handler in your keyPress function. Instead, you should print the text into an HTML element.
    • You are looping without a limit. num is incremented and will, at some point, be higher than your result array’s length. You need to fix that or you will print “undefinedundefinedundefined”…
    • You should avoid using the String() constructor. For several reasons, the best one being that strings constructed with it are typed “object” and that can be harmful.
    • If you’re writing code inside that string, make sure you escape double quotes or use single quotes to enclose your strings. In the example you provide, you did not escape double quotes and that resulted in syntax errors.

    I fixed a few of the issues in the snipplet below. Hope this helps.

    // pre defined variables
    let num = 0;
    let counter = true;
    
    // this shows wich text will go on the screen
    let text = "let name = 'John';\nlet firstName = 'Jack'\n;";
    // splits the text into an array after every character
     let result = text.split("");
    
    // start of the hackertyper
    function keyPress(){
    
    // where text will be shown
     if (num < result.length) {
       document.getElementById("codeType").innerHTML += result[num];
       num++;
     }
     
    
    }
    
    document.addEventListener('keypress', keyPress);
    <span id="codeType"></span>

    Make sure you click on the snippet window before typing text, or it won’t work.

    Reply

Leave a Comment