Home > Mobile >  how to shorten js probabilities for functions
how to shorten js probabilities for functions

Time:01-01

i have a js code wanna make it more simple in less steps it working but have many steps in detail it's function show 3 paramters but if log them in the console them have no sorting for exp function a(b,c,d){} it show console.log(a(d,b,c)) there is the code what it the best practise for this function

function show(name="mostafa",age=0,status=true){
    let result;
    let status1= (status == true || age == true || name == true) 
    ? `you are available for hiring`
    : `you are not available for hiring`;
    result= 
    (typeof name == 'string' && typeof age == 'number' && typeof status == 'boolean') // abc
    ?`welcome ${name} your age is ${age} ,${status1}`
    
    :(typeof name == 'string' && typeof status == 'number' && typeof age == 'boolean') // acb 
    ?`welcome ${name} your age is ${status} ,${status1}`
    
    :(typeof age == 'string' && typeof name == 'number' && typeof status == 'boolean') // bac
    ?`welcome ${age} your age is ${name} ,${status1}`
    
    :(typeof age == 'string' && typeof status == 'number' && typeof name == 'boolean') // bca
    ?`welcome ${age} your age is ${status} ,${status1}`
    
    :(typeof status == 'string' && typeof name == 'number' && typeof age == 'boolean') // cab
    ?`welcome ${status} your age is ${name} ,${status1}`
    
    :(typeof status == 'string' && typeof age == 'number' && typeof name == 'boolean') // cba
    ?`welcome ${status} your age is ${age} ,${status1}`
    :`damn !!`
    return result
}
console.log(show(true,25,'mostafa'))
function show(name="mostafa",age=0,status=true){
    let result;
    let status1= (status == true || age == true || name == true) 
    ? `you are available for hiring`
    : `you are not available for hiring`;
    result= 
    (typeof name == 'string' && typeof age == 'number' && typeof status == 'boolean') // abc
    ?`welcome ${name} your age is ${age} ,${status1}`
    
    :(typeof name == 'string' && typeof status == 'number' && typeof age == 'boolean') // acb 
    ?`welcome ${name} your age is ${status} ,${status1}`
    
    :(typeof age == 'string' && typeof name == 'number' && typeof status == 'boolean') // bac
    ?`welcome ${age} your age is ${name} ,${status1}`
    
    :(typeof age == 'string' && typeof status == 'number' && typeof name == 'boolean') // bca
    ?`welcome ${age} your age is ${status} ,${status1}`
    
    :(typeof status == 'string' && typeof name == 'number' && typeof age == 'boolean') // cab
    ?`welcome ${status} your age is ${name} ,${status1}`
    
    :(typeof status == 'string' && typeof age == 'number' && typeof name == 'boolean') // cba
    ?`welcome ${status} your age is ${age} ,${status1}`
    :`damn !!`
    return result
}
console.log(show(true,25,'mostafa'))

CodePudding user response:

The best maintainable way would be to pass an object instead, whose destructured properties default to the desired defaults.

function show({ name="mostafa",age=0,status=true } = {}){
  return `welcome ${name} your age is ${age}, you are ${status ? '' : 'not '}available for hiring`;
};
console.log(show());
console.log(show({ status: false }));
console.log(show({ name: 'bob', status: true }));

If you're not allowed to do that, I suppose you could collect all arguments into an array, and .find each one with the correct typeof. Default arguments don't work in combination with when the position of each argument is unknown; you'll have to move that functionality outside the parameter list.

function show(...args) {
  const name = args.find(arg => typeof arg === 'string') ?? "mostafa";
  const age = args.find(arg => typeof arg === 'number') ?? 0;
  const status = args.find(arg => typeof arg === 'boolean') ?? true;
  return `welcome ${name} your age is ${age}, you are ${status ? '' : 'not '}available for hiring`;
}

console.log(show());
console.log(show(false));
console.log(show('bob', true));

CodePudding user response:

actually is very simple, compared to your code :)

just use an object as a parameter:

  • the order doesn't matter by default,
  • no extra logic is needed (for me is the best suitable in this case)

One Line Code:

function show(obj) {
  return `welcome ${obj.name} your age is ${obj.age} ,${obj.status}`;
}

what is a Object?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Tests:

function show(obj) {
  return `welcome ${obj.name} your age is ${obj.age} , you are ${obj.status ? '' : 'not'} available for hiring`;
}

/* TESTS */

// name, age, status
console.log(
  show({
    name: "mostafa",
    age: 15,
    status: true,
  })
);

// age, name, status
console.log(
  show({
    age: 20,
    name: "ahmed",
    status: true,
  })
);

// status, name, age
console.log(
  show({
    status: true,
    name: "ali",
    age: 10
  })
);

// the combinations can go for ever

  • Related