Skip to content

Instantly share code, notes, and snippets.

@Ephellon
Created June 10, 2024 23:34
Show Gist options
  • Save Ephellon/bfe147059b5620312265d9b1f429d849 to your computer and use it in GitHub Desktop.
Save Ephellon/bfe147059b5620312265d9b1f429d849 to your computer and use it in GitHub Desktop.
Do the following... Needs proper documentation (see JSDoc)
// ******** //
let total = 0;
// Write a function called `add` that does the following:
// 1) Takes an argument (`n`) and adds it to the `total`
// 2) Can be called using a running parenthesis chain (see examples)
// These should increase the total...
add(); // total = 1
add(1)(2)(3)(4)(5); // total = 16
add(-1)(+1); // total = 16
// ******** //
// Create a class called `Todo` that does the following:
// 1) Takes arguments: `name`, `date`, and `complete`
// a) `name` should be a string but, can be any type; coerce this into a string
// b) `date` **must** be a Date; if a date is not given, throw an error
// c) `complete` should be a boolean; coerce this into a boolean
// 2) Has two (2) static methods:
// a) `markAs` which takes 2 arguments: a Todo object and a completion status, then changes the Todo's status to the given one
// b) `remove` which takes 1 argument: a Todo object, then removes it
// 3) Has four (4) prototype methods:
// a) `markComplete` which marks the item complete
// b) `markIncomplete` whcih marks the item incomplete
// c) `dueDate` which changes the item's due date if a new date is given, otherwise it returns the due date
// d) `description` which adds a description to the item if a new one is given, otherwise it returns the description
let getHairCut = new Todo('Haircut', new Date('12-12-2024'), false);
gethairCut.description(); // undefined
gethairCut.description("Xmas haircut!"); // "Xmas haircut!"
gethairCut.description(); // "Xmas haircut!"
getHairCut.dueDate(); // Thursday, December 12, 2024
getHairCut.name; // "Haircut"
@Ephellon
Copy link
Author

@dskw1 — This

@dskw1
Copy link

dskw1 commented Jun 11, 2024

let total = 0;

/**
 *Adds the given number to the total and allows chaining.
 * @param {number} [n=1] - The number to add to the total. Defaults to 1 if not provided.
 * @returns {function} - Returns a function that can be called with another number to continue the chain.
 */

function add(n = 1) {
    total += n;
    return (m) => add(m);
}

@dskw1
Copy link

dskw1 commented Jun 11, 2024

class Todo {
    constructor(name, date, complete) {
        this.name = new String(name);

        if(!(date instanceof Date)) {
            throw new Error('Invalid date');
        }

        this.date = date;
        this.complete = new Boolean(complete);
        this._description = undefined;
    }

    static markAs(todo, status) {
        if(todo instanceof Todo)
            todo.complete = new Boolean(status);
    }

    static remove(todo) {
        if(todo instanceof Todo) {
            todo = null;
        }
    }

    markComplete() {
        this.complete = true;
    }

    markIncomplete() {
        this.complete = false;
    }

    dueDate(newDate) {
        if(newDate instanceof Date) {
            this.date = newDate;
        } else
            return this.date.toDateString();
        }
    }

    description(newDescription) {
        if (newDescription !== undefined) {
            this._description = new String(newDescription);
        } else {
            return this._description;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment