Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   почему не работает скрипт? (https://javascript.ru/forum/misc/78525-pochemu-ne-rabotaet-skript.html)

m~r.Nemo 25.09.2019 19:46

почему не работает скрипт?
 
function monthTable(month, year) {
  const monthNames = [
    'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 
    'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'
  ];

  let calendar =  { 0: ['Вс'], 1: ['Пн'], 2: ['Вт'], 3: ['Ср'], 4: ['Чт'], 5: ['Пт'], 6: ['Сб'] };

  const now = new Date();
  const m = month ? month - 1 : now.getMonth();
  const y = year ? year : now.getFullYear();

  console.log(`${monthNames[m]}, ${y}`);
  console.log();

  const days = new Date(y, m + 1, 0).getDate();

  let week = 1;
  let start = new Date(y, m, 1).getDay();
  for (let i = 0; i < days; i++) {
    if (start % 7 === 1 && i > 0) {
      week++;
    }
    if (calendar[start % 7].length !== week) {
      calendar[start % 7].push('  ');
    }
    if (i < 9) {
      calendar[start % 7].push(` ${i + 1}`);
    } else {
      calendar[start % 7].push(i + 1);
    }
    start++;
  }

  for (let i = 1; i < 8; i++) {
    console.log(calendar[i % 7].join(' '));
  }
}

monthTable(5, 2015);
console.log();
monthTable(2, 2020);
console.log();
monthTable();

рони 25.09.2019 20:07

m~r.Nemo,
работает.

рони 25.09.2019 20:13

m~r.Nemo,
function monthTable(month, year) {
  const monthNames = [
    'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь',
    'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'
  ];

  let calendar =  { 0: ['Вс'], 1: ['Пн'], 2: ['Вт'], 3: ['Ср'], 4: ['Чт'], 5: ['Пт'], 6: ['Сб'] };

  const now = new Date();
  const m = month ? month - 1 : now.getMonth();
  const y = year ? year : now.getFullYear();



  const days = new Date(y, m + 1, 0).getDate();

  let week = 1;
  let start = new Date(y, m, 1).getDay();
  for (let i = 0; i < days; i++) {
    if (start % 7 === 1 && i > 0) {
      week++;
    }
    if (calendar[start % 7].length !== week) {
      calendar[start % 7].push('  ');
    }
    if (i < 9) {
      calendar[start % 7].push(` ${i + 1}`);
    } else {
      calendar[start % 7].push(i + 1);
    }
    start++;
  }
  let txt = `${monthNames[m]}, ${y}`;
  for (let i = 1; i < 8; i++) {
    txt += `\n ${calendar[i % 7].join(' ')}`;
  }
  return  txt
}

console.log(monthTable(5, 2015));
console.log(monthTable(2, 2020));
console.log(monthTable());

m~r.Nemo 25.09.2019 20:44

рони,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<script>
function monthTable(month, year) {
  const monthNames = [
    'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь',
    'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'
  ];

  let calendar =  { 0: ['Вс'], 1: ['Пн'], 2: ['Вт'], 3: ['Ср'], 4: ['Чт'], 5: ['Пт'], 6: ['Сб'] };

  const now = new Date();
  const m = month ? month - 1 : now.getMonth();
  const y = year ? year : now.getFullYear();



  const days = new Date(y, m + 1, 0).getDate();

  let week = 1;
  let start = new Date(y, m, 1).getDay();
  for (let i = 0; i < days; i++) {
    if (start % 7 === 1 && i > 0) {
      week++;
    }
    if (calendar[start % 7].length !== week) {
      calendar[start % 7].push('  ');
    }
    if (i < 9) {
      calendar[start % 7].push(` ${i + 1}`);
    } else {
      calendar[start % 7].push(i + 1);
    }
    start++;
  }
  let txt = `${monthNames[m]}, ${y}`;
  for (let i = 1; i < 8; i++) {
    txt += `n ${calendar[i % 7].join(' ')}`;
  }
  return  txt
}

console.log(monthTable(5, 2015));
console.log(monthTable(2, 2020));
console.log(monthTable());
</script>
</body>
</html>

рони 25.09.2019 20:53

m~r.Nemo,
в чём проблема?

m~r.Nemo 25.09.2019 20:58

рони,
почему я вижу пустую страницу?

рони 25.09.2019 21:00

Цитата:

Сообщение от m~r.Nemo
почему я вижу пустую страницу?

потому что вы ничего на страницу не добавили.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<script>
function monthTable(month, year) {
    const monthNames = [
        'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь',
        'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'
    ];

    let calendar =  { 0: ['Вс'], 1: ['Пн'], 2: ['Вт'], 3: ['Ср'], 4: ['Чт'], 5: ['Пт'], 6: ['Сб'] };

    const now = new Date();
    const m = month ? month - 1 : now.getMonth();
    const y = year ? year : now.getFullYear();



    const days = new Date(y, m + 1, 0).getDate();

    let week = 1;
    let start = new Date(y, m, 1).getDay();
    for (let i = 0; i < days; i++) {
        if (start % 7 === 1 && i > 0) {
            week++;
        }
        if (calendar[start % 7].length !== week) {
            calendar[start % 7].push('  ');
        }
        if (i < 9) {
            calendar[start % 7].push(` ${i + 1}`);
        } else {
            calendar[start % 7].push(i + 1);
        }
        start++;
    }
    let txt = `${monthNames[m]}, ${y}`;
    for (let i = 1; i < 8; i++) {
        txt += `<br> ${calendar[i % 7].join(' ')}`;
    }
    return  txt
}
document.body.insertAdjacentHTML('beforeend', monthTable(5, 2015)+'<br>')
document.body.insertAdjacentHTML('beforeend', monthTable(2, 2020)+'<br>');
document.body.insertAdjacentHTML('beforeend', monthTable());
</script>
</body>
</html>

рони 25.09.2019 21:01

m~r.Nemo,
Изменение документа

m~r.Nemo 25.09.2019 21:16

рони,
всё равно страница белая как снег, может из-за браузера?
Mozilla/5.0 (Linux; Android 4.4.2; HUAWEI Y360-U61 Build/HUAWEIY360-U61) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.109 Mobile Safari/537.36 ?

рони 25.09.2019 21:35

Цитата:

Сообщение от m~r.Nemo
может из-за браузера?

может.


Часовой пояс GMT +3, время: 12:42.