JavaScript encoding and decoding challenges

Can anyone help me figure out what's wrong? I'm trying to encode and decode a simple input, but it just doesn't seem to work! Any ideas why? Thanks in advance for your assistance :)

ENCODE:

function encryption_encode(s, delta) {
    var temp = "";
    var alt, neu;
    for(var i = 0; i < s.length; i++) {
        alt = s.charCodeAt(i);
        if(alt >= 65 && alt <= 90) {
            neu = alt + delta;
            if(neu > 90) {
                neu -= 26;
            }
        } else if (alt >= 97 && alt <= 122) {
            neu = alt + delta;
            if (neu > 122) {
                neu -= 26;
            };
        } else {
            neu = alt;
        }
        temp += String.formCharCode(neu);
    }
    return temp;
}

DECODE:

function encryption_decode(s, delta) {
    var temp = "";
    var temp, neu;
    for (var i = 0; i < s.length; i++) {
        alt = s.CharCodeAt(i);
        if (alt >= 65 && alt <= 90) {
            neu = alt - delta;
            if (neu < 65) {
                neu += 26;
            }
        } else if (LT >= 97 && alt <= 122) {
            neu = alt - delta;
            if (neu < 97) {
                neu += 26;
            }
        } else {
            neu = alt;
        }
        temp += String.formCharCode(neu);
    }
    return temp;
}

HTML: I've set up a textarea for the input and another one for the output

<html>
<head>
    <meta  charset="UTF-8">
    <title>encrypt |</title>
    <link rel="stylesheet" type="text/css" href="encode.js">
    <link rel="stylesheet" type="text/css" href="decode.js">

    <script type="text/javascript">

        function encrypt(f) {
            var input = f.elements["input"].value;
            var delta = parseInt(f.elements["delta"].value);
            var output = encryption_encode(input, delta);
            f.elements["output"].value = output;
        }

    </script>

</head>
<body>

    <form onsubmit="return false;">
        <textarea name="input" cols ="70" rows="10"></textarea> <br />
        <input type="text" name="delta" value="13" />
        <input type="button" value="Encode" onclick="encrypt(this.form);" />
        <textarea name="output" cols="70" rows="10" onfocus="this.blur();"></textarea>

</body>
</html>

Answer №1

HTML

<html>
<head>
    <meta  charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="encode.js">
    <link rel="stylesheet" type="text/css" href="decode.js">

    <script type="text/javascript">

        function encodeText(inputForm) {
            var input = inputForm.elements["input"].value;
            var shift = parseInt(inputForm.elements["shift"].value);
            var output = codeEncode(input, shift);
            inputForm.elements["output"].value = output;
        }

    </script>

</head>
<body>

    <form onsubmit="return false;">
        <textarea name="input" cols ="70" rows="10"></textarea> <br />
        <input type="text" name="shift" value="13" />
        <input type="button" value="Encode" onclick="encodeText(this.form);" />
        <textarea name="output" cols="70" rows="10" onfocus="this.blur();"></textarea>

</body>
</html>

DECODE:

function decodeText(s, shift) {
    var temp = "";
    var old, newChar;
    for (var i = 0; i < s.length; i++) {
        old = s.charCodeAt(i);
        if (old >= 65 && old <= 90) {
            newChar = old - shift;
            if (newChar < 65) {
                newChar += 26;
            }
        } else if (old >= 97 && old <= 122) {
            newChar = old - shift;
            if (newChar < 97) {
                newChar += 26;
            }
        } else {
            newChar = old;
        }
        temp += String.fromCharCode(newChar);
    }
    return temp;
}

ENCODE:

function codeEncode(s, shift) {
    var temp = "";
    var old, newChar;
    for(var i = 0; i < s.length; i++) {
        old = s.charCodeAt(i);
        if(old >= 65 && old <= 90) {
            newChar = old + shift;
            if(newChar > 90) {
                newChar -= 26;
            }
        } else if (old >= 97 && old <= 122) {
            newChar = old + shift;
            if (newChar > 122) {
                newChar -= 26;
            };
        } else {
            newChar = old;
        }
        temp += String.fromCharCode(newChar);
    }
    return temp;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to set a read-only attribute using getElementByName method

To make a text field "readonly" by placing JavaScript code inside an external JS file, the HTML page cannot be directly modified because it is located on a remote server. However, interaction can be done by adding code in an external JS file hosted on a pe ...

Have I got it all wrong with the way Controllers communicate with Services and how I conduct unit testing?

Currently, I am in the process of developing an AngularJS application and I want to ensure it follows best practices. When making external API calls using either $resource or $http, I always do this within a separate Service or Factory. However, I am unsur ...

Cryptocurrency price tracker with sleek Bitcoin symbol and FontAwesome icons

My assignment involved creating a function that retrieves Bitcoin trades from a JSON URL, allows users to change the interval with buttons, uses fontawesome arrows to indicate rate changes (up/down/no change), and displays the data on a website. Everythin ...

Converting a JS result set into JSON format

Having an issue with converting code from XML output to a JSON object instead. Here is the current code: public String evaluate() throws Exception { // Code block here } I need assistance in using GSON JSON utility methods to convert the result s ...

The size of the website elements needs to be increased for better visibility on mobile devices

While developing the official website for my Discord bot - Idle Baker, I opted to utilize Bootstrap 5 for its user-friendly design features, making it easier than dealing with extensive CSS coding. After completing the homepage of the site, I encountered ...

Issues with navigating the HTML menu are preventing users from properly accessing

Below is the structure of my menu in HTML: <div id=navigation> <ul id=nav> <li> <asp:HyperLink ID=HyperLink1 runat=server NavigateUrl=~/Home.aspx>Home</asp:HyperLink> ...

What is the procedure for defining the secret code for a private key in saml2-js?

I need to implement a key/cert with a passphrase in my project that's currently using saml2-js. I have already set up everything but encountering a bad decrypt error without the passphrase. Is there a way to incorporate this passphrase? Below are the ...

Display the table upon completion of the form submission

I am trying to create a form where the table #mytable is only displayed after the form has been submitted. If nothing is entered in the form, the table should remain hidden. Any suggestions on how I can achieve this? <form action="" id="myform" method ...

Guide to setting up page.js

I am eager to incorporate page.js into my project. However, it seems like it requires some type of build system, so I'm not able to locate a single page.js file to include in my page and immediately begin using. page('/',index) Does anyone ...

Implementing Ideone API functionality on Codeigniter with the use of ajax, javascript, and soapclient

I am new to using Codeigniter and I apologize if my question is basic. I found some code on this site: Working with IDE One API (Full project code available here) and I'm attempting to incorporate it into Codeigniter. I have been able to get it worki ...

The Navbar is throwing a TypeError because it is unable to retrieve the property 'classList' of null

I am currently experimenting with building a navbar in HTML that has the capability to dynamically switch pages without changing links using href. The method I'm employing to achieve this involves adding a classList of is-active to the div elements wi ...

Having issues with the Bootstrap tabs code provided in the documentation not functioning correctly

Exploring the world of Bootstrap 5 tabs led me to copy and paste code from the official documentation (https://getbootstrap.com/docs/4.1/components/navs/#javascript-behavior), but unfortunately, it's not functioning as expected: clicking on a tab does ...

Encountering the error message "handleChange is not a function" when trying to select a date in Material UI

Encountering an error message 'handleChange is not a function' when selecting a specific date in the DatePicker component. The DatePicker component is nested within the Controller component of react-hook-form. The expected behavior is to display ...

Validating decimals in a text field with either JavaScript or jQuery

I need to implement text field validation on the keyup event. The text field should only accept money type decimals such as: (12.23) (.23) (0.26) (5.09) (6.00) If any incorrect value is entered, it should revert back to the previous value and remove the ...

save the received data to a new document

How can I pass the data returned from the API below to another function in a separate JavaScript file? This question may be similar to others, but those do not involve working with returned results. Please consider this before labeling it as a duplicate. ...

Angular is able to select an element from a specified array

I'm currently struggling with using Angular to manipulate a TMDB API. I am having difficulty retrieving an item from an array. Can someone provide assistance? Here is the response that the array returns: { "id": 423108, "results ...

Accessing content from a different HTML document

I'm currently working on a project using node.js where I need to take input from a text box in an HTML file and store it in an array. However, I'm struggling with how to achieve this. My ultimate aim is to create a chat application, and I believe ...

Tailwind - make sure the dropdown list is always on top of all other elements

Having an issue configuring the position of a dropdown list. The objective is to ensure it stays on top of all elements, however, when inside a relative positioned element, it ends up being obscured by it. Here's an example code snippet to illustrate ...

When attempting to run Protractor, an error occurs indicating that the module '../built/cli.js' cannot be located

Due to an issue present in Protractor 3.3.0 with getMultiCapabilities, we had to install the latest version directly from GitHub where a fix has been implemented (refer to the fix scheduled for Protractor 3.4). To include this fix, we updated our package. ...

Achieving a seamless scrolling effect using CSS

I recently completed the design of a basic website that includes both mobile and desktop versions. However, I am looking to incorporate some animation into the site to enhance its appearance. One specific feature is a "go up" link at the bottom of the mob ...