skip to Main Content
    function captureArguments(arg1, arg2, arg3) {
        if (
//first section
          (arg1 === null && arg2 === null && arg3 === null) ||
          arguments.length === 0
        ) {
          return "Nothing Passed";
        } else if (
          typeof arg1 === typeof arg2 &&
          typeof arg2 === typeof arg3
        ) {
          return "All the same";
        }
//second section
        if (arg1 !== null && arg2 === null && arg3 === null) {
          return alpha;
        }
        if (arg1 === null && arg2 !== null && arg3 === null) {
          return second;
        }
        if (arg1 === null && arg2 === null && arg3 !== null) {
          return gamma;
        }
//third section
        if (arg1 === "string" && arg2 === "string" && garg3 === "string") {
          return {
            first: arg1,
            second: arg2,
            third: arg3
        }
      }

My goal is to 1. check if an argument is being passed, 2. if ex captureArguments("first", null, null) then it return the value "first", 3. if all parameters are of string value for it to return it ex. captureArguments("ball", "hill", "water") returns {first: "ball", second "hill, third:"water}. Third section keeps returning undefined

2

Answers


  1. function captureArguments(arg1, arg2, arg3) {
        if (arguments.length === 0) {
            return "Nothing Passed";
        }
    
        if (arg1 === null && arg2 === null && arg3 === null) {
            return "All arguments are null";
        }
    
        if (typeof arg1 === "string" && typeof arg2 === "string" && typeof arg3 === "string") {
            return {
                first: arg1,
                second: arg2,
                third: arg3
            };
        }
    
        return arg1 || arg2 || arg3;
    }
    
    console.log(captureArguments()); // Output: "Nothing Passed"
    console.log(captureArguments("first", null, null)); // Output: "first"
    console.log(captureArguments("ball", "hill", "water")); // Output: {first: "ball", second: "hill", third: "water"}
    console.log(captureArguments(null, null, null)); // Output: "All arguments are null"
    console.log(captureArguments("apple", "banana", null)); // Output: "apple"
    
    Login or Signup to reply.
  2. It is not 100% clear what you want, but this code should contain all possible tests you need

    const captureArguments = (...args) => {
      // Check if no arguments were passed
      if (args.length === 0) return "Nothing Passed";
      if (args.every(arg => arg === null)) return `${args.length} null passed`;
    
      // Return non-null arguments if at least one is null
      const nonNullArgs = args.filter(arg => arg !== null);
      if (nonNullArgs.length < args.length) {
        return nonNullArgs;
      }
    
      // Check if all arguments are strings and handle up to 3 arguments dynamically
      if (args.every(arg => typeof arg === 'string')) {
        let result = {};
        if (args[0]) result.first = args[0];
        if (args[1]) result.second = args[1];
        if (args[2]) result.third = args[2];
        return result;
      }
    
      // If none of the above conditions match
      return "Other reasons";
    };
    
    console.log(captureArguments()); // Output: "Nothing Passed"
    console.log(captureArguments("first", null, null)); // Output: ["first"]
    console.log(captureArguments("ball", "hill", "water")); // Output: {first: "ball", second: "hill", third: "water"}
    console.log(captureArguments(null, null, null)); // Output: 3 null passed"
    console.log(captureArguments("apple", "banana", null)); // Output: ["apple","banana"]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search