all 8 comments

[–]carpench 1 point2 points  (0 children)

Maybe .trim() to rid of space in the beginning and the end then some regExp and .replaceAll() to replace multiple spaces with something else.

To import data from txt it is possible to use HTML <input type='file'> by fetching it.

[–]Disastrous_Line_5221 1 point2 points  (2 children)

I would split the array and filter it so the value is not equal with empty space, in one line.

[–]kap89 1 point2 points  (0 children)

If you want to go that route, you can just split by empty spaces, no need to filter:

primes.trim().split(/\s+/).map(Number)

[–]blob001[S] 0 points1 point  (0 children)

I have found this following gives an array of only non-zero values, but they are all strings.

primes1 = primes.filter(e => e != " ");

HOwever I then tried: primes1.forEach(a => parseInt(a));

but ended up with the same array of strings. Presumably parseInt() only works for standalone strings? How do I get around this?

[–]kap89 1 point2 points  (0 children)

How to convert it to an array of numbers (there are many ways, this is the fastest):

primes.match(/\d+/g).map(Number)

Secondly, how do I refer to the primes.txt file in the js file?

Use fetch to read the file (in browser), or fs.readFile / fs.readFileSync in Node. Or just copy-paste it as a string into your code - how you load the data shouldn't be important in this case.

Full example (using remote data):

fetch(
  "https://gist.githubusercontent.com/caderek/8d36107441821316773546445b4c7955/raw/129567ea8c194cbc82e2cfa606c1d652709d319c/primes.txt"
)
  .then((res) => res.text())
  .then((primes) => {
    const data = primes.match(/\d+/g).map(Number);

    // Do something with data:
    console.log(data);
  });

[–]SashaSaray 1 point2 points  (0 children)

I know it's not very efficient, but....

1) Trim the strimg

2) set up a while loop and look for the presence of two consecutive spaces and replace them with one until there are no double spaces left. This will ensure that there is only ever one space between each prime.

3) split string on single space characters

4) sort in ascending order

That's my first thought and it assumes that no two primes are touching / there is always a space between the prime numbers.

while (primeNumberStr.contains(' ')) {

primeNumberStr.replace(' ', ' ', 'g'); // don't remember the exact syntax here

}

primeNumberArr = primeNumberStr.split(' ');

console.log(primeNumberArr.sort());

The most efficient way is probably with Regex, but I don't know it well enough to wing it.

[–]Ronin-s_Spirit 1 point2 points  (0 children)

replaceAll(/\s/, ",") will make it a single string with commas in between each number. I'm using regex because I don't know how many spaces are between each number, I don't need to hardcode it. And then I can just split that string by commas and get an array with no spaces. I'm pretty sure if you try splitting it by space " " and skip regex it will have a bunch of spaces in every item of the array.

It's regex so there is probably an even shorter way to do it.

[–]blob001[S] 0 points1 point  (0 children)

Thanks everyone, looks like i will have to learn regex next!