hw-3.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // *************1*************
  2. var person = [
  3. a = {surname: 'A', name: 'aa', fathername: 'Aa', country: 'Ukraine',},
  4. b = {surname: 'B', name: 'bb', country: 'Polsha',},
  5. c = {surname: 'C', name: 'cc', country: 'Vengria',}
  6. ];
  7. // *************2*************
  8. for (var items of person) {
  9. console.log(items);
  10. }
  11. // *************3*************
  12. for (var i = 0; i < person.length; i++) {
  13. console.log('name: ' + person[i].name);
  14. console.log('surname: ' + person[i].surname);
  15. }
  16. // *************4*************
  17. for (var i = 0; i < person.length; i++) {
  18. if ('fathername' in person[i]) {
  19. person[i].fullName = prompt('enter full name',)
  20. }
  21. }
  22. console.log(person);
  23. // *************5*************
  24. let json = JSON.stringify(person);
  25. console.log(json);
  26. // *************6*************
  27. let d = '{"surname":"D","name":"dd"}';
  28. d = JSON.parse(d);
  29. person.push(d);
  30. console.log(person);
  31. // *************7*************
  32. var str = "<table border='1'><tr><td>Name</td><td>Surname</td></tr>";
  33. for (let i = 0; i < person.length; i++) {
  34. str += ' <tr><td>${person[i].name}</td><td>${person[i].surname}</td></tr>'
  35. }
  36. str += "</table>";
  37. console.log(str);
  38. document.write(str);
  39. // *************8*************
  40. function avg2(a, b) {
  41. if (isNaN(a) || isNaN(b)) {
  42. console.log('there must be numbers');
  43. }
  44. else {
  45. return ((a + b) / 2);
  46. }
  47. }
  48. console.log(avg2(4, 6));
  49. // *************9*************
  50. function sum(a, b, c) {
  51. if (isNaN(a)) {
  52. return b + c
  53. }
  54. if (isNaN(b)) {
  55. return a + c
  56. }
  57. else if (isNaN(c)) {
  58. return a + b
  59. }
  60. else if (isNaN(b) && isNaN(c) && isNaN(a)) {
  61. return 'error';
  62. }
  63. else return a + b + c;
  64. }
  65. console.log(sum(1, 'fdrhzrht', 5));
  66. // *************10*************
  67. function intRandom(a, b) {
  68. if (b === undefined) {
  69. b = a;
  70. a = 0;
  71. }
  72. a = Math.ceil(a);
  73. b = Math.floor(b);
  74. return Math.round(Math.random() * (b - a + 1)) + a;
  75. }
  76. console.log(intRandom(5));
  77. // *************11*************
  78. function sum(...arguments) // параметры собираются в массив params
  79. {
  80. var sum = 0;
  81. for (let i = 0; i < arguments.length; i++) {
  82. sum += arguments[i];
  83. }
  84. return sum;
  85. }
  86. console.log(sum(3, 45, 7, 3, 4, 5, 6, 7));