Ami Hasan
Ami Hasan

Reputation: 125

Find the correct word by incrementing letters in alphabet

Hi I came acroos this challenge from jschallenger page. I write the following code and It's working Fine unless any capital letter is passed. It seems to me ltr = "abcdefghijklmnopqrstuvwxyz" is not the right way. Could you please tell how can I improve my code?

function myf(a){
 let ltr = "abcdefghijklmnopqrstuvwxyz"
 let newstr =""

for(i=0; i<=a.split("").length-1; i++){
    let str = ltr.indexOf(a.charAt(i))+1
    
    let mystr = ltr.charAt(str )
    newstr += mystr
    }

 console.log(newstr)
}

myf("bnchm")  // result: coding
myf("bgddrd") // result: cheese
myf("sdrshmf")// result:testing

Upvotes: 1

Views: 606

Answers (7)

the
the

Reputation: 11

     // remember capitals
      var obj={};
      for(let itr=0;itr<arg.toString().length;itr++){
       if(arg[itr]==arg[itr].toUpperCase()){
          obj[itr]=true // isUpperCase
       }
        // toLowerCase
         for(let iter=0;iter<String(arg).toLowerCase().split('');iter++){
       // your code
       }

The remembered capitals are used to restore uppercase letters before logging the result.

Upvotes: 1

Mr.Developer
Mr.Developer

Reputation: 545

Hope this code will help you.

const convert = (str) => {
  let result = "";
  for (const char of str) {
    result += String.fromCharCode(
      char.charCodeAt(0) + 1 === 123
        ? 97
        : char.charCodeAt(0) + 1 === 91
        ? 65
        : char.charCodeAt(0) + 1
    );
  }
  console.log(result);
};

convert("XYZ");
convert("bgddrz");
convert("bnchmf");

Upvotes: 1

Peter Pointer
Peter Pointer

Reputation: 4170

Correcting typo

You simply have a typo in your code. ltr.charAt(nxtltr) gives the error "nxtltr is not defined". If you replace it with str the decoding seems to work correctly.

Below is a snippet of it working:

function myf(a){
  let ltr = "abcdefghijklmnopqrstuvwxyz"
  let newstr = ""

  for(i=0; i<=a.split("").length-1; i++){
    let str = ltr.indexOf(a.charAt(i))+1
    let mystr = ltr.charAt(str)
    newstr += mystr
  }

  console.log(newstr)
}

myf("bnchm")  // result: coding
myf("bgddrd") // result: cheese
myf("sdrshmf")// result:testing

When developing it is useful to look at the browser's developer console (usually with F12 or right-click the website and select Inspect Element or similar.

Upvotes: 0

Mina
Mina

Reputation: 17189

You can use charCodeAt to get the code of the letter, you can increment by one, to get the code of the next letter, and you can use fromCharCode to get the character, This will work for capital and small characters.

function myf(a){
 let newstr =""

for(i=0; i<=a.split("").length-1; i++){
    let str = a[i];
    let code = str.charCodeAt()
    // if the letter equal to z make it a
    let nextCode = code === 122 ? 97 : code === 90 ? 65 : code + 1
    let nextCharacter = String.fromCharCode(nextCode)
    newstr += nextCharacter
}

 console.log(newstr)
}

myf("bnchmf")  // result: coding
myf("BNCHMF")  // result: CODING
myf("bgddrd") // result: cheese
myf("sdrshmf")// result:testing
myf("zmfkd") // result: angle

Upvotes: 1

SNikhill
SNikhill

Reputation: 504

You have not provided the question but, it looks like a Caesar Cipher.

You have a string that only contains lowercased letters. You will not be able to match uppercased letters against this string unless you perform a case conversion by:

  • Using toLowerCase method
  • Using ASCII char code trick and incrementing char code of the uppercased letter by 32 to get the corresponding lower cased letter

A good way will be to always perform a check in a single case. That is, irrespective of the user input, you convert the input string to lowercase or uppercase and then proceed with deciphering it.

Upvotes: 0

Filip Hanes
Filip Hanes

Reputation: 805

I don't know the exact challange, but would suggest this:

  1. you can add uppercase alphabet in ltr
  2. I suppose that after letter "z" should be letter "a"
  3. don't need to split a in each loop
function myf(a){
  const alphabet = "abcdefghijklmnopqrstuvwxyzaABCDEFGHIJKLMNOPQRSTUVWXYZA"
  let newstr = ""

  for(i = 0; i < a.length; i++){
    let nxt = alphabet.indexOf(a.charAt(i)) + 1
    newstr += alphabet.charAt(nxt)
  }

  console.log(newstr)
}

myf("bnchm")  // result: coding
myf("bgddrd") // result: cheese
myf("sdrshmf")// result:testing

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could calculate the letter by parsing a character, get the value in a 36 numbers system and add one. To ge the wanted value without carry take the rest of 36 and if zero add ten to get an 'a' (this was a 'z' before).

function convert(string) {
    let result = '';
    for (const c of string) {
        result += (((parseInt(c, 36) + 1)  % 36) || 10).toString(36);
    }
    return result;
}

console.log(convert("bnchmf"));   // coding
console.log(convert("bgddrd"));  // cheese
console.log(convert("sdrshmf")); // testing
console.log(convert("abcdefghijklmnopqrstuvwxyz")); // bcdefghijklmnopqrstuvwxyza

Upvotes: 4

Related Questions