From 877f6091fd4837e762872f394789d41228541314 Mon Sep 17 00:00:00 2001 From: Sreehari <126333578+thesrhari@users.noreply.github.com> Date: Sun, 4 Aug 2024 17:24:16 +0530 Subject: [PATCH] Modify index.html, Create script.js Fixed typos and modified contents in index.html. Created script.js --- index.html | 17 +++++++---------- script.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 script.js diff --git a/index.html b/index.html index 8e9decd..59bd5fd 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,7 @@

Caesar Cipher

+



@@ -14,22 +15,18 @@

Caesar Cipher



- +
- +

- +

- + + + diff --git a/script.js b/script.js new file mode 100644 index 0000000..23f46ff --- /dev/null +++ b/script.js @@ -0,0 +1,50 @@ +const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', + 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + + +function caesar(text, shift, direction){ + let caesar_text = ""; + shift = Number(shift); + if (direction == "decode"){ + shift *= -1; + } + for (const letter of text){ + if (alphabet.includes(letter)){ + const position = alphabet.indexOf(letter); + const shifted_position = position + shift; + const alphabet_length = alphabet.length; + const new_position = mod(shifted_position, alphabet_length); + caesar_text += alphabet[new_position]; + console.log(position + shift); + } + else{ + caesar_text += letter + } + } + let output = document.getElementById("output"); + output.value = caesar_text; +} + +function mod(a, b){ + return ((a % b) + b) % b; +} + + +function getValue(){ + let plain_text = document.getElementById("plain_text").value; + let shift_amount = document.getElementById("shift_amount").value; + let direction_type; + if (document.getElementById("encode").checked){ + direction_type = document.getElementById("encode").value; + } + else if (document.getElementById("decode").checked){ + direction_type = document.getElementById("decode").value; + } + + if ((plain_text == "") || (shift_amount == "") || (direction_type == undefined)){ + alert("Please enter all fields!"); + } + else{ + caesar(plain_text, shift_amount, direction_type); + } +} \ No newline at end of file