skip to Main Content

I seem to not be to pass all the tests:

function profileActivation(profile, reason) {
  if (reason === "confirm status manually") {
    profile.active = true;
  }

  if (profile.active) {
    if (profile.reason) {
      return "confirm status manually";
    } else {
      delete profile.reason;
      return profile;
    }
  } else {
    if (!profile.reason) {
      return "confirm status manually";
    } else {
      profile.active = true;
      return profile;
    }
  }
}

// 🧠 profileActivation takes two arguments, profile and reason, which are an object and a string respectively
//  The reason arg is only used -and therefore required- to deactivate profiles, not to activate them
//  The profile arg has an `active` property holding a Boolean value
//    * If profile is active, profileActivation deactivates it and writes a `reason` prop holding the reason
//    * If profile is inactive, profileActivation activates it and deletes the `reason` prop from the object
//    * In both cases the updated profile object is returned
// 🧠 Edge cases:
//    * If the profile is inactive but its `reason` prop is **missing**, return the string "confirm status manually"
//    * If the profile is active but it has a `reason` prop, return the string "confirm status manually"
//    * If the profile is **missing** an `active` prop, return the string "impossible to ascertain status"
// 🌟 HINT: to confirm the existence of a prop, check that its value is not undefined

Only passing 3 and four

2

Answers


  1. Chosen as BEST ANSWER
    function profileActivation(profile, reason) {
      if (profile.active === undefined && profile.reason === undefined) {
    return "impossible to ascertain status";
    }
    if (profile.active && profile.reason !== undefined) {
    return "confirm status manually";
    }
    if (!profile.active) {
    profile.active = true;
    profile.reason = reason;
    } else {
    profile.active = false;
    }
    if (!profile.active) {
    profile.active = true;
    } else {
    delete profile.reason;
    }
    

  2. Not sure why you are using the reason argument in the first line of the function – the description of the function never mentions using the reason to activate a profile!

    Your code also never returns "impossible to ascertain status", so it’s like you completely ignored that edge case

    The way I would do it is to handle the edge cases first, and then handle activation/de-activation.

    Anyway, here’s the code I came up with

    function profileActivation(profile, reason) {
        // handle edge cases
        // edge case 1 and 2
        if (profile.active === (profile.reason !== undefined)) {
            return "confirm status manually";
        }
        // edge case 3
        if (profile.active === undefined) {
            return "impossible to ascertain status";
        }
        // toggle activation
        profile.active = !profile.active;
        // add/remove reason as required
        if (!profile.active) {
            profile.reason = reason;
        } else {
            delete profile.reason;
        }
        return profile;
    }
    console.log('Test 1', profileActivation({}));
    console.log('Test 2', profileActivation({"active":true,"reason":""}));
    console.log('Test 5', profileActivation({"active":true},"because"));
    console.log('Test 6', profileActivation({"active":false,"reason":"because"}));

    Seems to do what is expected

    Re comment that it doesn’t work – well, the following is your code and it fails exactly like you claim mine fails – i.e. you claim the following failures

    Test 1 fails: profileActivation({}) should return "impossible to ascertain status" but returns "confirm status manually"
    Test 2 fails: profileActivation({"active":true,"reason":""}) should return "confirm status manually" but returns {"active":true}
    Test 5 fails: profileActivation({"active":true},"because") should return {"active":false,"reason":"because") but returns {"active":true}
    Test 6 fails: profileActivation({"active":false,"reason":"because"}) should return {"active":true} but returns {"active":true,"reason":"because"}
    

    perhaps you forgot to use my code?

    function profileActivation(profile, reason) {
      if (reason === "confirm status manually") {
        profile.active = true;
      }
    
      if (profile.active) {
        if (profile.reason) {
          return "confirm status manually";
        } else {
          delete profile.reason;
          return profile;
        }
      } else {
        if (!profile.reason) {
          return "confirm status manually";
        } else {
          profile.active = true;
          return profile;
        }
      }
    }
    console.log('Test 1', profileActivation({}));
    console.log('Test 2', profileActivation({"active":true,"reason":""}));
    console.log('Test 5', profileActivation({"active":true},"because"));
    console.log('Test 6', profileActivation({"active":false,"reason":"because"}));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search