gpt4 book ai didi

JavaScript 学校练习 - 提示和数组

转载 作者:行者123 更新时间:2023-11-30 20:09:33 34 4
gpt4 key购买 nike

我需要解决这个学校练习,但我不知道如何解决。正文如下:

我需要制作一个程序,通过 prompt() 对话框从用户那里获取信息。用户需要输入的信息是他的姓名和性别(例如 Marc,m)。基于这些信息,程序需要在 alert() 对话框中写入(已经给出的)列表中的形容词,这些形容词的开头字母与姓名中的字母相同。

例如:用户姓名为Marc,性别为男。程序需要使用男性列表中的形容词(有两个形容词列表,男性和女性),并在警告对话框中这样写:

疯狂

准确

推理

计算

如果您垂直阅读他们的第一个字母,他们会说 MARC。

我对字母表中的所有字母都有形容词,包括男性和女性。请记住,变量名和形容词是我的母语(塞尔维亚语),但这应该不是问题,您会明白我的意思,我会在评论中解释代码。

var pridevi = {
m: ["atraktivan", "blesav", "ciničan", "čudan", "ćopav", "duhovit", "džangrizav", "đavolast", "elokventan", "fantastičan", "grozan", "halapljiv", "imućan", "jak", "katastrofalan", "lep", "ljubazan", "mudar", "naivan", "njanjav", "otporan", "posesivan", "razigran", "smešan", "šaljiv", "tolerantan", "uobražen", "veseo", "zabrinut", "žut"],
z: ["atraktivna", "blesava", "cinična", "čudna", "ćopava", "duhovita", "džangrizava", "đavolasta", "elokventna", "fantastična", "grozna", "halapljiva", "imućna", "jaka", "katastrofalna", "lepa", "ljubazna", "mudra", "naivna", "njanjava", "otporna", "posesivna", "razigrana", "smešna", "šaljiva", "tolerantna", "uobražena", "vesela", "zabrinuta", "žuta"],
} // I stored adjectives in object where property m: stands for male and property f: stands for female adjectives

var unos = prompt("Upišite ime i pol. Npr. Mirko, m"); // prompt format

var ime = unos.toLowerCase().split(", ").shift(); // in this variable I stored name
var pol = unos.toLowerCase().split(", ").pop(); // in this variable I stored sex

// console.log(ime + " " + pol) > mirko m

if (unos === null) {
alert("Korisnik je odustao."); // if user clicks cancel, this message shows in alert dialog
}
else if (unos === undefined && ime < 0 && pol < 0) {
alert("Nisu uneseni ispravni podaci."); // if user doesn't write the data in correct form, this message shows in alert dialog
}
else {
var odgovor = pridevi[pol].find(opis => ime[0] === opis[0]); // here's the main thing that doesn't work as it should. it only shows the adjective of the first letter of the name, but not all of them
alert(odgovor);
}

最佳答案

这个答案依赖于模式匹配,它相当脆弱,因为如果有人不遵循定义的格式,它将无法正常工作。一些注意事项:

  • 将形容词组移动到持有对象中,因为访问动态属性相当简单,并且需要更少的 if/else 链接才能到达正确的数组组。
  • 定义了一些实用的箭头函数。如果这些不熟悉,请随意使用标准函数尝试它们,如下所示:
    • 函数 notEmpty(val) {
      return (val !== undefined && val !== null && val.length > 0);
      }
  • 用过Array.prototype.map使用回调重新格式化结果数组
  • 使用数组解构语法,您可以通过在获取映射结果后定义这些变量来替换它 (const name = mappedArray[0])
  • 在 JS 中,您不需要在访问其索引之前拆分字符串,因此 'string'[0] 会为您提供 's',这在 Array.prototype.find 中很有用回调函数

// adjective group
const adjectives = {
f: ["accurate", "bodybuilder", "calculative", "decisive" ],
m: ["abstract", "beautiful", "capable", "delightful" ],
}

// utility function to check that a value exists and isn't empty
const notEmpty = val => (val !== undefined && val !== null && val.length > 0);

// prompt response holder (define a format)
const question = prompt("Write your given name and sex (format: Marc, m.)");

// function to format each entry in the response
// /\W/g is a regex for stripping non alphanumeric
const formatEntries = val => val.replace(/\W/g, '').toLowerCase();

// use format to split and handle your response
const [name, adjectiveGroup] = question.split(", ", 2)
.map(formatEntries)

// if both variables are set then try and find matching adjective
if (notEmpty(name) && notEmpty(adjectiveGroup)) {
const answer = adjectives[adjectiveGroup].find(adj => name[0] === adj[0])

console.log(answer);
}

关于JavaScript 学校练习 - 提示和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52544880/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com