Good afternoon, please help js jedi in solving the problem. There is such an array, the functions are specified in the prompt. I rewrote the code several times and now it does not load at all. I will be very grateful.

Arrays

<script type="text/javascript"> function ask() { var conf; do { list = { name: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ своС имя:"), sname: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ свою Ρ„Π°ΠΌΠΈΠ»ΠΈΡŽ:"), age: +prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ свой возраст:"), sex: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ свой ΠΏΠΎΠ»:"), codelang: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ языки програмирования ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²Ρ‹ Π·Π½Π°Π΅Ρ‚Π΅:"), }; list.push(list); cl.push(list.codelang) conf = confirm("ΠŸΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠΈΡ‚ΡŒ?"); } while (conf != false); } function say() { var i; for (i = 4; i < list.length; i++) { document.write("Имя: " + list[i].name + "<br />"); document.write("Ѐамилия: " + list[i].sname + "<br />"); document.write("Возраст: " + list[i].age + "<br />"); document.write("Пол: " + list[i].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[i] + "<br />"); document.write("<hr/>"); }; } function deli() { var index = +prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π½ΠΎΠΌΠ΅Ρ€ индСкса ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Ρ‹ Ρ…ΠΎΡ‚Π΅Π»ΠΈ Π±Ρ‹ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ"); list.splice(index, 1); cl.splice(index, 1); } function deln() { var name = prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π²Ρ‹ Ρ…ΠΎΡ‚Π΅Π»ΠΈ Π±Ρ‹ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ"); for (i = 4; i < list.length; i++) { if (name === list[i].name ) { list.splice(i, 1) }; else if (name === list[1].name || name === list[2].name || name === list[3].name) break; } function readylist() { document.write("Имя: " + list[1].name + "<br />"); document.write("Ѐамилия: " + list[1].sname + "<br />"); document.write("Возраст: " + list[1].age + "<br />"); document.write("Пол: " + list[1].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[1] + "<br />"); document.write("<hr/>"); document.write("Имя: " + list[2].name + "<br />"); document.write("Ѐамилия: " + list[2].sname + "<br />"); document.write("Возраст: " + list[2].age + "<br />"); document.write("Пол: " + list[2].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[2] + "<br />"); document.write("<hr/>"); document.write("Имя: " + list[3].name + "<br />"); document.write("Ѐамилия: " + list[3].sname + "<br />"); document.write("Возраст: " + list[3].age + "<br />"); document.write("Пол: " + list[3].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[3] + "<br />"); document.write("<hr/>"); } function compare(a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; } var list = [{ name: '', sname: '', age:'', sex: '', codelang: '', },{ name: "Π”ΠΌΠΈΡ‚Ρ€ΠΈΠΉ", sname: "ВСрнадский", age: 30, sex: "ΠœΡƒΠΆΡΠΊΠΎΠΉ", codelang: ["html, css, php"], },{ name: "АндрСй", sname: "Π‘ΠΎΡ€ΠΎΠΊΠ°", age: 21, sex: "ΠœΡƒΠΆΡΠΊΠΎΠΉ", codelang: ["html, css, php"], },{ name: "Π’Π°Ρ‚ΡŒΡΠ½Π°", sname: "Π‘ΠΈΡ‚Π°Π»ΠΎ", age: 24, sex: "ЖСнский", codelang: ["html, css, php"], }]; cl[1] = list.codelang[1]; cl[2] = list.codelang[2]; cl[3] = list.codelang[3]; var func = prompt("МСню: 1 - Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° Π² ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ, 2 - ΠΏΠΎΡΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ вСсь список Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½Ρ‹Ρ…, 3.1 - ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ, 3.2 - ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΏΠΎ индСксу, 4.1 - сортировка массива ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ, 4.2 - сортировка массива ΠΏΠΎ количСству языков програмирования, 4.3 - сортировка массива ΠΏΠΎ возрасту, 5 - Π²Ρ‹Ρ…ΠΎΠ΄ с ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹"); switch (func) { case "1": { readylist(); ask(); say(); } break; case "2": { readylist(); say(); } break; case "3.1": { deln(); readylist(); } break; case "3.2": { deli(); readylist(); } break; case "4.1": { list.sort(compare); readylist(); } break; case "4.2": { cl.sort(function(a, b){ return b.length - a.length; }); readylist(); } break; case "4.3": { function compare(a, b) { return a.age - b.age; } list.sort(compare); readylist(); } break; case "5": { alert("Π”ΠΎ свидания!"); } } </script> 

  • 2
    If I were you, I would shorten the code to the minimum possible, that the sheet that is is unreal to understand - Sergey V.
  • You have 2 errors that are visible right in the browser. The first is in the if else block. After if (name === list [i] .name) {} is; which is not needed. The second - there is no closing bracket in the function at the end of the script - alexoander
  • From the help Questions on the topic that you shouldn't ask anyway: Search for errors in the code. Before asking β€œwhy is this (sheet of a thousand lines) code not working for me?”, Try to localize the problem yourself ... - Andrew B
  • I recommend to avoid using document.write , 2016 is the year of the court - andreymal

1 answer 1

 $(document).ready(function(){ var list = [{ name: '', sname: '', age: '', sex: '', codelang: '', }, { name: "Π”ΠΌΠΈΡ‚Ρ€ΠΈΠΉ", sname: "ВСрнадский", age: 30, sex: "ΠœΡƒΠΆΡΠΊΠΎΠΉ", codelang: ["html, css, php"], }, { name: "АндрСй", sname: "Π‘ΠΎΡ€ΠΎΠΊΠ°", age: 21, sex: "ΠœΡƒΠΆΡΠΊΠΎΠΉ", codelang: ["html, css, php"], }, { name: "Π’Π°Ρ‚ΡŒΡΠ½Π°", sname: "Π‘ΠΈΡ‚Π°Π»ΠΎ", age: 24, sex: "ЖСнский", codelang: ["html, css, php"], }]; cl[1] = list[1].codelang; cl[2] = list[2].codelang; cl[3] = list[3].codelang; var func = prompt("МСню: 1 - Π΄ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° Π² ΠΎΡ‡Π΅Ρ€Π΅Π΄ΡŒ, 2 - ΠΏΠΎΡΠΌΠΎΡ‚Ρ€Π΅Ρ‚ΡŒ вСсь список Π΄ΠΎΠ±Π°Π²Π»Π΅Π½Π½Ρ‹Ρ…, 3.1 - ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ, 3.2 - ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΏΠΎ индСксу, 4.1 - сортировка массива ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ, 4.2 - сортировка массива ΠΏΠΎ количСству языков програмирования, 4.3 - сортировка массива ΠΏΠΎ возрасту, 5 - Π²Ρ‹Ρ…ΠΎΠ΄ с ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹"); switch (func) { case "1": { readylist(); ask(); say(); } break; case "2": { readylist(); say(); } break; case "3.1": { deln(); readylist(); } break; case "3.2": { deli(); readylist(); } break; case "4.1": { list.sort(compare); readylist(); } break; case "4.2": { cl.sort(function(a, b) { return b.length - a.length; }); readylist(); } break; case "4.3": { function compare(a, b) { return a.age - b.age; } list.sort(compare); readylist(); } break; case "5": { alert("Π”ΠΎ свидания!"); } } }) var cl = []; function ask() { var conf; do { list = { name: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ своС имя:"), sname: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ свою Ρ„Π°ΠΌΠΈΠ»ΠΈΡŽ:"), age: +prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ свой возраст:"), sex: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ свой ΠΏΠΎΠ»:"), codelang: prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ языки програмирования ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²Ρ‹ Π·Π½Π°Π΅Ρ‚Π΅:"), }; list.push(list); cl.push(list.codelang) conf = confirm("ΠŸΡ€ΠΎΠ΄ΠΎΠ»ΠΆΠΈΡ‚ΡŒ?"); } while (conf != false); } function say() { var i; for (i = 4; i < list.length; i++) { document.write("Имя: " + list[i].name + "<br />"); document.write("Ѐамилия: " + list[i].sname + "<br />"); document.write("Возраст: " + list[i].age + "<br />"); document.write("Пол: " + list[i].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[i] + "<br />"); document.write("<hr/>"); }; } function deli() { var index = +prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Π½ΠΎΠΌΠ΅Ρ€ индСкса ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Ρ‹ Ρ…ΠΎΡ‚Π΅Π»ΠΈ Π±Ρ‹ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ"); list.splice(index, 1); cl.splice(index, 1); } function deln() { var name = prompt("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ имя Ρ‡Π΅Π»ΠΎΠ²Π΅ΠΊΠ° ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ Π²Ρ‹ Ρ…ΠΎΡ‚Π΅Π»ΠΈ Π±Ρ‹ ΡƒΠ΄Π°Π»ΠΈΡ‚ΡŒ"); for (i = 4; i < list.length; i++) { if (name === list[i].name) { list.splice(i, 1) } else if (name === list[1].name || name === list[2].name || name === list[3].name) break; } function readylist() { document.write("Имя: " + list[1].name + "<br />"); document.write("Ѐамилия: " + list[1].sname + "<br />"); document.write("Возраст: " + list[1].age + "<br />"); document.write("Пол: " + list[1].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[1] + "<br />"); document.write("<hr/>"); document.write("Имя: " + list[2].name + "<br />"); document.write("Ѐамилия: " + list[2].sname + "<br />"); document.write("Возраст: " + list[2].age + "<br />"); document.write("Пол: " + list[2].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[2] + "<br />"); document.write("<hr/>"); document.write("Имя: " + list[3].name + "<br />"); document.write("Ѐамилия: " + list[3].sname + "<br />"); document.write("Возраст: " + list[3].age + "<br />"); document.write("Пол: " + list[3].sex + "<br />"); document.write("Π―Π·Ρ‹ΠΊΠΈ програмирования: " + cl[3] + "<br />"); document.write("<hr/>"); } function compare(a, b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

2 Errors - After if (name === list [i] .name) {} costs; which is not needed. The second is that there is no closing bracket in the function at the end of the script.

UPD In the code there are a lot of minor errors that it would be useful to correct the author himself for gaining experience =)

  • It also doesn’t work - Andrew S.
  • This is an array that opens in a menu that offers several functions. var func = prompt ("Menu: 1 - add a person to the queue, 2 - view the entire list of added ones, 3.1 - delete a person by name, 3.2 - remove a person by index, 4.1 - sort an array by name, 4.2 - sort an array by number of languages programming, 4.3 - array sorting by age, 5 - exit from the program "); - Andrew S.
  • Updated code - see. You would read a little more about js and about working with arrays, because l [1] = list.codelang [1] is incorrect in your context because list is already an array. As an object, it does not have the codelang property that you defined on an array element . - alexoander
  • The second point is that the numbering in the array always starts from the 0th element. In your case, the elements by index 0 are always empty. - alexoander
  • one
    @AndrewS. Write here, if you have something else to change and you get stuck. I will try to help you. - alexoander