javascript 返回一个字符串,其中每个字符都被替换为cipher对象中相应的值

hof1towb  于 2022-12-28  发布在  Java
关注(0)|答案(2)|浏览(94)

我在用与字符串字母匹配的键替换对象中的值时遇到了问题。
Challenge:创建一个函数secretCipher,该函数接收一个字符串(句子)和一个对象(密码)。返回一个字符串,其中每个字符都被替换为密码中对应的值。如果该字符在密码中不存在,则使用原始字符。
这是我到目前为止写的。我的console.log的显示我需要他们,但我的挂断似乎是在某处,当我试图识别相同的对应键的sentence[i]。我已经尝试了许多方法,如.includes,严格等于等。
我还尝试使用.replace将任何sentence[i]替换为by value变量。

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"
qjp7pelc

qjp7pelc1#

您可以更有效地迭代单词,但这非常简单:

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"

如果您的浏览器不知道??是什么,请将其替换为||。根据MDN,它应该可以在所有现代浏览器上工作。

wd2eg0qa

wd2eg0qa2#

  • let value = cipher[key];什么也不做,因为value没有在其他任何地方被引用(它的作用域在循环迭代结束后结束)
  • if (sentence[i].includes(cyf)) {没有意义,因为sentence[i]是字符串(字符),而cyf是键数组。字符串不包含数组。请检查数组是否包含字符。
  • sentence[i] = sentence[i].replace(sentence[i], value)不起作用,因为字符串是不可变的。请创建一个新字符串。
  • 当找到匹配时,添加原始的sentence[i]没有意义-您希望用对象上的值替换,而不是用原始字符替换。
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"

或者更准确地说

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"

相关问题