Skip to content

Instantly share code, notes, and snippets.

@amirping
Created December 2, 2017 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirping/547ec8e4f96da4b9388e4d7eac27707d to your computer and use it in GitHub Desktop.
Save amirping/547ec8e4f96da4b9388e4d7eac27707d to your computer and use it in GitHub Desktop.
Cesar Cryptot
<!DOCTYPE html>
<html ng-app="app">
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body ng-controller="main">
<h1>Hello Plunker!</h1>
<textarea ng-model="indata"></textarea>
<input type="text" ng-model="shift" value="0">
<button ng-click="crypte()">crypte</button>
<textarea ng-model="outdata"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="script.js"></script>
</body>
</html>
// Code goes here
angular.module('app', [])
.controller('main', function($scope) {
$scope.indata = "abc";
$scope.outdata = "";
$scope.shift = 1;
$scope.crypte = function() {
$scope.outdata = caesarShift($scope.indata,parseInt($scope.shift));
}
var caesarShift = function(str, amount) {
// Wrap the amount
if (amount < 0)
return caesarShift(str, amount + 26);
// Make an output variable
var output = '';
// Go through each character
for (var i = 0; i < str.length; i++) {
// Get the character we'll be appending
var c = str[i];
// If it's a letter...
if (c.match(/[a-z]/i)) {
// Get its code
var code = str.charCodeAt(i);
// Uppercase letters
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
// Lowercase letters
else if ((code >= 97) && (code <= 122))
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
}
// Append
output += c;
}
// All done!
return output;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment