Why is this giving me an error? - javascript

Why is this code giving me the following error message?
TypeError: Object 97 has no method 'charCodeAt'
var str = "Caesar Cipher";
str = str.split("");
num = 2;
x = 0;
for (var i = 0; i < str.length; i++) {
x = 0;
while (x < num) {
if (str[i].charCodeAt(0) <= 122 && str[i].charCodeAt(0) >= 97) {
str[i] = str[i].charCodeAt()+x;
}
x++;
}
}
console.log(str);
Incase anyone was interested in my final result: http://jsfiddle.net/zackarylundquist/8L9b5/

The line:
str[i] = str[i].charCodeAt()+x;
Is converting the element in the array from a string to a number. A number doesn't have a charCodeAt() method, hence why you're getting the error. Use the following:
str[i] = str[i].charCodeAt()+x + "";
This will keep the element a string, and you will still be able to call the charCodeAt() method.

Couldn't help it, I know it doesn't solve the op's problem exactly, but here's a nice caesar implementation (fiddle: http://jsfiddle.net/z97HR/3/):
var str = ("CaesarCipher").toLowerCase(); // let's use only lowercase letters, no spaces
console.log(str);
var offset = 0;
var crypt = str.split("").map(function(letter){
var offsetLetter = 97 + (letter.charCodeAt(0) + offset - 97) % 26;
return String.fromCharCode(offsetLetter);
}).join("")
console.log(crypt);

Related

How to use any Higher order functions in JavaScript to get the desired output for following code?

I have input string = 'ABCDEFGHI' and N = 3. Now I want to get output as 'CFIBGAHEJD' by circularly rotating input string and using N value.
Explanation:
string = ABCDEFGHI
O/P = C
string = ABDEFGHI
O/P = CF
string = ABDEGHI
O/P = CFI
string = ABDEGH
O/P = CFIB
.
.
.
string = D
O/P = CFIBGAHEJ
string = ''
O/P = CFIBGAHEJD
I write code in JavaScript as below.
function alphabet(string,N){
string = string.split('');
newStr = "";
var count = 0;
var i = 0
while(string.length > 0){
count++;
if(count === N ){
newStr += string.splice(i,1)
count = 0;
i = i-1;
}
if( i === string.length-1){
i = -1;
}
if(string.length === 1){
newStr += string.splice(0,1)
}
i++
}
console.log(newStr);
}
alphabet("ABCDEFGHIJ",3)
alphabet("ABCDEFGHIJ",12)
alphabet("ABC",5)
Now, as a beginner I want to do this using higher-order functions in JavaScript. But i am not able to cracking it. Anyone can help me to get output using those functions. If we can't able use those functions for this type of problem just tell me the reason why we can't.
Thanks in Advance.
There's literally no reason to use higher-order functions - they take functions as arguments, and that isn't what you need here. If you really wanted to, you could do something like this:
function alphabet(string, numFunc) {
const N = numFunc();
string = string.split('');
newStr = "";
var count = 0;
var i = 0
while (string.length > 0) {
count++;
if (count === N) {
newStr += string.splice(i, 1)
count = 0;
i = i - 1;
}
if (i === string.length - 1) {
i = -1;
}
if (string.length === 1) {
newStr += string.splice(0, 1)
}
i++
}
console.log(newStr);
}
alphabet("ABCDEFGHIJ", () => 3);
alphabet("ABCDEFGHIJ", () => 12);
alphabet("ABC", () => 5);
But it's just overcomplicated - your original approach was best.

Why am I getting random undefined chars in my decryption output in my Vigenere Cipher algorithm?

I am working on my own Vigenere Cipher in JavaScript. I enjoy it. Anyway, the encryption and decryption is the same except decrypt() is '-' keyStr instead of '+' towards the bottom. The encryption works perfectly. But, for some reason, when decrypting, some of the chars come out as undefined randomly. I know the algorithm works for C++, Python, Java, and Swift. What is the error here?
I have tried printing the char indices in the alphabet array and the index values in decrypt() come out odd and I can't figure out why.
function ascii(x) {
return x.charCodeAt(0);
}
function decrypt() {
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var msgStr = "";
var keyTemp = "";
var keyStr = "";
var output = "";
var input = document.getElementById("inMsg").value;
var key = document.getElementById("key").value;
input = input.toUpperCase();
key = key.toUpperCase();
for(let i = 0; i < input.length; i++) {
for(let x = 0; x < alpha.length; x++) {
if (input[i] == alpha[x]) {
msgStr += alpha[x];
}
}
}
for(let i = 0; i < msgStr.length; i++) {
keyTemp += key[i % key.length]
}
for(let i = 0; i < keyTemp.length; i++) {
for(let x = 0; x < alpha.length; x++) {
if (keyTemp[i] == alpha[x]) {
keyStr += alpha[x];
}
}
}
for(let i = 0; i < msgStr.length; i++) {
let x = (ascii(msgStr[i]) - ascii(keyStr[i])) % 26;
output += alpha[x];
}
document.getElementById("outMsg").value = output;
}
The problem you are having is being caused by this line:
let x = (ascii(msgStr[i]) - ascii(keyStr[i])) % 26;
because
ascii(msgStr[i]) - ascii(keyStr[i])
can be negative.
The % operator isn't really a modulus operator in javascript its a remainder operator and it works a little differently.
From the link above, you should be able to do something more like this to get it to work:
let x = ((ascii(msgStr[i]) - ascii(keyStr[i])) % 26) + 26) % 26

Reverse String In Place Using for loop in JavaScript

var n = "reversestrings", k=3;
want to reverse string in chunk of 'k',
Answer would be : ver sre tse nir gs;
if Last word less then 'k' then don't need to reverse.
I am using below code but not getting expected answer.
var n = 'stringreverses', k = 3, str = '', s = '';
var c = 0;
for( var i=0; i<n.length; i++ ){
if( c<k ){
c++
str += n[i];
s=str.split('').reverse().join('');
}
else{
console.log("-" + s);
c=0;
}
}
First we need to split input to chunks with the same size (the last one can be smaller), next we reverse every chunk and concatenate at the end.
var input = "123456",
chunks = input.match(new RegExp('.{1,' + k + '}', 'g'));
var result = chunks.map(function(chunk) {
return chunk.split('').reverse().join('');
}).join('');
Homework or not, here is a good use case to start with strings.
Here is a C approach but you have more in Javascript.
In fact you want to reverse by chunk so deal with chunk. How to create a chunk of string ? a way is to use slice https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/String/slice
var str = "abcdef";
console.log(str.slice(0,2));
So you have an easy way to slice your string into chunk.
Then you have to iterate over it, there is no good way of doing it actually there is dozen but you could do it from backward to the beginning of the string:
for( i=str.length ; i>0 ; i -= k ){
// i will go from the end of your str to
// the beginning by step of k(=3) and you can use i - k and i
// to slice your string (as we see it before)
// you have to take care of the last part that could be less than
// 3
}
then you have to format the result, the most easy way to do that is to concatenate results into a string here it is :
var strRes = "";
strRes += "res 1";
strRes += "res 2";
console.log(strRes); // should screen "res 1res 2"
As it is homework, I wont make a jsfiddle, you have here all the pieces and it's up to you to build the puzzle.
hope that help
$(function() {
var n = 'reversestrings', k = 3;
var revString = "";
for (var i =0; i<=n.length; i++) {
if (i%k == 0) {
l = parseInt(k) + parseInt(i);
var strChunk = n.substring(i,l);
var innerStr = "";
for (var j =0; j<strChunk.length; j++) {
var opp = parseInt(strChunk.length) - parseInt(j) - 1;
innerStr = innerStr + strChunk.charAt(opp);
}
revString = revString + " "+innerStr;
}
}
alert(revString);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
My take on this. Pure JS without even built-in functions:
function reverseSubStr(str) {
var right = str.length - 1, reversedSubStr = '';
while(right >= 0) {
reversedSubStr += str[right];
right--;
}
return reversedSubStr;
}
function reverseStr(str) {
var initialStr = str, newstr = '', k = 3, substr = ''
for(var i = 1; i <= initialStr.length; i++) {
substr += initialStr[i - 1]; // form a substring
if(i % k == 0) { // once there are 3 symbols - reverse the substring
newstr += reverseSubStr(substr) + " "; // ... and add space
substr = ''; // then clean temp var
}
}
return newstr += substr; // add the remainder of the string - 'gs' - and return the result
}
var str = 'reversestrings';
console.log(reverseStr(str)); //ver sre tse nir gs
I like #Jozef 's approch but here is mine as well for those who are not much into Regex -
//Taking care of Tail Calling
function reverStrInChunk(str, k, r=''){
let index=0, revStr,
res = str.substring(index, k), remStr;
revStr = res.split("").reverse().join("");
remStr = str.substring(k, str.length);
r = r + revStr;
if(remStr.length>k){
return reverStrInChunk(remStr,k, r+" ");
}
else if(remStr.length<k) {
return r +" "+remStr;
}else{
return r +" "+ remStr.split("").reverse().join("");
}
}
var aStr = reverStrInChunk('reversestrings',3);//ver sre tse nir gs
console.log(aStr);

Javascript Unicode Conversion and Search

I'm wondering whether anyone has any insight on converting an array of character codes to Unicode characters, and searching them with a regex.
If you have
var a = [0,1,2,3]
you can use a loop to convert them into a string of the first four control characters in unicode.
However, if you then want to create a regex
"(X)+"
where X == the character code 3 converted to its Unicode equivalent, the searches never seem to work. If I check for the length of the string, it's correct, and .* returns all the characters in the string. But I'm having difficulties constructing a regex to search the string, when all I have to begin with is the character codes. Any advise?
Edit:
var a = [0,1,2,3,0x111];
str = "";
for(var i = 0; i < a.length; i++) {
str += String.fromCharCode(a[i]);
}
var r = [0x111]
var reg = ""
reg += "(";
for(var i = 0; i < r.length; i++) {
var hex = r[i].toString(16);
reg += "\\x" + hex;
}
reg += ")";
var res = str.match(RegExp(reg))[0];
Edit
//Working code:
var a = [0,1,2,3,0x111];
str = "";
for(var i = 0; i < a.length; i++) {
str += String.fromCharCode(a[i]);
}
var r = [3,0x111]
var reg = ""
reg += "(";
for(var i = 0; i < r.length; i++) {
var hex = r[i].toString(16);
reg += ((hex.length > 2) ? "\\u" : "\\x") + ("0000" + hex).slice((hex.length > 2) ? -4 : -2);
}
reg += ")";
var res = str.match(RegExp(reg))[0];
With changes to a few details, the example can be made to work.
Assuming that you are interested in printable Unicode characters in general, and not specifically the first four control characters, the test vector a for the string "hello" would be:
var a = [104, 101, 108, 108, 111]; // hello
If you want to match both 'l' characters:
var r = [108, 108]
When you construct your regular expression, the character code must be in hexadecimal:
reg += "\\x" + ("0" + r[i].toString(16)).slice(-2);
After that, you should see the results you expect.

Arrangement of two string charecters into one string using loop

I have two string which should be put together into one string. First string is a input value and second string is a pattern how the first string should look. Here is the example - Input string( var val ) - 9165678823 Patter string( var mask ) - (999)999-9999 Output string should look like( var startVal ) - (916)567-8823 I have tried working out and this is my code
var val = $(control).data("loadMaskValue"); // Input Value
var mask = $(control).attr("mask"); //Masking Pattern
var startVal = "";
var j = 0;
for (var i = 0; i < mask.length; i++) {
var c = mask.charAt(j);
if (c == '9' || c == 'X' || c == 'A') { //Checks the char is normal char
startVal += val.charAt(j);
}
else {
startVal += c; //Inserts the special char to string like ( ) -
startVal += val.charAt(j);
}
j = startVal.length;
}
The problem with this code is it misses one number in between. The result of this code is startValue - (965)688-2. PLease help me.
Here's a slightly simpler implementation:
var input = '9165678823';
var mask = '(999)999-9999';
var output = '';
var offset = 0;
for (var i = 0; i < mask.length; i++) {
var char = mask.charAt(i);
if ('9XA'.indexOf(char) != -1) {
output += input.charAt(i - offset);
} else {
output += mask.charAt(i);
offset += 1;
}
}
console.log(output);
​Make sure that input has been stripped of all whitespace at the beginning and end.
Demo: http://jsfiddle.net/qWtjk/
You can use a regular expression to take the elements out. Check the fiddle: http://jsfiddle.net/BuddhiP/9MmqS/
var str= '9165678823';
var regEx = new RegExp("(\d{3})(\d{3})(\d{4})");
var m = regEx.exec(str);
var res = '(' + m[1] + ')' + m[2] + '-' + m[3];
console.log(res);
result is (916)567-8823
UPDATE: How to make this work with a dynamic pattern. Check updated fiddle: http://jsfiddle.net/BuddhiP/9MmqS/
$(function() {
var str= '9165678823';
var regEx = new RegExp("(\\d{3})(\\d{3})(\\d{4})");
var m = regEx.exec(str);
var mask = "({0}){1}-{2}";
var res = mask.supplant(m.slice(1));
console.log(res);
});​
Using supplant method from here: http://javascript.crockford.com/remedial.html
Once you understand your regular expression, you can make this work with any pattern and mask.

Categories