-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-example.js
More file actions
35 lines (31 loc) · 1.04 KB
/
app-example.js
File metadata and controls
35 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//A Javascript Program to illustrate Caesar Cipher Technique
// Encrypts text using a shift od s
function encrypt(text, s)
{
let result=""
for (let i = 0; i < text.length; i++)
{
let char = text[i];
if (char.toUpperCase(text[i]))
{
let ch = String.fromCharCode((char.charCodeAt(0) + s-65) % 26 + 65);
result += ch;
}
else
{
let ch = String.fromCharCode((char.charCodeAt(0) + s-97) % 26 + 97);
result += ch;
}
}
return result;
}
// Driver code
let text = "ATTACKATONCE";
let s = 4;
console.log(`text: ${text}`)
console.log(`shift: ${s}`)
console.log(`encrypt: ${encrypt(text, s)}`)
// document.write("Text : " + text + "<br>");
// document.write("Shift : " + s + "<br>");
// document.write("Cipher: " + encrypt(text, s) + "<br>");
// This code is contributed by avanitrachhadiya2155