0

I need to use a for loop to add 2 random letters to the string inputed into the text box and returned in the encrypted string box when the button is clicked.

So, for example, if cat was inputted it could return like cynarwtpp. I am new to for loops and unsure how to proceed from here, I need to use a for loop that will cycle through the alphabet array. any help would be greatly appreciated.

Javascript:

<script type="text/javascript">

 var uncoded_array = uncoded.split("");
 var coded_str = "";
 var alphabet = new    Array("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");
</script>

Html:

<form action="">
Enter a String: <input type="text" name="uncoded" ></br>
<input type="button" value="cipher" onClick=document.forms[0].coded.value=    ></br>
Encrypted String: <input type="text" name="coded" ></br>
Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22
mb93
  • 9
  • 1
  • what type of encryption algorithm, you have framed? In what basis `cat` is encrypted to `cynarwtpp`? Is there any pattern matching you have planned? – Praveen Nov 07 '13 at 04:36
  • Two letters are to be randomly selected from the alphabet and placed after each letter in cat. so c (random) (random) a (random) (random) t (random) (random) – mb93 Nov 07 '13 at 04:45
  • I wouldn't say that that is secure. Also PLEASE just use `["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']` – bjb568 Nov 07 '13 at 04:49

3 Answers3

0

This is what I would do

html

Enter a String: <input type="text" id="uncoded" />
<input type="button" value="cipher" onclick="cypher(); return false;" />
Encrypted String: <input type="text" id="coded" />

js

function cypher() {
  var coded = document.getElementById('coded');
  var uncoded = document.getElementById('uncoded');
  coded.value = uncoded.value.split('').map(function (char) {
      return char + randomLetter() + randomLetter();
  }).join('');
}

function randomLetter() {
  return Math.random().toString(36).replace(/[^a-zA-Z]/gi, '')[0];
}
kavun
  • 3,358
  • 3
  • 25
  • 45
0

Here is a simple approach.

1) From this answer I learned to pick random element from array.

var item1 = alphabet[Math.floor(Math.random()*alphabet.length)];
var item2 = alphabet[Math.floor(Math.random()*alphabet.length)];

In your case 2 random letter from array.

2) In the for iteration, I have taken a string length and used to add the random element after each letter and concatenated together.

var alphabet = new Array("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");


var original = "cat";
var encrypted = "";
for (var i=0; i<original.length; i++ ) {
    var item1 = alphabet[Math.floor(Math.random()*alphabet.length)];
    var item2 = alphabet[Math.floor(Math.random()*alphabet.length)];
    encrypted += original[i] + item1 + item2;    
}
alert(encrypted);

JSFiddle

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

Here's a simple function that performs the string operation. Just feed it the value of the first form input, and dump its result into the second form input.

function cipher(str) {
    var rand, 
        output = '',
        chars  = 'abcdefghijklmnopqrstuvwxyz';

    for (i=0; i<str.length; i++) {
        output += str[i];
        for (j=0; j<2; j++) {
            output += chars[Math.floor(Math.random() * chars.length)];
        }
    }

    return output;
}

cipher('cat');
Ben Y
  • 1,711
  • 1
  • 25
  • 37