NoroffFEU / yeetsheet

1 stars 4 forks source link

Implement eval() function #30

Closed petternikolai closed 3 months ago

petternikolai commented 3 months ago

Contact Kristian Hagesaeter before starting on this task. You are going to create a sub branch of Kristian's branch to avoid conflict and repetision.

Take a look at those concepts:

-Web Workers - new Worker('worker.js'); -Limiting scope while doing eval (see example below)

function safeEval(code, context = {}) {
    const contextKeys = Object.keys(context);
    const contextValues = contextKeys.map(key => context[key]);
    const wrappedCode = `
        (function(${contextKeys.join(', ')}) {
            return eval(${JSON.stringify(code)});
        })(...arguments)
    `;
    try {
        return (new Function(wrappedCode))(...contextValues);
    } catch (error) {
        return `Error: ${error.message}`;
    }
}
// Example usage
const result = safeEval('a + b', { a: 1, b: 2 });
petternikolai commented 3 months ago

I am not sure what I'm expected to do here and I've tried working it out but without any luck.

WeronikaMartinsen commented 3 months ago

https://www.loom.com/share/1a926c64a00949de944b463f5ca48b5d?sid=03c88b3d-3bbd-4def-9a1d-7b53eabe0f4d

Here is walk througth the Eval() function made by Oliver. Please, take a look.