将对象(字典)和字符串(句子)作为函数参数,并从足够的键中获取 return 值
Take object ( dictionary) and string (sentence) as function parameters and return values from adequete keys
我必须创建一个函数,将字典(对象)和句子作为参数,returns 足够的键值,如果字典函数中缺少单词,应该抛出错误“错误:缺失值”
这些是输出示例:
翻译({
“杰”:“我”,
“suis”:“我”,
“神父”:“父亲”,
"ton": "your"}, "je suis ton pere"
) // 'I am your father'
翻译({
“的”:“勒”,
“可爱”:“牛排”,
“你的”:“吨”,
“狗”:“简”,
"is": "est"}, "这只狗很可爱"
) // 'le chien est mignon'
翻译({
“的”:“勒”,
“可爱”:“牛排”,
“你的”:“吨”,
“狗”:“简”,
"is": "est"}, "这只狗毛茸茸的"
) // 'Error: missing value'
我的代码是这样的:它可以工作,但它在索引 0 处停止,所以我只能得到第一个结果,即“I”
let dictionary = {
"je": "I",
"suis": "am",
"pere": "father",
"ton": "your"
}
let dictKeys = Object.keys(dictionary)
let translated = []
for(let i=0; i < sentence.length; i++){
for(let j=0; j < dictKeys.length; i++){
if(sentence[i] == dictKeys[j]){
translated.push(dictionary[dictKeys[j]])
console.log(translated)
}
}
}
我不知道如何完成这个练习,请帮助我
您的代码几乎没问题。在你的第二个循环中,你再次递增 i 而不是 j。我添加了一个 toLowerCase 来覆盖区分大小写的单词
let dictionary = {
"je": "I",
"suis": "am",
"pere": "father",
"ton": "your"
}
let dictKeys = Object.keys(dictionary)
let translated = []
let missing = []
let sentence = "Je suis ton ami abc gfukgv".split(" ")
for (let i = 0; i < sentence.length; i++) {
if (!dictKeys.includes(sentence[i].toLowerCase())) {
missing.push(sentence[i])
//console.error(`"${sentence[i]}" word is missing from dictionary`)
}
for (let j = 0; j < dictKeys.length; j++) {
if (sentence[i].toLowerCase() == dictKeys[j].toLowerCase()) {
translated.push(dictionary[dictKeys[j]])
}
}
}
missing.length > 0 ? console.error(`The following words are missing from the dictionary: ${missing.join(", ")}`) : null
console.log(translated.join(" "))
我必须创建一个函数,将字典(对象)和句子作为参数,returns 足够的键值,如果字典函数中缺少单词,应该抛出错误“错误:缺失值”
这些是输出示例:
翻译({ “杰”:“我”, “suis”:“我”, “神父”:“父亲”, "ton": "your"}, "je suis ton pere" ) // 'I am your father'
翻译({ “的”:“勒”, “可爱”:“牛排”, “你的”:“吨”, “狗”:“简”, "is": "est"}, "这只狗很可爱" ) // 'le chien est mignon'
翻译({ “的”:“勒”, “可爱”:“牛排”, “你的”:“吨”, “狗”:“简”, "is": "est"}, "这只狗毛茸茸的" ) // 'Error: missing value'
我的代码是这样的:它可以工作,但它在索引 0 处停止,所以我只能得到第一个结果,即“I”
let dictionary = {
"je": "I",
"suis": "am",
"pere": "father",
"ton": "your"
}
let dictKeys = Object.keys(dictionary)
let translated = []
for(let i=0; i < sentence.length; i++){
for(let j=0; j < dictKeys.length; i++){
if(sentence[i] == dictKeys[j]){
translated.push(dictionary[dictKeys[j]])
console.log(translated)
}
}
}
我不知道如何完成这个练习,请帮助我
您的代码几乎没问题。在你的第二个循环中,你再次递增 i 而不是 j。我添加了一个 toLowerCase 来覆盖区分大小写的单词
let dictionary = {
"je": "I",
"suis": "am",
"pere": "father",
"ton": "your"
}
let dictKeys = Object.keys(dictionary)
let translated = []
let missing = []
let sentence = "Je suis ton ami abc gfukgv".split(" ")
for (let i = 0; i < sentence.length; i++) {
if (!dictKeys.includes(sentence[i].toLowerCase())) {
missing.push(sentence[i])
//console.error(`"${sentence[i]}" word is missing from dictionary`)
}
for (let j = 0; j < dictKeys.length; j++) {
if (sentence[i].toLowerCase() == dictKeys[j].toLowerCase()) {
translated.push(dictionary[dictKeys[j]])
}
}
}
missing.length > 0 ? console.error(`The following words are missing from the dictionary: ${missing.join(", ")}`) : null
console.log(translated.join(" "))