Normalizzazione di un array in un calendario in JavaScript
Nelle lezioni precedenti dovresti aver creato
un array di numeri da 1 all'ultimo giorno del mese:
let arr = range(getLastDay(year, month));
Inoltre, hai il numero del giorno della settimana per il primo giorno del mese e per l'ultimo:
let firstWeekDay = getFirstWeekDay(year, month);
let lastWeekDay = getLastWeekDay(year, month);
Ora completiamo il nostro array con stringhe
vuote a destra e a sinistra. Supponiamo di avere una funzione
normalize che accetta come primo parametro
l'array, come secondo - quante stringhe vuote aggiungere a sinistra,
e come terzo - quante stringhe vuote a destra:
function normalize(arr, left, right) {
}
Ricordo che a sinistra dobbiamo aggiungere firstWeekDay
elementi vuoti, e a destra - 6 meno
lastWeekDay elementi. Cioè useremo
la nostra funzione normalize
in questo modo:
let res = normalize(arr, firstWeekDay, 6 - lastWeekDay);
console.log(res);
Implementa la funzione normalize come descritto
e verifica il suo funzionamento.