0

I have this ternary operator

function digPow(n, p){
  return Number.isInteger((""+n).split("").map((num,index) => Math.pow(parseInt(num),(p+index))).reduce((a, b) => a + b, 0)/n) ? (""+n).split("").map((num,index) => Math.pow(parseInt(num),(p+index))).reduce((a, b) => a + b, 0)/n : -1;
}

As you can see this is a very long 1 liner. My question is, how do I recall the value inside the Number.isInteger() so that I don't have to repeat it again for the ternary operator in only 1 line.

This is the code I need a value from:-

    (""+n).split("").map((num,index) => Math.pow(parseInt(num),(p+index)))
      .reduce((a, b) => a + b, 0)/n 

Is there any syntax for this? I am relatively new to JS

EDIT: The main question is actually: "Is it possible to call the value from inside a ternary operator without using a variable"

EDIT-2: Sorry for my bad coding. Here is a simpler way to ask my question

const x = 6
const y = 3
console.log(Number.isInteger(x+y/3) ? x+y/3 : -1)

Is it possible to recall the x+y/3 value without repeating it or making a new variable?

Sarun UK
  • 6,210
  • 7
  • 23
  • 48
Squish
  • 419
  • 4
  • 22
  • please add the result of all ... with some examples? – Nina Scholz Oct 29 '20 at 12:46
  • 1
    you can replace `Math.pow(3, 4)` with `3 ** 4` you can also replace `parseInt(num)` with `+num` in most cases. – GottZ Oct 29 '20 at 12:47
  • 1
    What is this code even trying to do here? – VLAZ Oct 29 '20 at 12:47
  • At a certain point, the things you gain from a one liner begin to detract from the readability of the logic. And readability and maintainability, imho, trump one liners every day – Taplar Oct 29 '20 at 12:48
  • No need for syntax, just store the repeated expression in a variable then use that –  Oct 29 '20 at 12:49
  • @Taplar while this is reasonable, sometimes code golfing is fine too. rarely, but occasionally – GottZ Oct 29 '20 at 12:51
  • The reason is I finished this challange right here https://www.codewars.com/kata/5552101f47fc5178b1000050 , but I was wondering if there is a simpler way to call a value from inside a ternary operator @VLAZ – Squish Oct 29 '20 at 12:51
  • 1
    Just because you can do something on one line, doesn't mean you should. [Any fool can write code that a computer can understand. Good programmers write code that humans can understand.](https://en.wikiquote.org/wiki/Martin_Fowler) – Liam Oct 29 '20 at 12:51
  • i wonder why everyone is expressing what should not be done. after all clean code is not in question here. – GottZ Oct 29 '20 at 12:53
  • Because @GottZ this is a common mistake made by new programmers. Do you not think it's helpful to point this out? – Liam Oct 29 '20 at 12:54
  • @GottZ I disagree - OP is already suffering from poor code and asks how to make it better. But does it [in a bad way](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – VLAZ Oct 29 '20 at 12:57
  • Ok, I started trying to sketch out an answer. But that line is so difficult to read I struggled. I tried to refactor it out, but it's just a mess. I've been a professional developer for 15+ years and I'm having a hard time making sense of this...That's not a good thing.... throw this away and start again but this time **don't** try and do it in one line. In fact here's an exercise for you. Do it in such a way that it is the **most easily read**. – Liam Oct 29 '20 at 13:01
  • neither of us knows the background of this question. to me, this question lacks clarity and quality wich prevents me from even attempting to write an answer. if this is clearly about code golfing (wich we don't know) then saying "this is bad" is not helping at all while expressing interest in the root of this question is. i'm not blaming you for this at all. it's fine. i'm just wondering how that's relevant even though we don't know the reason for this question in the first place. i'll cast a close vote now. – GottZ Oct 29 '20 at 13:04
  • There's a site for code golf https://codegolf.stackexchange.com/ – Liam Oct 29 '20 at 13:06
  • 1
    alot of devs don't know about meta or other branches of SO and post their questions here. i'd say we should not judge too fast :D – GottZ Oct 29 '20 at 13:07
  • 1
    I am sorry everyone who is confused because of my question, please look at edit 2 for a clearer question. I am sorry... – Squish Oct 29 '20 at 13:08
  • @GottZ is the 2nd edit clearer ? – Squish Oct 29 '20 at 13:11
  • i'd suggest rewriting the whole question from scratch tho. your question is more likely solvable by these tho: https://stackoverflow.com/questions/60524232/trick-to-call-a-function-inside-tenary-expression-and-use-its-return-value-witho – GottZ Oct 29 '20 at 13:18

1 Answers1

2

Not exactly sure, what your function is doing or what arguments it's supposed to take, but you could use a Self-Executing Anonymous Function aka IIFE (Immediately Invoked Function Expression).

Let's start by formatting what you currently have:

const digPow = (n, p) => Number.isInteger(
  ("" + n)
  .split("")
  .map((num, index) => Math.pow(parseInt(num), (p + index)))
  .reduce((a, b) => a + b, 0) / n
)
  ? ("" + n)
    .split("")
    .map((num, index) => Math.pow(parseInt(num), (p + index)))
    .reduce((a, b) => a + b, 0) / n
  : -1;
    
console.log(digPow(6, 3)); // 36

It looks like this part is inside the condition and also a return if the result is an integer:

("" + n)
  .split("")
  .map((num, index) => Math.pow(parseInt(num), (p + index)))
  .reduce((a, b) => a + b, 0) / n

You can reduce your logic to the following (pseudo-code):

const digPow = (x => Number.isInteger(x) ? x : -1)(split/map/reduce/divide);

Let's pass that into an IIFE:

const digPow = (n, p) => (
  (value) => Number.isInteger(value) ? value : -1)
  (("" + n)
    .split("")
    .map((num, index) => Math.pow(parseInt(num), (p + index)))
    .reduce((a, b) => a + b, 0) / n);
    
console.log(digPow(6, 3)); // 36
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132