garageScript / curriculum

GarageScript Curriculum
21 stars 164 forks source link

Update 5.js #331

Closed greencarlos closed 3 years ago

greencarlos commented 4 years ago

These tests DO NOT do what the challenge is asking. If you read the challenge description for js3/5.js then it:

  1. Takes in two objects: obj1 and obj2
  2. Then if the keys match the function value of obj2 is invoked with the value of obj1 as a parameter.
  3. Otherwise, the values of obj1 stay the same

Here are some examples:

const case1 = solution ({ "name": "drake", "age": "33", "power": 'finessing', "color": "platinum" }, { "name": (e) => { return e + "kendrick" }, "power": (e) => { return "motivating" + e } });

// will return : const result1 = { "name": "drakekendrick", "age": "33", "power": "motivatingfinessing", "color": "platinum" }

const case2 = solution({ "add2": "5", "sub2": "10", "div2": "6", "mult2": "4" }, { "add2": (num) => { return num + " + 2"}, "mult2": (num) => { return "2 * " + num} })

const result2 = { "add2": "5 + 2", "sub2": "10", "div2": "6", "mult2": "2 * 4" }

const case3 = solution({}, { 'white': () => { return 'walkers' } }) const result3 = {}

const case4 = solution({ 'name': 'pikachu', 'age': '59', 'power': 10, 'color': 'red' }, { 'name': (e) => { return 'Raichu' }, 'power': (num) => { return num * num } })

const result4 = { 'name': 'Raichu', 'age': '59', 'power': 100, 'color': 'red' }

const case5 = solution({ 'name': 'khaleesi', 'age': 25, 'power': 'mother of dragons', 'weakness': 'john snow' }, { 'weakness': (e) => { return e + ' knows nothing' }, 'power': (e) => { return e + ' is fire proof' } })

const result5 = { 'name': 'khaleesi', 'age': 25, 'power': 'mother of dragons is fire proof', 'weakness': 'john snow knows nothing' }

greencarlos commented 4 years ago
/* globals describe expect it */
const solution = require('../5').solution;

describe('call function in 2nd object (if possible) using the corresponding values in 1st object as function params, return new object', () => {
  it('should return if object is empty ', () => {
    const result = solution({}, {});
    expect(result).toEqual({});
  });

  it('should return {}', () => {
    const result = solution(
      {},
      {
        white: () => {
          return 'walkers';
        },
      },
    );
    expect(result).toEqual({});
  });

  it ('should return new object', () => {
    const result = solution({
      add2: 5,
      sub2: 10,
      div2: 6,
      mult2: 4
        }, {
      add2: (num) => { return num + " + 2 = 7"},
      mult2: (num) => { return "2 * " + num + " = 8"}
      })

    expect(result).toEqual({
      add2: "5 + 2 = 7",
      sub2: 10,
      div2: 6,
      mult2: "2 * 4 = 8"
    })
})

  it('should return new object', () => {
    const result = solution(
      {
        name: 'pikachu', age: '59', power: 10, color: 'red'},
      {
        name: (e) => {
          return e + ' evolves into Raichu';
        },
        power: (num) => {return num * num}
      },
    );
    expect(result).toEqual({
      name: 'pikachu evolves into Raichu',
      age: '59',
      power: 100,
      color: 'red',
    });
  });

  it('should return ', () => {
    const result = solution(
      {
        name: 'khaleesi',
        age: 25,
        power: 'mother of dragons',
        weakness: 'john snow',
      },
      {
        weakness: e => {
          return e + ' who knows nothing';
        },
        power: e => {
          return e + ' is fireproof';
        },
      },
    );
    expect(result).toEqual({
      name: 'khaleesi',
      age: 25,
      power: 'mother of dragons is fireproof',
      weakness: 'john snow who knows nothing',
    });
  });
});

Solution that passes the test

const solution = (obj1, obj2, i=0) => {
  const keys = Object.keys(obj1)
    return keys.reduce((acc, curr) => {
    if (obj2[curr]) {
      acc[curr] = obj2[curr](obj1[curr])
    } else {
      acc[curr] = obj1[curr]
    }
    return acc
  }, {})
}
greencarlos commented 3 years ago

This issue has been fixed