I need to solve this school exercise but I don't know how. Here's the text:
I need to make a program that takes information from the user via prompt() dialog. The information that user needs to enter is his name and sex (eg. Marc, m). Based on that information, the program needs to write in alert() dialog the adjectives from the (already given) list that start with the same letter as the letters from the name.
For example: user's name is Marc, and his sex is male. Program needs to use adjectives from the male list (there are two lists with adjectives, for both male and female), and to write them like this in alert dialog:
Mad
Accurate
Reasoning
Calculative
If you read their first letters vertically, they say MARC.
I have adjectives for all the letters in alphabet, for both male and female. Keep in mind that variable names and adjectives are on my native language (Serbian), but it shouldn't be the problem, you will get the point and I will explain the code in the comments.
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);
}
After days of trying, I finally managed to solve this problem. I have to say that @D Lowther helped me with few lines, such as to store male and female adjectives in one object instead of two arrays and to split the entry from prompt on this way var [name, adjectiveGroup] = question.split(", ", 2)
. These were really useful.
You can check the program immediately by clicking on Run.
// in this object I stored all the adjectives, both male and female, because on my language they are various depending on the sex
var pridevi = {
m: ["атрактиван", "блесав", "циничан", "чудан", "ћопав", "духовит", "џангризав", "ђаволаст", "елоквентан", "фантастичан", "грозан", "халапљив", "имућан", "јак", "катастрофалан", "леп", "љубазан", "мудар", "наиван", "њањав", "отпоран", "посесиван", "разигран", "смешан", "шаљив", "толерантан", "уображен", "весео", "забринут", "жут"],
z: ["атрактивна", "блесава", "цинична", "чудна", "ћопава", "духовита", "џангризава", "ђаволаста", "елоквентна", "фантастична", "грозна", "халапљива", "имућна", "јака", "катастрофална", "лепа", "љубазна", "мудра", "наивна", "њањава", "отпорна", "посесивна", "разиграна", "смешна", "шаљива", "толерантна", "уображена", "весела", "забринута", "жута"]
}
// prompt form
var unos = prompt("Упишите име и пол. Нпр. Мирко, м.");
// here I separated the prompt entry into array of two values: name and sex
var [ime, pol] = unos.toLowerCase().split(", ", 2);
// here I stored my native language because that language is required in prompt entry
var cirilica = /[\u0400-\u04FF]/;
// here I later stored the final array
var ispis = [];
// I made a function that converts the first letter of all values in array to uppercase
function velikoSlovo(a) {
for (var i = 0; i < a.length; i++) {
a[i] = a[i][0].toUpperCase() + a[i].substr(1);
}
return a.join("\n");
}
// if user clicks Cancel, the notification in alert window shows up
if (unos === null) {
alert("Корисник је одустао.");
}
// here I tested various things: if the prompt entry is empty || if there's either name or sex missing || if it's not on my native language - the notification in alert window shows up
else if (unos.trim() === "" || (unos.split(", ").length < 2) || cirilica.test(unos) === false) {
alert("Нису унесени исправни подаци.");
}
// if there are other values for sex instead of "m/f" (male/female), the notification in alert window shows up
else if ((unos.split(", ", 2)[1] !== "м") && (unos.split(", ", 2)[1] !== "ж")) {
alert("Нису унесени исправни подаци.");
}
// if prompt entry is correct (eg. Marc, m), I tested if the sex is male "м" or female "ж". Based on that, I used loop to compare the first letter of all the adjectives in array with all the letters of the name and then stored all the adjectives that match in the final array that I initialized before. Finally, I showed the final result in alert window and used the function to capitalize each word in string
else {
if (pol === "м") {
for (var i = 0; i < ime.length; i++) {
for (var e = 0; e < pridevi.m.length; e++) {
if (ime.charAt(i) === pridevi.m[e].charAt(0)) {
ispis.push(pridevi.m[e]);
}
}
}
alert(velikoSlovo(ispis));
}
else if (pol === "ж") {
for (var i = 0; i < ime.length; i++) {
for (var e = 0; e < pridevi.z.length; e++) {
if (ime.charAt(i) === pridevi.z[e].charAt(0)) {
ispis.push(pridevi.z[e]);
}
}
}
alert(velikoSlovo(ispis));
}
}