Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 5.js #331

Closed
wants to merge 1 commit into from
Closed

Update 5.js #331

wants to merge 1 commit into from

Conversation

greencarlos
Copy link
Contributor

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'
}

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' 
}
@@ -14,12 +14,12 @@ describe('call function in 2nd object (if possible) using the corresponding valu
})

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you are changing the tests, could you help me format the object so its easier to read? Right now its all in one line and it is difficult to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just showing an example. I believe Paul already wrote the appropriate tests for this challenge.

Copy link
Contributor

@songz songz Jan 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I closed his PR so I can merge in yours (this one). Please make this PR the best PR you can make so we can merge this in.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

solution({
  name: 'ffgff',
  age: '1213'
  ...
})

@songz songz mentioned this pull request Jan 9, 2020
@greencarlos greencarlos reopened this Jan 16, 2020
@greencarlos
Copy link
Contributor Author

/* 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
Copy link
Contributor Author

This issue has been fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants