/* Modify any methods that you can to support chaining. Ideally, the methods to add and remove classes should be chainable.*/
const $ = (function () {
// Create Constructor function here const Constructor = function (selector) { this.elements = document.querySelectorAll(selector); };
// Immutable copy of the matching elements Constructor.prototype.items = function () { returnArray.prototype.slice.call(this.elements); };
// Get first item Constructor.prototype.first = function () { returnthis.elements[0]; }
// Get last item Constructor.prototype.last = function () { returnthis.elements[this.elements.length - 1]; }
// Add class to element Constructor.prototype.addClass = function (newClass) { this.items().forEach(element => { element.classList.add(newClass); }) returnthis; };
// Remove class from element Constructor.prototype.removeClass = function (newClass) { this.items().forEach(element => { element.classList.remove(newClass); }) returnthis; };
return Constructor; })();
// Create new instance const btns = new $('.button')
// Check the console logs in dev tools console.log('$.items()', btns.items()); console.log('$.first()', btns.first()); console.log('$.last()', btns.last());
// Add and remove class btns.addClass('btn-purple').removeClass('btn-blue');