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.
.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.reduce(althoughsomeis the right tool for the job here.reducecould be used if you wanted to reinvent the wheel).[...num]is num a string? If it's an array, why would you use the spread operator on it?numis misleading, then.