1

General JavaScript question here. It wasn't an easily googleable question (in my opinion) so I figured I'd ask it here to humans, and if it gets flagged as a duplicate then that's okay. It dawned on me while I was writing this function that there must be a way to write this in a way that doesn't rely on a temporary variable.

const isHex = num => {
  let result = true;
  [...num].map(n => { if (isNaN(parseInt(n,16))) result = false; });
  return result;
};

How would you inline a function like this and get rid of result? I feel like this is probably a gap in my JavaScript knowledge and I'm curious to know the answer. Maybe it's painfully obvious and this is a silly question. Idk.

8
  • 4
    You could use .some() instead of .map(). It's not clear what you mean by the term "inline" here; usually that refers to what a language runtime might do to avoid making a function call. Commented Mar 20, 2019 at 13:22
  • Or reduce (although some is the right tool for the job here. reduce could be used if you wanted to reinvent the wheel). Commented Mar 20, 2019 at 13:23
  • 1
    [...num] is num a string? If it's an array, why would you use the spread operator on it? Commented Mar 20, 2019 at 13:26
  • Yea, I do. But the name num is misleading, then. Commented Mar 20, 2019 at 13:42
  • @Cerbrus it's supposed to be a string. Commented Mar 20, 2019 at 13:46

2 Answers 2

2

You could use Array#every and return early.

const isHex = num => [...num].every(n => !isNaN(parseInt(n, 16)));
    
console.log(isHex('1a')); // true
console.log(isHex('1#')); // false

Same with Array#some.

const isHex = num => ![...num].some(n => isNaN(parseInt(n, 16)));
    
console.log(isHex('1a')); // true
console.log(isHex('1#')); // false

Sign up to request clarification or add additional context in comments.

4 Comments

This assumes num is an array of strings, which isn't exactly clear in the question. The code in the question suggests num isn't even an array.
No, it doesn't make sense. That's my point. The only logical input for the OP's code is a string of numeric characters, which is weird and requires clarification.
come on. op takes a spreadable object, this is not an array, because if so, it is superfluous to spred it into an array and do no make any mutation on it. even the (wrong) use of map shows that the values should not be changed. the only valid data structure, which is spreadable and not an array or an array like (which would not contain simple characters) is a string. at least the singular num suggests a single number.
This is what I was looking for. Great answer Nina.
0

You could write it like this

isHex = num => (!![...num].filter(n => (isNaN(parseInt(n, 16)))).length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.