Search code examples
javascriptcolorsmappingemotion

can't figure out how to code this mapping emotion and color


Hello I try to map colors and emotions together, each post has an initial emotion and color, but while responding the color and emotion changes by the user's choice, for example: the initial emotion of the post is sad and blue, and the user chooses happy and green, by combining the four variables Come out a new result of emotion and color, I tried to do it through if ?, but I encountered a problem, the manual mapping did not include additional possibilities, so does anyone have an idea how to do a more effective mapping that comes I can give a different result each time, here an example for what i done

this is the main function

const {angry} = require('./emotions/angry/angry')


exports.ECBrige = (userEmotion, userColor,postEmotion, postColor) => {
  if(userEmotion === 'angry'){
    return angry(userEmotion, userColor,postEmotion, postColor)
  }   
}

this is the mapping angry

const {yellow} = require('./colors/yellow');
const {lime} = require('./colors/lime');
const {green} = require('./colors/green');
const {aqua} = require('./colors/aqua');
const {blue} = require('./colors/blue');
const {pink} = require('./colors/pink');
const {red} = require('./colors/red');
const {orange} = require('./colors/orange');

exports.angry = (userEmotion, userColor,postEmotion, postColor) => {
    if(userColor === 'yellow'){
        return yellow(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'lime'){
        return lime(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'green'){
        return green(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'aqua'){
        return aqua(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'blue'){
        return blue(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'pink'){
        return pink(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'red'){
        return red(userEmotion, userColor,postEmotion, postColor)
    }else{
        return orange(userEmotion, userColor,postEmotion, postColor)
    }
}

it output undefined for some


Solution

  • You may use object to store colors, then you access your color function like color[colorName].

    ./colors/index.js - export your all colors

    const {yellow} = require('./colors/yellow');
    const {lime} = require('./colors/lime');
    const {green} = require('./colors/green');
    const {aqua} = require('./colors/aqua');
    const {blue} = require('./colors/blue');
    const {pink} = require('./colors/pink');
    const {red} = require('./colors/red');
    const {orange} = require('./colors/orange');
    
    export { yellow, lime, green, aqua, ... }
    

    ./mapping.js - access property of color object above.

    // import your all colors
    const colors = require('./colors');
    
    exports.angry = (userEmotion, userColor,postEmotion, postColor) => {
        try {
            return colors[userColor](userEmotion, userColor,postEmotion, postColor);
        } catch (err) {
            // you may choose some color default
            return colors.orange(userEmotion, userColor,postEmotion, postColor);
        }
    }