2

I want this but in fewer lines using arrows

console.log([1, 2, 3, 4].every(
  value => {
    if (value > 0) {
      return 1
    }
  }))

output: true

It is working fine but when I tried to reduce the lines as below code it is not working is there any way to do it, is it not possible to write a single if statement without using any ternary

console.log([1, 2, 3, 4].every(value =>
  if (value > 0) 1)
);
output :  /tmp/main.js:1
console.log([1,2,3,4].every(value=> if(value>0) 1)) 
                                    ^^

SyntaxError: Unexpected token 'if'
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

[Program exited with exit code 1]

Is it possible to return anything apart from true or false using arrow operator like this? I want to return awesome but it is throwing me an error.

k=val=>val>0 "awesome";
console.log(k(2))
k=val=>val>0 "awesome";
             ^^^^^^^^^

SyntaxError: Unexpected string
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

I tried to ask this question which is based on arrow function but someone told me to ask the question separately, that's why I'm asking here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Indratej Reddy
  • 165
  • 1
  • 7
  • 4
    Just `value=> value>0`. You cannot use an `if` because it's a statement, an arrow function without a body needs only an expression. Also, no real reason to return `1` – VLAZ Apr 08 '21 at 11:43
  • Just use a [ternary operator](https://stackoverflow.com/questions/6259982/how-do-you-use-the-conditional-operator-in-javascript). – Dane Brouwer Apr 08 '21 at 11:46
  • 1
    @DaneBrouwer why? – VLAZ Apr 08 '21 at 11:48
  • @VLAZ Ah, misread the question. Saw the OP returned `1` so I assumed they wanted to return some sort of value from the condition. Should literally just be able to provide the condition as you suggested. – Dane Brouwer Apr 08 '21 at 11:51
  • @VLAZ thank you so much i got it value=> value>0 it will return true therefore every function directly take it as true suppose if i want return anything apart of 1 or 0 like value=>if val>1 "awesome" in that case how can i do it ?? without using {} it is possible? – Indratej Reddy Apr 08 '21 at 11:52
  • @IndratejReddy `value => value>1` – VLAZ Apr 08 '21 at 11:52
  • Check my answer. Happy coding – Rashed Rahat Apr 08 '21 at 11:54
  • what if i want to return any string it is possible when value>1 is it possible without using {} or ternary operator? – Indratej Reddy Apr 08 '21 at 11:54
  • `console.log([1, 2, 3, 4].filter(value => value > 0));` –  Apr 08 '21 at 12:20
  • @IndratejReddy You have to use `ternary operator`. Anyway, i have updated my answer where without using `{}` to get the result you want. – Rashed Rahat Apr 08 '21 at 13:16
  • Hi @IndratejReddy. Please check the answer and mark the question as answered if it so. Thanks! – CyberEternal Apr 22 '21 at 08:44

2 Answers2

1

You can do it in the following way

console.log([1, 2, 3, 4].every(value => value > 0))
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
CyberEternal
  • 2,259
  • 2
  • 12
  • 31
-1

console.log([1, 2, 3, 4].every(value => value > 0) ? "awesome" : null)
Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38
  • 1
    It's pointless to use a ternary operator in this case @RashedRahat – CyberEternal Apr 08 '21 at 11:51
  • what if i want to return a string like "awesome" when value>0 without using any ternary operator or {}, is it possible to do that? – Indratej Reddy Apr 08 '21 at 11:56
  • 1
    @IndratejReddy it is. The callback for every is (element, index, array). You could modify the array if you wanted to, but this is not what OP wanted. – Silviu Burcea Apr 08 '21 at 12:01
  • @CyberEternal just want to try because of my curiosity :) – Indratej Reddy Apr 08 '21 at 12:06
  • @SilviuBurcea is it possible to return anything apart from true or false using arrow operator like this i want to return awesome but it is throwing me an error k=val=>val>0 "awesome"; console.log(k(2)) k=val=>val>0 "awesome"; – Indratej Reddy Apr 08 '21 at 12:07
  • @IndratejReddy, Actually, you can do it by the following way `console.log([1, 2, 3, 4].every(value => value > 0 ? 'any' : null))`, but in your case It's pointless – CyberEternal Apr 08 '21 at 12:54
  • @IndratejReddy You have to use `ternary operator`. Anyway, i have updated my answer where without using `{}` to get the result you want. – Rashed Rahat Apr 08 '21 at 13:16
  • @IndratejReddy you can do that, as pointed out by CyberEternal, but you need to do `reduce` instead of `every` – Silviu Burcea Apr 08 '21 at 14:00