0

I want to check if my input string contains numbers and display an array of these numbers. The number is composed of an optional sign (- or +), one or more consecutive digits and an optional fractional part. The fractional part is composed of a dot . followed by zero or more digits.

For example f2('a1 12 13.b -14.5+2') : returns [1, 12, 13, -14.5, 2]

I try this code from a response here

function f2(input) {
  let str = String(input);

  for (let i = 0; i < str.length; i++) {
    console.log(str.charAt(i));

    if (!isNaN(str.charAt(i))) {
      //if the string is a number, do the following                
      return str.charAt(i);
    }
  }
}

let result = f2("a1 12 13.b -14.5+2");

console.log(result);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Soulef
  • 41
  • 8
  • 2
    That code makes rather little sense, as it returns the first single non-digit character it finds - not suitable for anything that is supposed to return an array to begin with. Do you have any own attempts to show, besides some copy&paste from sources that seem rather unfitting to begin with? – 04FS Feb 06 '19 at 13:22
  • what if input is `'a1a1 12 13.b -14.5+2'` – Maheer Ali Feb 06 '19 at 13:23
  • `a1 12 13.b -14.5+2` seems an incredibly arbitrary format. Where does this come from? We need to understand the rules better to answer this question I think – Liam Feb 06 '19 at 13:24
  • You should definitely use regular expressions. Since you don't give any info for your string format, we can't actually answer the question. Here is very informative answer about parsing floats from string in different cases, check it out: https://stackoverflow.com/a/12643073/1219754 – loler Feb 06 '19 at 13:33
  • @Liam it s really an arbitry format as it shown in the problem . the main goal is to define a function that accepts a string as parameter and returns an array of all numbers that a contains (the array elements must be numbers not strings). – Soulef Feb 06 '19 at 14:15
  • Ok, so what numbers a re in `1.5.2.251.1a`? It's a lot more difficult to find "all numbers" from a string than it seems. Why stringify the data at all? – Liam Feb 06 '19 at 14:16
  • @04FS I refer to the code and not copy/paste all what contains here is what i do function f2(input){ let str = String(input); let arrayResult = []; let R; for( let i = 0; i < str.length; i++){ console.log(str.charAt(i)); if(!isNaN(str.charAt(i))){ //if the string is a number, do the following R= str.charAt(i); return R; } arrayResult[i]=R } } let result=f2("a1 12 13.b -14.5+2"); let x=""; for(let i=0;i – Soulef Feb 06 '19 at 14:17
  • @Liam thats zhy i didnt find a sollution for that the exercice that i have to resolve contain this exemple to clarify the function – Soulef Feb 06 '19 at 14:19
  • @F0G thanx a lot i ll try it – Soulef Feb 06 '19 at 14:22

1 Answers1

1

You can easily use a regex expression to match the numbers in the string:

function f2(input) {
      let str = String(input);
      let result = str.match(/\-?\d+\.\d+|\-?\d+/g)
      return result
    }
    
    let result = f2("a1 12 13.b -14.5+2");
    
    console.log(result);
Neill Herbst
  • 2,072
  • 1
  • 13
  • 23
  • 1
    that solves problem thanx a lot but result is duplicated i choose an other name for the var result – Soulef Feb 06 '19 at 14:25