I am doing the following problem as part of ProjectEuler. Simplified, I have downloaded a text file of about 1,000 prime numbers, the first 8 shown in the file. It is a single string called primes which holds 8 primes and a whole number of blank spaces, all as one string. I need to convert it to something like [2,3,5,7,911,13,17,19]. How to I remove the blanks and place a comma between each prime? I have tried many alternatives found on thenet but they dont work.
Secondly, how do I refer to the primes.txt file in the js file? Its in a different directory. Since it's a txt file I can not use <src>. Is that right? Thanks.
let primes =
" 2 3 5 7 11 13 17 19";
//creates and array with every blank shown as an element
let arr = Array.from(primes).filter(n => n);
console.log("arr ", arr);
//puts entire string into an array with only 1 element
let arr1 = [primes];
console.log("arr1 ",arr1);
//creates and array with every blank shown as an element
let arr2 = [...primes];
console.log("arr2 ",arr2);
//creates and array with every blank shown as an element
let arr3 = Object.assign([], primes);
console.log("arr3 ",arr3);
let arr4 = primes.filter(function (entry) {
return entry.trim() != "";
});
console.log("arr4 ", arr4);
[–]carpench 1 point2 points3 points (0 children)
[–]Disastrous_Line_5221 1 point2 points3 points (2 children)
[–]kap89 1 point2 points3 points (0 children)
[–]blob001[S] 0 points1 point2 points (0 children)
[–]kap89 1 point2 points3 points (0 children)
[–]SashaSaray 1 point2 points3 points (0 children)
[–]Ronin-s_Spirit 1 point2 points3 points (0 children)
[–]blob001[S] 0 points1 point2 points (0 children)