Recursive parser using split in javascript - javascript

I have an algorithm where the user will enter a string and I will parse it into an array of 2+ dimensions. So, for example, the user can enter 1,2,3;4,5,6 and set the text to be parsed by the semicolon and the comma. The first pass through will create an array with 2 entries. The second pass through will create a 3 entry array in both prior spots.
The user can add or remove the number of text items to be used to parse the original string such as the semicolon or comma, meaning the resulting array can have as many dimensions as parsing items.
This doesn't seem like a difficult problem, but I have run into some snags.
Here is my code so far.
vm.parsers = [';', ','];
vm.inputString = "1,2,3,4,5;6,7,8,9,10";
function parseDatasetText( )
{
vm.real = vm.parseMe( vm.inputString, 0);
};
function parseMe( itemToParse, indexToParse )
{
if ( indexToParse < vm.parsers.length )
{
console.log('Parsing *'+itemToParse+'* with '+vm.parsers[indexToParse]);
var tempResults = itemToParse.split( vm.parsers[indexToParse] );
for (var a=0; a<tempResults.length; a++)
{
console.log('Pushing '+tempResults[a]);
tempResults[a] = vm.parseMe( tempResults[a], parseInt( indexToParse ) + 1 )
console.log('This value is '+tempResults[a]);
}
}else
{
console.log('Returning '+itemToParse);
return itemToParse
}
};
As you can see from the console logs, the algorithm spits out an undefined after the last parse, and the final answer is undefined.
Maybe I just haven't slept enough, but I was thinking that the array would recursively populate via the splits?
Thanks

function parseDatasetText(){
//composing parser from right to left into a single function
//that applies them from left to right on the data
var fn = vm.parsers.reduceRight(
(nextFn, delimiter) => v => String(v).split(delimiter).map(nextFn),
v => v
);
return fn( vm.inputString );
}
Don't know what else to add.

You can use a simple recursive function like the following (here an example with 3 different delimiters):
function multiSplit(xs, delimiters) {
if (!delimiters.length) return xs;
return xs.split(delimiters[0]).map(x => multiSplit(x, delimiters.slice(1)));
}
data = '1:10,2:20,3:30;4:40,5:50,6:60';
res = multiSplit(data, [';', ',', ':']);
console.log(res)

The following function should suit your requirements, please let me know if not
var parsers = [';', ',', ':'],
inputString = "1:a,2:b,3:c,4:d,5:e;6:f,7:g,8:h,9:i,10:j",
Result = [];
function Split(incoming) {
var temp = null;
for (var i = 0; i < parsers.length; i++)
if (incoming.indexOf(parsers[i]) >= 0) {
temp = incoming.split(parsers[i]);
break;
}
if (temp == null) return incoming;
var outgoing = [];
for (var i = 0; i < temp.length; i++)
outgoing[outgoing.length] = Split(temp[i])
return outgoing;
}
Result = Split(inputString);
try it on https://jsfiddle.net/cgy7nre1/
Edit 1 -
Added another inputString and another set of parsers: https://jsfiddle.net/cgy7nre1/1/

Did you mean this?
var inputString = "1,2,3,4,5;6,7,8,9,10";
var array=inputString.split(';');
for (var i=0;i<array.length;i++){
array[i]=array[i].split(',');
}
console.log(array);

Related

Bringing numbers to the front

(Javascript)
functionName(“2 plus 3 is = 5!”);
would produce following message in console:
235 plus is =
I am unable to bring the numbers to the front, I am a little stumped
function frontNum(str) {
let emptyArr = [];
let rev = str.split("");
let expression = /[\d]/g;
for(let i = 0; i < rev.length; i++) {
if (rev[i].match(expression)) {
emptyArr += rev.pop(rev[i]);
}
}
console.log(emptyArr + rev.join(''));
}
frontNum("2 plus 3 is = 5!");
Since this is your home work I won't give you the correct version, but give you some pointers instead:
your emptyArr is an array, but you are adding data to it as if it was a string.
take a look at this topic, your pop causing problems
you can use your expression to capture all the digits and remove them from the string without need converting the string into array and loop through it (it can be done with 2 lines of code)
a way todo that
'use strict'
function frontNum(str)
{
let s = ''
, n = ''
for (let x of str)
{
if (/[0-9]/.test(x)) n += x
else s += x
}
console.log( n + s.replace(/ +/g,' ') )
}
frontNum('2 plus 3 is = 5!')

Can you call a for loop in a forEach method in Javascript?

First of all, I'm a total beginner in coding and have started a few weeks back, as an persona/intellectual challenge.
I want to know why do I get a null array in this simple script. I think it has something to do with var "i", but I can't find where the error is.
function DivEsc() {
var ssIn = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Input");
var ssOut = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Output");
var esc = ssIn.getRange(1,3).getValue(); //Escaños en reparto
var i = 0;
var arr = [400, 200,100,50];
var newArr = []
newArr = arr.forEach(num => {
for (var i = 1; i <= esc; i++){
return (num/i)
// newArr.push(num)
}
newArr.push(num/i)
}
)
Logger.log(newArr)
}
Array.prototype.forEach does not return a value, newArr will therefore become undefined. JavaScript is a dynamic language where the reassignment of values - even if they don't have the same type - is possible.
Further you return in the first iteration of the for loop, meaning that only the value for num / 1 will be calculated; probably not what you want.
// newArr is now undefined!
newArr = arr.forEach(num => {
for (var i = 1; i <= esc; i++){
// If you return a value here, the loop will stop after the first round with i = 1!
return (num/i)
// newArr.push(num)
}
newArr.push(num/i)
}
)
You could just directly push into newArr to make it work.
arr.forEach(num => {
for(var i = 1; i <= esc; i++) {
newArr.push(num / i);
}
)
If you want to learn more to try out different ways to implement your algorithm, look into JavaScript's functional array methods as e.g. Array.prototype.map and Array.prototype.reduce.
On a side note: Try using an editor/IDE that handles formatting code for you. In your snippet the formatting (e.g. end of the forEach call) makes it hard to reason about the code.

Javascript Regex Custom Replace

How do I get the following conversion using Regex?
Content(input data structure):
a-test
b-123
c-qweq
d-gdfgd
e-312
Conversion:
1-test
2-123
3-qweq
4-gdfgd
Final-312
var index = 1;
function c_replace() {
if(index == 5) { return "Final"; }
return index++;
}
there you go :D
// i assume you have a string input that contains linebreaks due to your question format
const input = `a-test
b-123
c-qweq
d-gdfgd
e-312`.trim(); // removing whitespace in front or behind the input data.
//splitting the lines on whitespace using \s+
const output = input.split(/\s+/).map((s, i, a) => {
// this will map your pattern asd-foooasdasd
const data = s.match(/^[a-z]+-(.+)$/);
// you may want to tweak this. right now it will simply throw an error.
if (!data) throw new Error(`${s} at position ${i} is a malformed input`);
// figure out if we are in the final iteration
const final = i == a.length -1;
// the actual output data
return `${final ? "Final" : (i + 1)}-${data[1]}`;
// and of course join the array into a linebreak separated list similar to your input.
}).join("\n");
console.log(output);
Test
var index=1;
var text=`a-test
b-123
c-qweq
d-gdfgd
e-312`;
function c_replace() {
if(index == 5) { return "Final-"; }
return index++ +'-';
}
console.log(text.replace(/.-/g,c_replace));
var input = [
'a-test',
'b-123',
'c-qweq',
'd-gdfgd',
'e-312'
];
var output = input.map((e, i) => ++i + e.slice(1));
output[output.length - 1] = 'Final' + output[output.length - 1].slice(1);
console.log(output);

Store coordinates from prompt and calculate the distance

I work on distance calculation between coordinates and I built something which works fine.
var pointsCoordinates = [[5,7],[5,8],[2,8],[2,10]];
function lineDistance(points) {
var globalDistance = 0;
for(var i=0; i<points.length-1; i++) {
globalDistance += Math.sqrt(Math.pow( points[i+1][0]-points[i][0] , 2 ) + Math.pow( points[i+1][1]-points[i][1] , 2 ));
}
return globalDistance;
}
console.log(lineDistance(pointsCoordinates));
I would like to improve it a little bit and send a prompt to store coordinates sent by users.
example:
alert(prompt("send me random coordinates in this format [,] and I will calculate the distance))
I would like to store theses coordinates and calculate the distance with my function which works.
I know I have to use push but it doesn't works, someone can help me to write it? I know it's simple but... I can't do it.
Thank you very much
Working and tested code. Take coordinates from the prompt and pass it to the lineDistance function and convert passed string into array.
function lineDistance(points) {
var globalDistance = 0;
var points = JSON.parse(points); // convert entered string to array
for(var i=0; i<points.length-1; i++) {
globalDistance += Math.sqrt(Math.pow( points[i+1][0]-points[i][0] , 2 ) + Math.pow( points[i+1][1]-points[i][1] , 2 ));
}
return globalDistance;
}
var pointsCoordinates = prompt("send me random coordinates in this format [,] and I will calculate the distance");
if (coordinates != null)
console.log(lineDistance(coordinates)); //[[5,7],[5,8],[2,8],[2,10]]
else
alert("Entered value is null");
Hope this will help you.
var userPrompt = prompt("send me random coordinates");// e.g [100,2] [3,45] [51,6]
var pointsCoordinates = parseCoordinates(userPrompt);
function parseCoordinates(unparsedCoord) {
var arr = unparsedCoord.split(" ");
var pair;
for (var i = 0; i < arr.length; i++) {
pair = arr[i];
arr[i] = pair.substr(1, pair.length - 2).split(",")
}
return arr;
}
function lineDistance(points) {
var globalDistance = 0;
for(var i = 0; i < points.length - 1; i++) {
globalDistance += Math.sqrt(Math.pow(points[i + 1][0] - points[i][0], 2) + Math.pow(points[i+1][1]-points[i][1], 2));
}
return globalDistance;
}
console.log(pointsCoordinates);
console.log(lineDistance(pointsCoordinates));
If you use prompt you don't need to also wrap it with alert.
Also, prompt will return with a string value, so you'll need to parse the data you're getting back (unless you're fine with a string)
In this case, since you want to get back an array of points, parsing can be a more complicated than just converting values. My recommendation is to use JSON.parse() to parse the input array
In your case you could use this code
var coordString = prompt("send me random coordinates in this format [,] and I will calculate the distance");
try {
var coordinates = JSON.parse(coordString);
// now check that coordinates are an array
if ( coordinates.constructor === Array ) {
// this might still fail if the input array isn't made of numbers
// in case of error it will still be caught by catch
console.log(lineDistance(coordinates));
}
} catch(error) {
// if something goes wrong you get here
alert('An error occurred: ' + error);
}
I recommend reading the MDN docs for prompt() if you want to know what else you can do with them.
Also, the docs for JSON.parse() can be useful if you want to parse values from a text string.

How to efficiently check if any substring in an array is contained in another string

I want to have something that checks if any substring in a list of strings is included in string. I have something that works, but I'm hoping there is something cleaner and more efficient, preferably just in one line I can call like if(string.contains(list.any)) or something.
var list = ['aa','bb','cc','dd'];
var string = "nygaard"; // true because "aa" is in "nygaard".
for (var i = 0; i < list.length; i++) {
if( string.indexOf( list[i] ) > -1 ) {
alert("True");
break;
}
}
var list = ['aa','bb','cc','dd'];
var string = "nygaard";
var patt = new RegExp(list.join('|'))
// regEx - /aa|bb|cc|dd/
patt.test(string)
//output true
Demo
Explanation
Dynamically create a regEx using new RegExp
for checking existance of multiple SubString we have | operator in RegEx
Use Array.join('|') to dynamically make a regExp like this aa|bb|cc
use test func to validate String
Edit - For Complex Cases - Problem is strings in list may have to be escaped for RegEx to work, Pointed By - #GabrielRatener
Compairing to my solu, #GabrielRatener's solution is better
var list = ['aa','bb','cc','dd', 'ab?', '(aa)'];
list = list.sort(function(a, b) {
if (a>b) return -1;
else if (a<b) return 1;
else return 0;
});
list = list.join(" ").replace(/[^\w\s]/g, function($1) {
return '\\' + $1
}).split(/\s/);
//["dd", "cc", "bb", "ab\?", "aa", "\(aa\)"]
//sorting needed to match "ab?" before single "a" as "b" will become optional
//due to "?"
//after processing
var string = "nygaard";
var patt = new RegExp(list.join('|')) // RegExp -> /dd|cc|bb|ab\?|aa|\(aa\)/
patt.test(string)
//true
Why not just put your loop in its own function you can call?
function stringContains(string, list) {
for (var i = 0; i < list.length; i++) {
if( string.indexOf( list[i] ) > -1 )
return true;
}
return false;
}
and then call it like this:
var list = ['aa','bb','cc','dd'];
var string = "nygaard";
if(stringContains(string, list))
alert("True");
If you're looking for a javascript library function, I don't believe there is one.
EcmaScript 6 is proposing a contains method for strings such that you could do this:
function stringContainsArray(str, array){
for (var i = 0; i < array.length; i++){
if (str.contains(array[i])){
return true;
}
}
return false;
}
var list = ['aa','bb','cc','dd'];
var string = "nygaard"; // true because "aa" is in "nygaard".
console.log(stringContainsArray(list));
// => true
Since implementation will likely remain spotty in the major browsers and other runtimes for the near future, you should add the following code for compatibility before the stringContainsArray function:
// String.prototype.contains will be implemented in
// the major browsers in the not too distant future
// this will add the same functionality to browsers
// or other runtimes that do not currently support it
if ( !String.prototype.contains ) {
String.prototype.contains = function() {
return String.prototype.indexOf.apply( this, arguments ) !== -1;
};
}
this may help: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/contains
You can also extend the string prototype to have a simple method:
String.prototype.containsArray = function(array){
return stringContainsArray(this, array);
}
Then you can simpy do:
"nygaard".containsArray(list);
// => true

Categories