2

new to JS.

I'm using google script to create a function that will input a string will output the interger in it (if any). In order to optimize for google script, I've read suggestions on allowing also ranges as input.

I'm getting an error when using .map, but I can't figure out what it is. Any help. I've been looking for more examples of the use of map but none were helpfull.

Any idea?

thanks everyone

if (input.map) {
  input.map(
    if (isInt(split[i])) {
      result = split[i]
    });
} else {
  for (i = 0; i < split.length; i++) {
    if (isInt(split[i])) {
      result = split[i];
    }
  }
}

3
  • 1
    First parameter of map is a function what you have is an expressionMDN. Commented Oct 9, 2015 at 13:50
  • 1
    If the code doesn’t actually modify your input array, then forEach is better suited than map. Commented Oct 9, 2015 at 13:53
  • You cannot use statements as function arguments. Have you ever seen code that looks like foo(if(true) { ... }). That doesn't make sense. MDN usually has examples. Commented Oct 9, 2015 at 13:58

2 Answers 2

3

To .map you should pass function as parameter,

input.map(function() {
    // your code
});
Sign up to request clarification or add additional context in comments.

Comments

0

will input a string will output the interger in it (if any)

Try using isNaN , Number() , Array.prototype.filter() , typeof to return Number values

var arr = ["1", "a", "0"];
var res = arr.map(function(n) {
  return !isNaN(Number(n)) && Number(n)
}).filter(function(n) {
  return typeof n === "number"
});
console.log(res)

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.