The problem with this code is its giving the output in alphabetic order even when the input is in reverse.. Any suggestions? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The problem with this code is its giving the output in alphabetic order even when the input is in reverse.. Any suggestions?

function fun(str){ var arr=['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','a','b','c','d','e','f','g','h','i','j','k','l','m','n']; var ptr=str.split(""); var abc=""; for(var i=0;i<arr.length;i++){ for(var j=0;j<ptr.length;j++){ if(ptr[j]===arr[i]){ abc=abc + arr[i+13]; } } } return abc; } console.log(fun("abc")); console.log(fun("cab"));

14th May 2021, 9:03 AM
dheeraj gupta
dheeraj gupta - avatar
17 Answers
+ 1
Then try that: function rot13(str) { var alp = "abcdefghijklmnopqrstuvwxyz"; str = str.split(""); var rot = ""; for(let c of str) { if(!alp.includes(c)) { rot += c; continue; } let i = alp.indexOf(c); i += 13; i %= 26; rot += alp[i]; } return rot; }
14th May 2021, 9:29 AM
Nico Ruder
Nico Ruder - avatar
+ 1
Nico Ruder your code is fine, but you doesn't help the OP by not pointing him its logical error and not correcting its own code ^^ dheeraj gupta your logic is wrong: you must first iterate over the string to be converted, then for each char iterate over your array to find it and append it converted (then you should exit the loop to avoid finding it twice) ;) also, you must keep track of not founded char and if so append it without converting: var abc = ""; var found; for (var j=0; j<ptr.length; j++) { found = false; for (var i=0; i<arr.length; i++) { if (ptr[j]===arr[i]) { abc = abc + arr[i+13]; found = true; break; } } // handle not founded characters if (!found) { abc = abc + ptr[j]; } } there are some improvements you could do also (wich could shortening the code), but that's not the question ^^
14th May 2021, 6:26 PM
visph
visph - avatar
+ 1
visph thats true, but its better to give a more efficient solution and helping this way, then using inefficient code. On the end I can still explain what I did and show how it to solve his problem easier and on the end he knew what he wanted and just needed a path to solve it. I think you also asked for a solution on stackoverflow, where the solution was just posted and no one cared.
14th May 2021, 6:40 PM
Nico Ruder
Nico Ruder - avatar
+ 1
i %= 26; // i = i % 26 (i = 36 % 26 = 10) And now we take the character of array with the index 10. c = arr[i];
19th May 2021, 1:32 AM
Nico Ruder
Nico Ruder - avatar
0
Do you try to make a rot13 function?
14th May 2021, 9:21 AM
Nico Ruder
Nico Ruder - avatar
14th May 2021, 9:22 AM
dheeraj gupta
dheeraj gupta - avatar
0
Nico Ruder but it's not working with spaces
14th May 2021, 9:42 AM
dheeraj gupta
dheeraj gupta - avatar
0
Nico Ruder thanks its working now
14th May 2021, 9:46 AM
dheeraj gupta
dheeraj gupta - avatar
0
No problem but check if it wraps around if you have xyz
14th May 2021, 9:47 AM
Nico Ruder
Nico Ruder - avatar
0
It's working fine until 'm'.. At 'n' it's undefined and then at 'o' it's 'a' but it should be 'b'
14th May 2021, 9:55 AM
dheeraj gupta
dheeraj gupta - avatar
0
Change 27 to 26 and it works
14th May 2021, 10:02 AM
Nico Ruder
Nico Ruder - avatar
0
Great... But I want spaces in the output too
14th May 2021, 10:05 AM
dheeraj gupta
dheeraj gupta - avatar
0
I edit it. Sorry for my mistakes
14th May 2021, 10:20 AM
Nico Ruder
Nico Ruder - avatar
0
Nico Ruder can u please explain how i%=26; is working there
18th May 2021, 8:46 PM
dheeraj gupta
dheeraj gupta - avatar
0
i %= 26 is the modulo operation on i itself. If you write any mathamatical or logical operation in front of an equal sign, then you calculate the i with that operation. As example: i += 5 means i = i + 5 or i -= 7 means i = i - 7 The modulo operation (mostly shown as a % sign) is just division with leaving the rest. Example: 16 % 6 = 4 because if you try to subtract 6 from 16 as many times as possible, then only 4 is left or 16 / 6 = 2 rest 4. Another example: 21 % 7 = 0 because 21 / 7 = 3 rest 0 Examples for the use of modulo: if you have a defined length of an array but you want to wrap around, if you go over the size, then you use modulo (%). here we have the alphabet with 26 characters. But what if i have a "x" and want to rotate it with ROT13, then you take the index of "x", which is 23 and add 13. i = array.indexOf("x"); // 23 i += 13; // i = i + 13 (i = 23 + 13 = 36) now we need to wrap around, because there is no 36th index in the array. So modulo comes to use.
19th May 2021, 1:19 AM
Nico Ruder
Nico Ruder - avatar
0
Nico Ruder thank you so much
19th May 2021, 8:36 AM
dheeraj gupta
dheeraj gupta - avatar
0
Hey, no problem
19th May 2021, 10:05 AM
Nico Ruder
Nico Ruder - avatar