Home > Back-end >  Return a string where every character is replaced with its corresponding value in the cipher object
Return a string where every character is replaced with its corresponding value in the cipher object

Time:12-27

I am having an issue replacing the value from an object with the keys matching the string letters.

Challenge: Create a function secretCipher that takes in an string(sentence) and an object(cipher). Return a string where every character is replaced with its cooresponding value in the cipher. If the character doesn't exist in the cipher, use the original character.

This is what I've written so far. My console.log's are showing what I need them to however my hang up appears to be somewhere when I try to identy the same corresponding keys to the sentence[i]. I have tried numerous methods like .includes, strictly equals etc.

I also have tried to use .replace to replace any sentence[i] with by value variable.

     function secretCipher(sentence, cipher){
  
      let result = '';
      let cyf = Object.keys(cipher)
      // console.log(cyf)
      for (let key in cipher){
        let value = cipher[key];
        // console.log(value)
      }
      // iterate through sentence, if char in string is stricktly equal to the key
      for (let i = 0; i < sentence.length; i  ) {
        // console.log(sentence[i])
        if (sentence[i].includes(cyf)){
      
          sentence[i] = sentence[i].replace(sentence[i], value)
        result  = sentence[i]
        } else {
          result  = sentence[i]
        }
    
      }

      return result;

    }



    //Uncomment the lines below to test your function:

    console.log(secretCipher("lqq me on flcebzzk" , { l : "a", q : "d", z: "o"})); //=> "add me on        facebook"
    console.log(secretCipher("where are you???" , { v : "l", '?' : "!"})) //=> "where are you!!!"
    console.log(secretCipher("twmce" , { m : "n", t : "d", w : "a"})); //=> "dance"



CodePudding user response:

You can iterate through the word more efficiently, but this is pretty simple:

 function secretCipher(sentence, cipher){
 return sentence.split('').map(letter => cipher[letter] ?? letter).join('');
}



//Uncomment the lines below to test your function:

console.log(secretCipher("lqq me on flcebzzk" , { l : "a", q : "d", z: "o"})); //=> "add me on        facebook"
console.log(secretCipher("where are you???" , { v : "l", '?' : "!"})) //=> "where are you!!!"
console.log(secretCipher("twmce" , { m : "n", t : "d", w : "a"})); //=> "dance"

If your browser does not know what ?? is, replace it with ||. It should work on all modern browsers according to MDN.

CodePudding user response:

  • let value = cipher[key]; does nothing because that value isn't referenced anywhere else (its scope ends after the loop iteration ends)
  • if (sentence[i].includes(cyf)) { doesn't make sense because sentence[i] is a string (character) and cyf is an array of keys. A string will not contain an array. Check if the array contains the character instead.
  • sentence[i] = sentence[i].replace(sentence[i], value) won't work because strings are immutable. Create a new string instead.
  • When a match is found, adding the original sentence[i] doesn't make sense - you want to replace with the value on the object, not with the original character.

function secretCipher(sentence, cipher) {
  let result = '';
  const keys = Object.keys(cipher);
  // iterate through sentence, if char in string is stricktly equal to the key
  for (let i = 0; i < sentence.length; i  ) {
    if (keys.includes(sentence[i])) {
      result  = cipher[sentence[i]];
    } else {
      result  = sentence[i]
    }
  }
  return result;
}

console.log(secretCipher("lqq me on flcebzzk", {
  l: "a",
  q: "d",
  z: "o"
})); //=> "add me on        facebook"
console.log(secretCipher("where are you???", {
  v: "l",
  '?': "!"
})) //=> "where are you!!!"
console.log(secretCipher("twmce", {
  m: "n",
  t: "d",
  w: "a"
})); //=> "dance"

or, more concisely

function secretCipher(sentence, cipher) {
  let result = '';
  for (const char of sentence) {
    result  = cipher[char] ?? char;
  }
  return result;
}

console.log(secretCipher("lqq me on flcebzzk", {
  l: "a",
  q: "d",
  z: "o"
})); //=> "add me on        facebook"
console.log(secretCipher("where are you???", {
  v: "l",
  '?': "!"
})) //=> "where are you!!!"
console.log(secretCipher("twmce", {
  m: "n",
  t: "d",
  w: "a"
})); //=> "dance"

  • Related