function within a loop within a function? - function

I'm coding an email verification form in 3 parts.
Part 1 - check a single character against a list of allowed characters and return true/false.
Part 2 - check a string of characters as the part before or after the '#' using a loop calling the previous function to each successive character.
Part 3 - check a complete email that it includes only one '#', the substring before and after the '#' both satisfy part 2 and the substring following the '#' has only one full stop.
I've got part 1 down but my loop for part 2 is incorrect and returning true for all input values other than a blank form. here is the code -
function isValidEmailPart(part)
{ var emailPartInput = document.getElementById("isValidPartArg").value;
var emailPartLength = alert(emailPartInput.length);
{
if (emailPartInput.length == "")
{
return (false)
}
else
{
NUMBER_OF_CHARACTERS = alert((emailPartInput.length) - 1);
var i = 0;
{for(var i=0; i<NUMBER_OF_CHARACTERS; i++)
{
function isValidEmailChar()
{ var validChars = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9,_,-,.,';
var emailPartInput = document.getElementById("isValidPartArg").value;
var charInput = emailPartInput.charAt(i);
var inputVar = validChars.indexOf(charInput);
if (inputVar < 0)
{
return (false)
}
}
}
return (true);
}
}
}
}
I know it must be something simple, there are no errors returning I have no idea what I'm doing wrong.

Please, consider the following things very carefully:
Define functions separately: you can call a function from another function BUT don't define a function inside a function
Make sure that your code is ok, pay attention to your code syntax: I found additional { for example. Usually your code editor highlights code syntax errors.
Pay attention to your code's indent: having a good indent helps you have a clearer view of your code and helps you find your potential code mistakes.
Review the different types of variables: in javascript, the variables can have different types: boolean, integer, float, string, etc. You can only compare variables of a same types (Do not mix carrots and potatoes!) and so, you cannot compare emailPartInput with an empty string "" for example.
Before reading the code bellow, you should try to search what was wrong it your code, and what has to be modified to make it work.
Check very carefully the comments I wrote in the code that follows (I took a lot of time to write them!)
The javascript functions:
// This functions verifies if a char 'my_char' is valid
function isValidEmailChar(my_char)
{
// 'my_char' is a i-th character of 'emailPartInput'
var output = false;
// 'validChars' is the array containing all the valid characters
var validChars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9','_','-','.'];
// We want to check if 'my_char' is in the array 'validChar'
// So, for each character in the array 'validChar', we check that there's at least
// 1 character in it which is equal to 'my_char'
for(var i=0; i<validChars.length; i++)
{
// 'output' is the result that the function 'isValidEmailChar' will return
// It is initially set to "false"
// The line below means: we store in 'output'
// the result of " output OR ['my_char' EQUALS the i-th character in the array 'validChars'] ".
// Which means that, in the end, 'output' will be "true" if there's at least one i-th character
// in the array 'validChars' where 'my_char' EQUALS the i-th character in the array 'validChars'.
output = (output || (my_char == validChars[i]));
}
// We return the output
// Note: It is better to define 1 'return' and not several
return output;
}
// This function verifies if a part of Email is valid
function isValidEmailPart(emailPartInput)
{
// 'emailPartInput' is the part of email
// 'output' is your function's result to be returned
var output = false;
alert("INPUT = "+emailPartInput);
var nb_of_characters = emailPartInput.length;
alert("number of characters = "+nb_of_characters);
if (nb_of_characters != 0)
{
output = true;
var i = 0;
while(output && i<nb_of_characters)
{
// 'is_character_valid' is a boolean value which is set to:
// - true: if the i-th character of 'emailPartInput' is valid
// - false: if not valid
var is_character_valid = isValidEmailChar(emailPartInput.charAt(i));
// The line below means that we store in the variable 'ouput' the result of
// 'output' AND 'is_character_valid', which means that:
// if there's at least one 'is_character_valid' set to false
// (= one i-th character of 'emailPartInput' is not valid)
// 'output' will then be equals to false
output = output && is_character_valid;
i++;
// We remark that if 'output' is false, we quit the 'while' loop
// because finding one invalid character means that 'emailPartInput' is invalid
// so, we do not need to check the other characters of 'emailPartInput'
}
}
else
{
alert("No emailPartInput has been input");
}
// We return the output
return output;
}
Here's a working example where you can test your functions:
<HTML>
<HEAD>
<SCRIPT language="javascript">
// This functions verifies if a char 'my_char' is valid
function isValidEmailChar(my_char)
{
// 'my_char' is a i-th character of 'emailPartInput'
var output = false;
// 'validChars' is the array containing all the valid characters
var validChars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9','_','-','.'];
// We want to check if 'my_char' is in the array 'validChar'
// So, for each character in the array 'validChar', we check that there's at least
// 1 character in it which is equal to 'my_char'
for(var i=0; i<validChars.length; i++)
{
// 'output' is the result that the function 'isValidEmailChar' will return
// It is initially set to "false"
// The line below means: we store in 'output'
// the result of " output OR ['my_char' EQUALS the i-th character in the array 'validChars'] ".
// Which means that, in the end, 'output' will be "true" if there's at least one i-th character
// in the array 'validChars' where 'my_char' EQUALS the i-th character in the array 'validChars'.
output = (output || (my_char == validChars[i]));
}
// We return the output
// Note: It is better to define 1 'return' and not several
return output;
}
// This function verifies if a part of Email is valid
function isValidEmailPart(emailPartInput)
{
// 'emailPartInput' is the part of email
// 'output' is your function's result to be returned
var output = false;
alert("INPUT = "+emailPartInput);
var nb_of_characters = emailPartInput.length;
alert("number of characters = "+nb_of_characters);
if (nb_of_characters != 0)
{
output = true;
var i = 0;
while(output && i<nb_of_characters)
{
// 'is_character_valid' is a boolean value which is set to:
// - true: if the i-th character of 'emailPartInput' is valid
// - false: if not valid
var is_character_valid = isValidEmailChar(emailPartInput.charAt(i));
// The line below means that we store in the variable 'ouput' the result of
// 'output' AND 'is_character_valid', which means that:
// if there's at least one 'is_character_valid' set to false
// (= one i-th character of 'emailPartInput' is not valid)
// 'output' will then be equals to false
output = output && is_character_valid;
i++;
// We remark that if 'output' is false, we quit the 'while' loop
// because finding one invalid character means that 'emailPartInput' is invalid
// so, we do not need to check the other characters of 'emailPartInput'
}
}
else
{
alert("No emailPartInput has been input");
}
// We return the output
return output;
}
function test() {
var my_input = document.getElementById("my_input").value;
var result = isValidEmailPart(my_input);
if(result)
alert("The part of email is valid");
else
alert("The part of email is NOT valid");
}
</SCRIPT>
</HEAD>
<BODY>
Enter you Email part here:
<INPUT type="text" id="my_input" value="" />
<button onclick="javascript:test();">Check the Email part!</button>
</BODY>
</HTML>
NB: The most important is to make sure that you understand what you wrote in your code and what was wrong.
I think you know that just copying a working won't be a benefit for you.
If you read my code, I hope you spent your time to understand it and to read the comments carefully (I took a lot of time to write them! :S)
You can check free online tutorials to learn javascript too! :)
Hope this helps. If you have any questions, do not hesitate to ask, I'll be glad to help.

Related

Script that would find and mark the same words in the paragraph

I'm a fiction writer and I used to do my writing in MS Word. I've written some macros to help me edit the fiction text and one of them check the paragraph and marks (red) the duplicate (or triplicate words, etc). Example:
"I came **home**. And while at **home** I did this and that."
Word "home" is used twice and worth checking if I really can't change the sentence.
Now I mostly use google documents for writing, but I still have to do my editing in MS Word, mostly just because of this macro - I am not able to program it in the google script.
function PobarvajBesede() {
var doc = DocumentApp.getActiveDocument();
var cursor = DocumentApp.getActiveDocument().getCursor();
var surroundingText = cursor.getSurroundingText().getText();
var WordsString = WORDS(surroundingText);
Logger.log(WordsString);
//so far, so good. But this doesn't work:
var SortedWordsString = SORT(WordsString[1],1,False);
// and I'm lost.
}
function WORDS(input) {
var input = input.toString();
var inputSplit = input.split(" ");
// Logger.log(inputSplit);
inputSplit = inputSplit.toString();
var punctuationless = inputSplit.replace(/[.,\/#!$%\?^&\*;:{}=\-_`~()]/g," ");
var finalString = punctuationless.replace(/\s{2,}/g," ");
finalString = finalString.toLowerCase();
return finalString.split(" ") ;
}
If I could only get a list of words (in uppercase, longer than 3 characters), sorted by the number of their appearances in the logger, it would help me a lot:
HOME (2)
AND (1)
...
Thank you.
Flow:
Transform the string to upper case and sanitize the string of all non ascii characters
After splitting the string to word array, reduce the array to a object of word:count
Map the reduced object to a 2D array [[word,count of this word],[..],...] and sort the array by the inner array's count.
Snippet:
function wordCount(str) {
str = str || 'I came **home**. And while at **home** I did this and that.';
var countObj = str
.toUpperCase() //'I CAME **HOME**...'
.replace(/[^A-Z ]/g, '') //'I CAME HOME...'
.split(' ') //['I', 'CAME',..]
.reduce(function(obj, word) {
if (word.length >= 3) {
obj[word] = obj[word] ? ++obj[word] : 1;
}
return obj;
}, {}); //{HOME:2,DID:1}
return Object.keys(countObj)
.map(function(word) {
return [word, countObj[word]];
}) //[['HOME',2],['CAME',1],...]
.sort(function(a, b) {
return b[1] - a[1];
});
}
console.info(wordCount());
To read and practice:
Object
Array methods
This is a combination of TheMaster answer and some of my work. I need to learn more about the way he did it so I spent some learning time today. This function eliminates some problems I was having the carriage returns and it also removes items that only appear once. You should probably pick TheMasters solution as I couldn't have done it without his work.
function getDuplicateWords() {
var str=DocumentApp.getActiveDocument().getBody().getText();
var countObj = str
.toUpperCase()
.replace(/\n/g,' ')
.replace(/[^A-Z ]/g, '')
.split(' ')
.reduce(function(obj, word) {
if (word.length >= 2) {
obj[word] = obj[word] ? ++obj[word] : 1;
}
return obj;
}, {});
var oA=Object.keys(countObj).map(function(word){return [word, countObj[word]];}).filter(function(elem){return elem[1]>1;}).sort(function(a,b){return b[1]-a[1]});
var userInterface=HtmlService.createHtmlOutput(oA.join("<br />"));
DocumentApp.getUi().showSidebar(userInterface);
}
function onOpen() {
DocumentApp.getUi().createMenu('MyMenu')
.addItem('Get Duplicates','getDuplicateWords' )
.addToUi();
}
And yes I was having problems with get the results to change in my last solution.

Comparison of objects always returns default select case

So, I aim to pass 2 states into the following function and have it determine whether the states are identical, or are neighbors. If they are, I want the function to return true, and if not, false.
function shouldWeRun(origin, destination) {
var shouldWeRun = false;
switch (origin) {
case destination:
shouldWeRun = true;
case "Massachusetts":
if ( destination == "Connecticut" ||
destination == "New Hampshire" ||
destination == "New York" ||
destination == "Rhode Island" ||
destination == "Vermont" ) { shouldWeRun = true; };
break;
default:
shouldWeRun = false;
};
return shouldWeRun;
};
When I declare states as an array of strings, like this...
var states = ["Massachusetts","Massachusetts","Connecticut","Virginia"];
...and run this:
Logger.log("same state should return true: " + shouldWeRun(states[0],states[1]));
Logger.log("state neighbors should return true: " + shouldWeRun(states[0],states[2]));
Logger.log("non-neighbor states should return false: " + shouldWeRun(states[0],states[3]));
...the function works as advertised.
However, here's the problem: if I instead get the states values from the spreadsheet (how I need to) like this...
var states = sheet.getRange("H2:H31").getValues();
...the function always returns the default case and therefore, false.
Assume that every cell in the range H2:H31 is "Massachusetts".
The object returned by sheet.getRange("H2:H31").getValues() is always a 2D array, ie an array of arrays, even when it is a single column.
You are comparing strings with arrays, that's why the result is always false.
You should "flatten" that 2D array to get a simple one.
You can do this simply with a small code like this :
var states = sheet.getRange("H2:H31").getValues().join().split();

Understanding ES6 tagged template literal

Following code snippet is used on Mozilla (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to explain Tagged Template literal, please help me understand what following function is doing, i am unable to get the actual flow of the function, since they have used keys.foreach and when i inspected in Chrome, keys was a function, so not able to understand
function template(strings, ...keys) {
return (function(...values) {
var dict = values[values.length - 1] || {};
var result = [strings[0]];
keys.forEach(function(key, i) {
var value = Number.isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join('');
});
}
var t1Closure = template`${0}${1}${0}!`;
t1Closure('Y', 'A'); // "YAY!"
var t2Closure = template`${0} ${'foo'}!`;
t2Closure('Hello', {foo: 'World'}); // "Hello World!"
Most of the complexity in the example comes from the overloaded function and the forEach invocation, not from the tagged template literals. It might better have been written as two separate cases:
function dictionaryTemplate(strings, ...keys) {
return function(dict) {
var result = "";
for (var i=0; i<keys.length; i++)
result += strings[i] + dict[keys[i]];
result += strings[i];
return result;
};
}
const t = dictionaryTemplate`${0} ${'foo'}!`;
t({0: 'Hello', foo: 'World'}); // "Hello World!"
function argumentsTemplate(strings, ...keys) {
is (!keys.every(Number.isInteger))
throw new RangeError("The keys must be integers");
return function(...values) {
var result = "";
for (var i=0; i<keys.length; i++)
result += strings[i] + values[keys[i]];
result += strings[i];
return result;
};
}
const t = argumentsTemplate`${0}${1}${0}!`;
t('Y', 'A'); // "YAY!"
Template is a custom function defined by us to parse the template string, whenever a function is used to parse the template stringThe first argument of a tag function contains an array of string values. The remaining arguments are related to the expressions. so here specifically we have written the function to that given output I had got confused because when in inspected keys inside the forEach, i got a function in console, but inspecting the function before forEach gave keys as the array of configurable string ${0} and ${1} in first example

Guessing Number Game Program not Functioning Correctly

in my program, the user sets a range of numbers for the computer to guess. The user then has to guess which number the computer chose with a limit of guesses starting at 5. There are several problems in my functioning program in which I do not understand how to fix. These errors include:
-The number of guesses left always remains at 0. It won't start at 5 and decrease by 1 each time I click the btnCheck button.
-Whenever I click the btnCheck button for a new guessing number, the statement if you've guessed too high or too low remains the same.
-When I press btnNewGame, the values I insert in my low value and my high value text inputs will not be cleared.
-How can the computer generate a random whole number based on what I set as the number range?
Revising my code down below will be much appreciated.
// This line makes the button, btnCheckGuess wait for a mouse click
// When the button is clicked, the checkGuess function is called
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
// This line makes the button, btnNewGame wait for a mouse click
// When the button is clicked, the newGame function is called
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Declare Global Variables
var computerGuess:String; // the computer's guess
var Statement:String; // Statement based on your outcome
// This is the checkGuess function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function checkGuess(e:MouseEvent):void
{
var LowValue:Number; // the user's low value
var HighValue:Number; // the user's high value
var UserGuess:Number; // the user's guess
var CorrectGuess:int; // the correct number
var FirstGuess:String; //the user's guess
// get the user's range and guess
LowValue = Number(txtinLow.text);
HighValue = Number(txtinHigh.text);
UserGuess = Number(txtinGuess.text);
// determine the number of the user
GuessesLeft = checkCorrectGuess(FirstGuess);
lblNumber.text = GuessesLeft.toString();
lblStatement.text = "You have guessed " + Statement.toString() + "\r";
}
// This is function checkColoursCorrect
// g1– the user's guess
function checkCorrectGuess(g1:String):int
{
var GuessesLeft:int = 5; // How many guesses are left
if (g1 != computerGuess)
{
GuessesLeft - 1;
}
else
{
GuessesLeft = 0;
}
return GuessesLeft;
}
// This is the newGame function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function newGame(e:MouseEvent):void
{
var Guess1:int; // computer's guess in numbers
var UserGuess1:int; // user's guess in numbers
Guess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
UserGuess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
if (Guess1 > UserGuess1) {
Statement = "TOO HIGH";
} else if (Guess1 < UserGuess1) {
Statement = "TOO LOW";
} else if (Guess1 == UserGuess1) {
Statement = "CORRECTLY";
}
txtinGuess.text = "";
lblStatement.text = "";
}
// This is function randomWholeNumber
// highNumber – the maximum value desired
// lowNumber – the minimum value desired
// returns – a random whole number from highNumber to lowNumber inclusive
function randomWholeNumber(highNumber:int,lowNumber:int):int //How do I make a whole random number based on the range the user made?
{
return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}
To answer your questions...
You've declared GuessesLeft inside checkCorrectGuess() which means its a local variable that's being redefined every time you call the function. Futhermore, because you're passing in var FirstGuess:String; (an uninitialized, non-referenced string variable), (g1 != computerGuess) is returning false, and the answer is always 0.
GuessesLeft - 1; is not saving the result back to the variable. You need to use an assignment operator such as GuessesLeft = GuessesLeft - 1 or simply type GuessesLeft-- if all you want is to decrement. You could also write GuessesLeft -= 1 which subtracts the right from the left, and assigns the value to the variable on the left. See AS3 Operators...
You've already assigned values to these TextFields earlier; simply repeat the process inside of newGame() with a txtinLow.text = "" (same with high)
Use your variables. You defined them earlier in checkGuess() as UserGuess, LowValue, and HighValue
Be mindful that you only need to split out functionality into separate functions if that piece of code is likely to be called elsewhere. Otherwise, every function on the stack incurs more memory and performance hits. checkCorrectGuess() falls into that category and is therefore unnecessary.
Also, you are printing your feedback to the user in the newGame() function instead of checkGuess(). It seemed like an oversight.
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Global Variables
var computerGuess:int;
var remainingGuesses:int;
newGame();
function newGame(e:MouseEvent):void {
// Reset our guess limit
remainingGuesses = 5;
// Generate a new number
computerGuess = random(int(txtinLow.text), int(txtinHigh.text));
// Reset our readouts.
txtinGuess.text = "";
lblStatement.text = "";
}
function checkGuess(e:MouseEvent):void {
var guess:int = int(txtinGuess.text);
var msg:String;
if (guess == computerGuess) { // Win
remainingGuesses = 0; // Zero our count
msg = "CORRECT";
} else { // Missed
remainingGuesses--; // Decrement our count
if (guess > computerGuess) {
msg = "TOO HIGH";
} else if (guess < computerGuess) {
msg = "TOO LOW";
}
}
lblNumber.text = remainingGuesses.toString();
lblStatement.text = "You have guessed " + msg;
}
function random(low:int, high:int):int {
return Math.floor((high - low + 1) * Math.random() + low);
}

GoogleScript Spreadsheet Custom Function Handling a range of cells and getting their values

I have a Goggle Spreadsheet with some data, and I want to write a custom function to use in the sheet, which accepts a range of cells and a delimiter character, takes each cell value, splits it by the delimiter, and counts the total.
For example
Column A has the following values in rows 1-3: {"Sheep","Sheep,Dog","Cat"}
My function would be called like this: =CountDelimitedValues(A1:A3;",");
It should return the value: 4 (1+2+1)
The problem I am having is in my custom script I get errors like
"TypeError: cannot get function GetValues from type Sheep"
This is my current script:
function CountArrayList(arrayList, delimiter) {
var count = 0;
//for (i=0; i<array.length; i++)
//{
//count += array[i].split(delimiter).length;
//}
var newArray = arrayList.GetValues();
return newArray.ToString();
//return count;
}
I understand that the parameter arraylist is receiving an array of objects from the spreadsheet, however I don't know how to get the value out of those objects, or perhaps cast them into strings.
Alternatively I might be going about this in the wrong way? I have another script which extracts the text from a cell between two characters which works fine for a single cell. What is it about a range of cells that is different?
That's something you can achieve without using script but plain old formula's:
=SUM(ARRAYFORMULA(LEN(A1:A3)-LEN(SUBSTITUTE(A1:A3; ","; "")) + 1))
Credit goes here: https://webapps.stackexchange.com/q/37744/29140
something like this works :
function CountArrayList(arrayList) {
return arrayList.toString().split(',').length
}
wouldn't it be sufficient ?
edit Oooops, sorry I forgot the user defined delimiter, so like this
function CountArrayList(arrayList,del) {
return arrayList.toString().split(del).length
}
usage : =CountArrayList(A1:C1;",")
NOTE : in this example above it would be dangerous to use another delimiter than "," since the toString() joins the array elements with commas... if you really need to do so try using a regex to change the commas to what you use and apply the split on that.
try like this :
function CountArrayList(arrayList,del) {
return arrayList.toString().replace(/,/g,del).split(del).length
}
Another solution I have was that I needed to implicitly cast the objects in the array being passed as a string.
For example this function accepts the array of cells, and outputs their contents as a string with del as the delimiter (similar to the String.Split() function). Note the TrimString function and that it is being passed an element of the array.
function ArrayToString(array,del) {
var string = "";
for (i=0; i < array.length; i++) {
if (array[i] != null) {
var trimmedString = TrimString(array[i]);
if (trimmedString != "") {
if (string.length > 0) {
string += del;
}
string += trimmedString;
}
}
}
return string;
}
Below is the TrimString function.
function TrimString(string) {
var value = "";
if (string != "" && string != null) {
var newString = "";
newString += string;
var frontStringTrimmed = newString.replace(/^\s*/,"");
var backStringTrimmed = frontStringTrimmed.replace(/\s*$/,"");
value = backStringTrimmed;
}
return value;
}
What I found is that this code threw a TypeError unless I included the declaration of the newString variable, and added the array element object to it, implicitly casting the array element object as a string. Otherwise the replace() functions could not be called.