Convert permute method from Java to JavaScript - javascript

I implemented a Java code for permutation combination a string with input str= "word1 word2 word3" then output as:
arr[0]= "word1 word2"
arr[1]= "word1 word3"
arr[2]= "word1 word2 word3"
arr[3]= "word2 word1"
arr[4]= "word2 word1 word3"
arr[5]= "word2 word3 word1"
arr[6]= "word3 word1"
arr[7]= "word3 word1 word2"
arr[8]= "word3 word2 word1"
This is Java code:
private static void permute(String[] ss, boolean[] used, String res, int level, List<String> list) {
if (level == ss.length && res != ""){
list.add(res);
return;
}
for (int i = 0; i < ss.length; i++) {
// Check if the string is currently used
if (used[i]) {
continue;
}
// Check if res is empty or a single word
if(level > 1)
list.add(res);
used[i] = true;
permute(ss, used, res + " " + ss[i], level + 1, list);
used[i] = false;
}
}
public static List<String> PermuteWords(String s){
String[] ss = s.split(" ");
boolean[] used = new boolean[ss.length];
String res = "";
List<String> list = new ArrayList<String>();
permute(ss, used, res, 0, list);
return list;
}
When converting to JS code, I don't know what are errors in this code:
function permute(ss, used, res, level, list){
if(level==ss.lenght&&res!==""){
list.add(res);
return;
}
for(var i=0; i<ss.lenght; i++){
if (used[i]===true){
continue;
}
if(level>1){
list.add(res);
used[i]=true;
permute(ss, used, res+" "+ss[i], level+1, list)
used[i]=false;
}
}
}
function permuteword(s){
var ss;
for(var i=0; i<s.length;i++){
ss[i]=s[i];
}
var used;
for(var j=0; j<s.length;j++){
used[j]=false;
}
var result;
permute(ss, used, res, 0, result);
return result;
}

Java and Javascript may sound almost the same but they aren't. They take different appoaches doing different things. If in Java You are checking the console for errors on running the program, then in Javascript you can check for the errors in the browser console (open console with F12)
POPULATE ARRAY
Javascript arrays don't have add() method. To add a value to it, use push:
list.push(res)
POPULATE ARRAY #2
In permuteword() function you are trying to populate variable ss that is not initialized. Compiler doesn't understand where you want to put that value. Initialize ss as an empty array:
var ss = [];
TYPOs
In the first for loop you have ss.lenght. Fix that. Always check for the typos.
EXTRA
In permuteword() you are passing res to the permute() function although you don't have it defined in the function. Things will work if res is a global variable defined outside of the functions.
And take advantage of that browser console. Every Javascript developer (from noobie to pro) has it always open!

I ran the code in couchdb and cloudant that showed the general errors that i missed checking the syntax. I modified the code as:
function permute(ss, used, res, level, list){
if(level==ss.lenght&&res!==""){
list.push(res);
return;
}
for(var i=0; i<ss.length; i++){
if (used[i]===true){
continue;
}
if(level>1){
list.push(res);
used[i]=true;
permute(ss, used, res+" "+ss[i], level+1, list)
used[i]=false;
}
}
}
function permuteword(s){
var ss=[];
for(var i=0; i<s.length;i++){
ss[i]=s[i];
}
var used=[];
for(var j=0; j<s.length;j++){
used[j]=false;
}
res="";
var result=[];
permute(ss, used, res, 0, result);
return result;
}
function(doc){
var list=permuteword("word1 word2 word3");
for(var i=0; i<list.length; i++){
index("default", list[i],{store, true});
}
}
However, i still meet the errors as:
{"error":"{nocatch,{compilation_error,<<\"Expression does not eval to a function. ((new String(\\"function permute(ss, used, res, level, list){\\r\\n if(level==ss.lenght&&res!==\\\\"\\\\"){\\r\\n list.push(res);\\r\\n return;\\r\\n }\\r\\n\\r\\n for(var i=0; i1){\\r\\n list.push(res);\\r\\n used[i]=true;\\r\\n permute(ss, used, res+\\\\" \\\\"+ss[i], level+1, list)\\r\\n used[i]=false;\\r\\n }\\r\\n }\\r\\n}\\r\\nfunction permuteword(s){\\r\\n var ss=[];\\r\\n ss=s.split(\\\\" \\\\");\\r\\n var used=[];\\r\\n for(var j=0; j>}}","reason":"[{couch_os_process,prompt,3,[{file,\"src/couch_os_process.erl\"},{line,65}]},\n {dreyfus_index_updater,update,2,\n [{file,\"src/dreyfus_index_updater.erl\"},{line,42}]}]"}

Related

JavaScript UDF fails in Airflow but works in Bigquery

CREATE TEMP FUNCTION replace_all(input_string STRING, replace_list ARRAY<STRING>)
RETURNS STRING
LANGUAGE js AS
r"""
myarray=input_string.split(" ")
for (var i = 0; i < replace_list.length; i++)
{
for(var j=0;j < myarray.length;j++){
if(myarray[j]==replace_list[i]){
myarray[j] =''
}
}
}
input_string = myarray.join(' ')
return input_string;
""";
This above function throws error 'Unclosed triple-quoted raw string literal' when executed in Airflow. This is a function which I have defined in my query. Works good at bigquey console but errored out in Airflow.
Any inputs how the above error can be resolved.

Nothing being Outputted after first Function Call - Javascript

I'm working on a javascript project that will list everything from user name, email, phone number, and notes from AD into into a spreadsheet, so far i have two functions. however anything i put after getExternalID(users[i].externalIds ||[] does not output any information. here's a snippet of what i have. I'm a bit of a novice, would this be due to how I formatted the script?
function writeToSpreadsheet(){
var values = [];
var users = AdminDirectory.Users.list({domain:'domain'}).users;
for (var i=0; i<users.length; i++){
values.push([users[i].name.fullName, getExternalID(users[i].externalIds ||[], getPhones(users[i].phones ||[], ))]); // accounts for blank data
}
var spreadsheetUrl = 'https://docslink';
SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}
function getExternalID(arr) {
return arr.map(function(ExternalIDsObj) {
return ExternalIDsObj.value;
}).join(', ') + ('#differentemail') //takes employeeID and adds Email Address
}
function getPhones(arr) {
return arr.map(function(phoneObj) {
return phoneObj.value;
}).join(', ')
}
Since you are aiming to output the data from Active Directory into a spreadsheet, you need to use Sheets API to do that after populating the values array.
Also, for best practice the two get functions are separated from the main function.
Edit: There must be a typo on your closing parenthesis on your getExternalID() call. It's only supposed to get one argument.
Sample Code:
function writeToSpreadsheet(){
var values = [];
var users = AdminDirectory.Users.list({domain:'domain'}).users;
for (var i=0; i<users.length; i++){
values.push([users[i].name.fullName, getExternalID(users[i].externalIds || []), getPhones(users[i].phones || [])]);
}
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1,values.length,values[0].length).setValues(values);
}
function getExternalID(arr) {
return arr.map(function(ExternalIDsObj) {
return ExternalIDsObj.value;
}).join(', ') + ('#differentemail.com');
}
function getPhones(arr) {
return arr.map(function(phoneObj) {
return phoneObj.value;
}).join(', ');
}
Sample Output: (note that these "accounts" do not have external ID information in Admin Console)

mongodb Push values into array variable

I am writing a cleansing function which will delete all invalid docs from collection. For this I have an idea to push invalid _id values into array variable and delete with $in.
function(scan){
var err
for(var n=1;n<scan;n++){
var doc = db.zeroDimFacts.findOne({_id:n}) ,nKeys =0;
for(k in doc)
nKeys++
if(nKeys <5)
err = n.toArray()
}
After I push all values to err Array, I have a script to delete matching docs. However, there is something missing with the code which throws me error at n.toArray() .
Can someone help me to correct the code?
function(scan) {
var doc;
var nKeys;
var err = [];
for(var n = 1; n < scan; ++ n) {
doc = db.zeroDimFacts.findOne({_id: n})
nKeys = 0;
for(k in doc) {
++ nKeys;
}
if(nKeys < 5) {
err.push(n);
}
}
return err;
};
Pay attention to findOne() call. When it returns null, n will get into the array which seems undesirable to me.
You can use findByIdAndRemove({criteria}) and depending on return value you can have the logic.

How to remove newline whitespace from text file in Node?

I'm writing a program that gives me random song names I've input into a text file. Here's the code I have so far:
var fs = require('fs'),
path = require('path');
fs.readFile('names.txt', 'utf8', function(err, data) {
var arr = data.toString().split('\n'),
names = [];
for (var i in arr) {
if (arr[i].length !== 0) {
names.push(arr[i].trim());
}
}
console.log(names[1]);
});
I've noticed that whenever I put an odd number into the console.log() statement it returns the empty newline / whitespace. How can I fix this or remove it? Thanks for any help!
Your situation isn't entirely clear without full details, mainly the contents of the text file, but you probably meant to trim before the if.
Try like this:
for (var i in arr) {
var trimmed = arr[i].trim();
if (trimmed .length !== 0) {
names.push(trimmed);
}
}
Also, you shouldn't really for(.. in ..) in arrays (See here).
It's better if you use for(var i = 0; i < arr.length; i++) (you can keep the rest exactly as it is)
Alternatively:
var names = data.toString().split('\n').map(function(line){
return line.trim();
}).filter(Boolean);

Cannot read property ScheduleDate from undefined

var AppPatientsList = JSON.parse(JSON RESPONSE);
var AppPatientsListSort = AppPatientsList.sort(function(a,b){
return a.firstName.toLowerCase() <b.firstName.toLowerCase()
? -1
: a.firstName.toLowerCase()>b.firstName.toLowerCase()
? 1 : 0;
});
var DataArray = [];
for (var i = 0; i < AppPatientsListSort.length; ++i) {
if (AppPatientsListSort[i].firstName === search.value) {
var appointment = {};
appointment.PatientID = AppPatientsListSort[i].PatientID;
appointment.ScheduleDate = AppPatientsListSort[i].ScheduleDate;
alert(appointment.ScheduleDate); // Works fine, i get the date...
}
DataArray[i] = appointment;
}
var RowIndex = 0;
var ScheduleDate = "";
for (i = 0, len = DataArray.length; i < len; i++) {
// Throws me error in this place... WHY?
if (ScheduleDate != DataArray[i].ScheduleDate) {
ScheduleDate = DataArray[i].ScheduleDate;
}
}
What's wrong with this code, why i am not able to access the ScheduleDate?
You are only initializing the appointment variable when you are inside the if clause, but you are adding it to the array on every iteration.
If the first element of AppPatientsListSort does not have the value you search for, DataArray[0] will contain undefined.
In the second loop you then try to access DataArray[0].ScheduleDate which will throw an error.
Update:
Even more important, as JavaScript has no block scope, it might be that several entries in DataArray point to the same appointment object.
Depending on what you want to do, everything it takes might be to change
DataArray[i] = appointment;
to
DataArray.push(appointment);
and move this statement inside the if clause so that only appointments are added that match the search criteria.
Further notes: To have a look what your DataArray contains, make a console.dir(DataArray) before the second loop and inspect the content (assuming you are using Chrome or Safari, use Firebug for Firefox).

Categories