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

Drop-down Navigation in HTML and CSS is a popular way to create

My navigation menu is functioning well and has an appealing design. The HTML structure for the menu is as follows: <div id="menubar"> <div id="welcome"> <h1><a href="#">Cedars Hair <span>Academy</span></ ...

Issues with PHP and mySQL search functionality

<?php $username = "root"; $password = ""; $hostname = "localhost"; $db_handle = mysql_connect($hostname, $username, $password) or die ("Could not connect to the database"); $selected= mysql_select_db("login", $db_handle); ...

JavaScript function failing to validate password

While engaging in an online game where the objective is to uncover a password by inspecting the page source or element, I encountered a puzzling line: if(el.value == ""+CodeCode+""). My assumption is that el.value represents my guess, and it indicates that ...

Is it possible to automatically access the most recent Beta build through package.json and npm?

We are currently working on a project that has a specific dependency requirement for the latest beta build from an npm library. However, there are also -dev builds within the library. For instance, in the "x-library" there might be versions like: "1.2.3- ...

Build a nested block containing a div, link, and image using jQuery or vanilla JavaScript

I have a button that, when clicked, generates a panel with 4 divs, multiple href links, and multiple images. I am new to web programming and understand that this functionality needs to be in the Javascript section, especially since it involves using jsPlum ...

Leveraging Material UI's Button element along with the enctype attribute

I'm working on incorporating Multer for image uploads and utilizing Material UI's button component with the onClick prop to submit the data. If I wrap the button component within a form tag, will it achieve the same result? If not, how can I spec ...

Is it possible to capture a Browser TAB click event using JavaScript or AngularJS?

How can I trigger an event when returning to a tab? For instance: Let's say I have opened four tabs in the same browser with the same URL: such as: http://127.0.0.1:/blabla http://127.0.0.1:/blabla http://127.0.0.1:/blabla http://127.0.0.1:/blabla ...

Encountering a JSON_PARSER_ERROR when trying to call Google FCM using MobileFirstAdapter JS

I am working on integrating Google FCM Api for sending Push Notifications. Below is the snippet of code from my JavaScript file: function sendNotificationToUser() { var request={ path :'/fcm/send', method: 'POST&ap ...

Discovering the value of an item when the editItem function is triggered in jsGrid

Within my jsGrid setup, I have invoked the editItem function in this manner: editItem: function(item) { var $row = this.rowByItem(item); if ($row.length) { console.log('$row: '+JSON ...

What steps should I take to resolve issues with the npm installation on Linux?

I'm encountering an issue while attempting to use npm install in order to install a package. Despite my attempts to update and re-download from the root directory, I am unable to resolve the error. hackathonday1-2 git:(save-button) ✗ npm install f ...

The dropdown item in Tailwindcss is unexpectedly flying off the edge of the screen rather than appearing directly under the dropdown button

Currently, I am developing an application using Rails, Vue, and TailwindCss 1.0+ I am facing an issue while creating a dropdown menu for my products. When I click on the dropdown button, the items in the dropdown fly off to the edge of the screen instead ...

What is the purpose of the assertEquals() method in JSUnit?

Currently, I am exploring unit test exercises with a HTML5/JS game that I created and JSUnit test runner. The simplicity of the setup impresses me, but I have noticed that even the documentation lacks a clear explanation of what assertEquals() truly does. ...

The integration of Angular 6 with AngularJS components fails to load properly in a hybrid application

Currently, I am in the process of upgrading a large AngularJS version 1.7.3 to a hybrid app using Angular 6. The initial phase involved converting all controllers/directives into an AngularJS component. Subsequently, I created a new Angular 6 project skele ...

Can one track the moment when a user clicks on an advertisement within a YouTube video?

Can we track when a user clicks on an advertisement within a YouTube video? We have integrated the YouTube API v2 into our website, allowing users to watch videos. The issue is that when a user clicks on an advertisement, the YouTube video pauses automat ...

Implement lazy loading for scripts in Next JS

Currently working on a project with Next JS and focusing on optimizations through Google's Page Speed Insights tool. One major performance issue identified is the presence of 3rd party scripts, specifically the Google Tag script (which includes Google ...

The user is defined, but the user's user ID is not specified

It seems that the user is defined, but user.user_id is not. My framework of choice is express.js and passport.js. router.post('/requestSale', function(req,res){ console.log('session user: ' + req.session.passport.user); //logs ...

Sorting function not working on dynamic Angular table

I'm currently facing a challenge with sorting a dynamic Angular table. If I manually code the table headers, everything works smoothly. Fiddle: https://jsfiddle.net/xgL15jnf/ <thead> <tr> <td> <a href="#" ...

What could be causing my carousel slider images to not adapt to different screen sizes?

I recently added a carousel slider to my website, and while it looks great on larger screens like desktops and laptops, I noticed that the images are not scaling properly when viewed on smaller screen widths. This has resulted in non-responsiveness which i ...

Safari experiencing a CSS issue not found in Chrome or Firefox

https://gist.github.com/2354116 When you check the link above in Chrome or Firefox, everything looks good. The divs at the bottom, including the headings and social icons, are centered within a container div without any issues. However, when viewed in Sa ...

Is searching for duplicate entries in an array using a specific key?

Below is an array structure: [ { "Date": "2020-07", "data": [ { "id": "35ebd073-600c-4be4-a750-41c4be5ed24a", "Date": "2020-07-03T00:00:00.000Z", ...