-1

How do I take an array, modify it by moving each array number forward i places and if i is greater than 26 than subtract 26, and then displaying the array as it would be in plaintext.

http://jsfiddle.net/clarinetking/kLy83oxj/4/

var 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];
var text;
$('#assign').click( function() {
    text = $('#Input').val();
    num = $('#Number').val();
});

$('#rotate').click( function() {
    for(i=0;i<alphabet.length;i++) {
        alphabet[i] = alphabet[i+num];
        if (alphabet[i]>26){
            i=i-26;
        }
    }
});
$('#solve').click( function() {
    alphabet.toString();
        $('#Output').append(alphabet);
});

It's kind of hard to explain. Sorry. It's basically a case of a caesar cipher i.e imagine a wheel of letters. Move the wheel i places clockwise and each letter has a new position in the alphabet. I'm trying to reverse this process.

OH by the way a lot of people ask why I only ever use html. Short answer, that's all I understand except some pascal :)

Clarinetking
  • 143
  • 2
  • 11

2 Answers2

2

I'm not sure if I get what you mean but could you check this fiddle if there is a missing functionality I will add it. I made is so that when you click rotate it will put the first letter on the last part. Try clicking and then solve to see the output.

http://jsfiddle.net/kLy83oxj/6/

var 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'];
var text;
jQuery(document).ready(function(){
$('#assign').click( function() {
    text = $('#Input').val();
    num = $('#Number').val();
});

$('#rotate').click( function() {
    var first = alphabet[0];
    alphabet.splice(0,1);
    alphabet.push(first);
});
$('#solve').click( function() {
        $('#Output').html(alphabet.join(','));
    });
});

To learn more about javascript array start from here http://www.w3schools.com/js/js_arrays.asp

andrex
  • 983
  • 5
  • 14
  • This is a heck of a lot further than I got :) Thanks :D How does it all work so I can modify it? What's splice, push, .join() and why's the array in quotes? Thanks ever so much :) – Clarinetking Sep 21 '14 at 10:12
  • @Clarinetking, those answers you'll find in the manual. I noticed you don't know how to use the browser console and need to know the buttons TidyUp and JSHint on JSfiddle. Good luck! – brasofilo Sep 21 '14 at 10:14
  • Oh I do use JSHint but some of the error messages are ambiguous. I didn't, however, know you could use browser console tools on jsfiddle so thanks for that :D – Clarinetking Sep 21 '14 at 10:15
  • 1
    to learn more about javascript array http://www.w3schools.com/js/js_arrays.asp also try doing some google. Google is everyone's friend :) – andrex Sep 21 '14 at 10:17
  • 2
    Humble request - please don't share links to w3schools. They contains lots of outdated and misleading info. If you want to learn js arrays, refer something official like [MDN DOcs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) – T J Sep 21 '14 at 10:20
  • Just worked out how to use all those cool new functions!! A huge thanks to @andrex and MDN DOcs!!!!! – Clarinetking Sep 21 '14 at 10:54
2

You have 2 syntax errors in your code:

var 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];

should be

var 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'];

because all strings in javascript need to be enclosed by ' or ", or else they will be seen as variables. Then your second syntax error:

$('#solve').click( function() {
    alphabet.toString();
        $('#Output').append(alphabet);
    });
});

should be:

$('#solve').click( function() {
    alphabet.toString();

    $('#Output').append(alphabet);
});

(you closed one to many functions)

Erik Terwan
  • 2,710
  • 19
  • 28