I am building a script that is embedded in HTML to generate a report that extracts information from an internal company system.
The objective is to build some kind of a funnel, making the sum of records in each of the stages. For example, I have:
Processes in stage 1 = "3"
Processes in stage 2 = "5"
Processes in stage 3 = " "
Processes at stage 4 = "2"
Processes in stage 5 = " "
However, I have a problem when one of the stages is empty (it is not 0) because there is no stage in that specific process. When I try to add, for example, Stage 1 and Stage 3, it always returns an empty value, while it should give result 3 (3 + 0).
<p id="global_calc"></p>
<script>
{
var A = Stage1.system.count;
var B = Stage2.system.count;
var total = A + B;
}
document.getElementById("global_calc").innerHTML = "The total of this stage is: " + total;
</script>
The Stage.system.count is the internal system's variable that has the number I want to eventually sum with var total.
But, for example, if Stage1.system.count = 3 and Stage2.system.count = " ", total result in " " instead of 3. How can I make this to count the black space as a 0?
First it's probably worth checking what variables you have; any number + " " will always give you a string:
const x = 123 + " "; // "123 ";
const y = 123 + "" // "123"
We can see that in practice in your example:
{
var A = 3;
var B = " ";
var total = A + B;
}
document.getElementById("global_calc").innerHTML = "The total of this stage is: " + total;
console.log(("The total of this stage is: " + total).replace(/ /g, '*'))
<div id="global_calc"></div>
It seems like you're using undefined as part of the addition, which returns the value NaN:
{
var A = 3;
var B = undefined;
var total = A + B;
}
document.getElementById("global_calc").innerHTML = "The total of this stage is: " + total;
console.log(("The total of this stage is: " + total).replace(/ /g, '*'))
<div id="global_calc"></div>
We'll need more details to figure out exactly why you're getting " " for total.
If you want a function which converts number -> number and " " -> 0 then Number would do:
const x = 3;
const y = " ";
console.log(Number(x));
console.log(Number(y));
Related
I am coding a phone number formatter for a large database. Everything is working, but there is an inconsistent printing of blank cells. Most of the cells are read through and are properly formatted, but there are some that show blank outputs in the wrong cells.
I have tried fixing this by resetting the cleanNumber variable to a blank string but this just posed another issue on line 33 with indexOf().
function myFunction() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // connects sheet to code
var startRow = 18;
var endRow = 41;
for (var i = startRow; i <= endRow; i++) { // i = currnet row | row to end at | add 1 to count each time
var workingCell = activeSheet.getRange(i, 2).getValue();
Logger.log("Original number: " + workingCell)
//If blank, move to next row
exit: if (workingCell.length == 0.0) {
var blank = "";
activeSheet.getRange(i, 3).setValue(blank);
Logger.log("This row is blank")
//break exit;
}
// cleanNumber if it isn't formatted already
else if (isNaN(workingCell)) { // runs if active cell is not a preformatted number
var cleanNumber = workingCell.replace(/\D/g, ''); // removes all non-numeric values
activeSheet.getRange(i, 3).setValue(cleanNumber);
Logger.log("Extra char's removed: " + cleanNumber)
}
// runs if active cell is already preformatted
else {
activeSheet.getRange(i, 3).setValue(workingCell);
Logger.log("No need for formatting: " + workingCell)
}
// If cleanNumber has a country code(+1), remove it
if ((cleanNumber.indexOf("1")) == 0) {
cleanNumber = cleanNumber.substring(1); //removes first character = "1"
activeSheet.getRange(i, 3).setValue(cleanNumber);
Logger.log("Country code removed: " + cleanNumber);
}
// If number is longer than 10 characters, create an extension variable - with entire number, remove 10 characters from front
if (cleanNumber.length > 10.0) {
var extension = cleanNumber.substring(10, 15);
var phoneNumber = cleanNumber.substring(0, 10);
var formatted = phoneNumber.slice(0, 3) + "-" + phoneNumber.slice(3, 6) + "-" + phoneNumber.slice(6, 15);
var finalPhoneNumber = formatted + " ext. " + extension;
activeSheet.getRange(i, 3).setValue(finalPhoneNumber);
Logger.log("This number is in its final ext. format: " + finalPhoneNumber);
}
//if number doesnt have an extension, put it into final format
else if (cleanNumber.length = 10.0) {
var frontFinal = cleanNumber.substring(0, 3);
var midFinal = cleanNumber.substring(3, 6);
var endFinal = cleanNumber.substring(6, 10);
var finalNumber = frontFinal + "-" + midFinal + "-" + endFinal;
activeSheet.getRange(i, 3).setValue(finalNumber);
Logger.log("This number is in its final format: " + finalNumber);
}
//if number is less than 10 numbers
else {
Logger.log("This number is shorter than 10 numbers" + cleanNumber);
}
cleanNumber = " ";
}
}
The pre-formatted numbers are on the left and the output is in the right column.
Here is some sample data, please consider that the issue seems to be stemming from blank rows.
Unformatted
1999-111-1111
1+2222-222222
4444444444 ext. 223
9738094395
9172609107
866.786.6682
973 330 2212
(631)563-4000 ext. 234
I look forward to solving this issue, thank you for the help :)
You can do it with ARRAYFORMULA or you may use the RegExp in your script.
=ArrayFormula(REGEXREPLACE(REGEXREPLACE(REGEXREPLACE(TO_TEXT(A2:A),"\D",),"^(?:1)?(\d{3})(\d{3})(\d{4})(\d{0,5}).*$","$1-$2-$3 ext. $4")," ext\. $",))
You are recommended to use batch operations
const values = [
['1999-111-1111'],
['1+2222-222222'],
['4444444444 ext. 223'],
[9738094395],
[9172609107],
['866.786.6682'],
['973 330 2212'],
['(631)563-4000 ext. 234'],
['973-809-4395'],
['']
];
const results = [];
for (const value of values) {
const cleanNumber = value[0].toString().replace(/\D/g, '');
const m = cleanNumber.match(/^(?:1)?(\d{3})(\d{3})(\d{4})(\d{0,5}).*$/);
if (m) {
let finalNumber = `${m[1]}-${m[2]}-${m[3]}`;
if (m[4]) { finalNumber += ` ext. ${m[4]}`; }
results.push([finalNumber]);
}
else {
results.push(value);
}
}
console.log(results.flat());
Problem
In my scripts.js the variable var fullURL = not getting the actual text to be tweeted out in the teaser1, teaser2 and teaser3 that I've stored in a variable. I basically want one of the three teasers to be randomly selected when people click fa-twitter
scripts.js
function shareTeam(){
$(".fa-twitter").click(function(){
// Grabs the names of all the players in the span
// Sets a variable marking the indexOf each of the names
// If the index doesn't find a space, it returns -1, which returns the full name
// Otherwise it will return only what follows the space
var lastNames = $("li span").map(function() {
var name = $(this).text();
var index = name.indexOf(" ");
return index == -1 ? name : name.substring(index + 1);
}).get();
console.log(lastNames);
var regularNames = lastNames.slice(0, 3); // Same as below, but no shuffling
regularName1 = regularNames[0]; // Forward
regularName2 = regularNames[1]; // Forward
regularName3 = regularNames[2]; // Defenseman
// Find me a random number between 1 and 3
// Where 1 is the start number and 3 is the number of possible results
var teaser = "teaser";
var rand = Math.floor(Math.random() * 3) + 1;
console.log(rand);
// Concatenate the two strings together
teaseRand = teaser.concat(rand);
// These are the components that make up that fullURL
var baseURI = "https://twitter.com/intent/tweet?url=";
var twitterUsername = "stltoday";
var interactiveURL = "http://graphics.########.com/STLblues";
// Randomly generate one of three teasers
var teaser1 = regularName3 + " to " + regularName2 + " back to " + regularName1 + " — GOAL! Create your own all-team #STLBlues team: ";
var teaser2 = "I picked my #STLBlues dream team. See which players I've chosen and build your own: ";
var teaser3 = "My #STLBlues team will skate circles around yours! Pick your team: ";
// This is the full url that will be switched in and out
// var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teaseRand;
var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teaseRand;
// It needs to be encoded properly as well
var encodedURL = encodeURIComponent(fullURL)
console.log(fullURL);
console.log(encodedURL);
// Change the href to the link every time the Twitter button is clicked
var changeLink = $("link--twitter").attr("href", encodedURL);
// if (lastNames.length === 6) {
// } else {
// // Generic teaser
// var teaser4 = "Pick your #STLBlues dream team from 50 of the best #StLouisBlues to hit the ice: " + interactiveURL + " (via #stltoday)";
// }
});
}
Unfortunately this teaseRand value will be either "teaser1" or "teaser2" or "teaser3" and not the value of your variables teaser1 or teaser2 or teaser3 if that makes sense. For your requirement you will need to add the teasers to an array and then randomly access from it. For e.g. if the array is called teaser then you will need to do teaser[rand] and obviously you will need to calculate the rand from 0 to 2 instead 1 to 3 like you have done now.
Please check the codepen that i have created here
http://codepen.io/19sthil80/pen/VKPqkR?editors=1111
$(document).ready(function(){
var teasers = [];
// Grabs the names of all the players in the span
// Sets a variable marking the indexOf each of the names
// If the index doesn't find a space, it returns -1, which returns the full name
// Otherwise it will return only what follows the space
var lastNames = $("li span").map(function() {
var name = $(this).text();
var index = name.indexOf(" ");
return index == -1 ? name : name.substring(index + 1);
}).get();
console.log(lastNames);
var regularNames = lastNames.slice(0, 3); // Same as below, but no shuffling
regularName1 = regularNames[0]; // Forward
regularName2 = regularNames[1]; // Forward
regularName3 = regularNames[2]; // Defenseman
// Find me a random number between 1 and 3
// Where 1 is the start number and 3 is the number of possible results
var teaser = "teaser";
var rand = Math.floor(Math.random() * 3);
console.log(rand);
// Concatenate the two strings together
teaseRand = teaser.concat(rand);
// These are the components that make up that fullURL
var baseURI = "https://twitter.com/intent/tweet?url=";
var twitterUsername = "stltoday";
var interactiveURL = "http://graphics.########.com/STLblues";
// Randomly generate one of three teasers
var teaser1 = regularName3 + " to " + regularName2 + " back to " + regularName1 + " — GOAL! Create your own all-team #STLBlues team: ";
var teaser2 = "I picked my #STLBlues dream team. See which players I've chosen and build your own: ";
var teaser3 = "My #STLBlues team will skate circles around yours! Pick your team: ";
teasers.push(teaser1);teasers.push(teaser2);teasers.push(teaser3);
// This is the full url that will be switched in and out
// var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teaseRand;
var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teasers[rand];
// It needs to be encoded properly as well
var encodedURL = encodeURIComponent(fullURL)
console.log(fullURL);
console.log(encodedURL);
// Change the href to the link every time the Twitter button is clicked
var changeLink = $("link--twitter").attr("href", encodedURL);
// if (lastNames.length === 6) {
// } else {
// // Generic teaser
// var teaser4 = "Pick your #STLBlues dream team from 50 of the best #StLouisBlues to hit the ice: " + interactiveURL + " (via #stltoday)";
// }
});
I am writing a small application for the user to enter the name of a book and then its price, push the values of those to an array, output the book name and cost to the page and then display the total.
the issue I am having is with the total, for example:
If I write 2 as the value of each of the values, the "totalOutput" says 022222 instead of 10 as I would expect, I have tried a few different things and read a few articles on here but haven't found any to be much help or that useful.
these are the exact lines I am having issues with:
//go over each item in price and add up the total
price.forEach(function addNumber(value) {
total += value;
});
//write the total
totalOutput.innerHTML = "the total value of the books is " + total;
}
And incase you need it - here is my full javascript code:
//Book shop task
function trackBooks() {
//target the output ul and store in a variable
var output = document.getElementById("booksOutput");
//Setup the two arrays to hold the book names and their prices.
var books = [];
var price = [];
//declare a variable for working out the total
var total = 0;
//target the total output
var totalOutput = document.getElementById("totalOutput");
//set up a counter for the loop
var x = 0;
//setup the loop for entering the names of the books and their prices, for the sample, I have set the loop to run 5 times as stated in the pseudo code
for (var i = 0; i < 5; i++) {
//add one to the counter on each loop
x = x + 1;
//declare a variable to ask the user for the book name and the cost and push those values to their arrays we created above
var bookName = prompt("enter book name number " + x);
books.push(bookName);
var bookPrice = prompt("how much does book number " + x + " cost?");
price.push(bookPrice);
//create a variable to create new li tags on output
var newLi = document.createElement("li");
//add the required info to the new link
newLi.innerHTML = "book " + x + ": " + "<strong>" + books[i] + "</strong>" + " costs " + "<strong>" + price[i] + "</strong>";
//write out the name and price to the page
output.appendChild(newLi);
}
//go over each item in price and add up the total
price.forEach(function addNumber(value) {
total += value;
});
//write the total
totalOutput.innerHTML = "the total value of the books is " + total;
}
var bookPrice = prompt("how much does book number " + x + " cost?");
price.push(bookPrice);
prompt returns a string, and when you add a number (0) and a string ("2"), you get a string ("02"). You should cast to number here:
price.push(+bookPrice);
(unary + casts to a number)
You are adding Strings not a Numbers. For example:
"Hello " + "World";
Will output "Hello World".
"10" + "20";
will output "1020"
Instead you have to convert the String to a Number
Number("10") + Number("20");
will ouput 30
To apply this to your code:
price.push(Number(bookPrice));
My code dynamically generates string/number operations. The program dynamically builds something similar to the following:
"My name " + "is " + "G-Man"
"Your age is " + "21"
"5" * "5"
I want to output this:
My Name is G-Man
Your age is 21
25
I can write a library for this, but I currently am under time constraints. If anyone aware of a library that can perform equations similar to above (int + int = int), (string + int = string), etc.?
I think you probably just want to use JavaScript's built in EVAL function.
var a = eval("5 + 5");
console.log(a); // >> 10
EDIT Wow I got 2 down-votes at almost robotic speed when I answered this question ~weird, but again EVAL is probably what you want.
var a = eval("'Your age is ' + '22'");
console.log(a); // >> Your age is 22
EDIT 2 Here's a starting point for doing some quick validation of the input to make sure nothing naughty gets Eval'd.
var test1 = [
"testing"
,"+"
,"123"
];
var test2 = [
"8"
,"*"
,"5"
,"/"
,"3"
];
var test3 = [
"window.alert('bad');"
];
var test4 = [
"\"It's hard to escape things\", said "
," + "
,"Bob"
];
function supereval(arr) {
var sEval = '';
for(var i=0; i<arr.length; i++) {
if(!isNaN(parseFloat(arr[i])) && isFinite(arr[i])) { // number
sEval += arr[i];
//console.log("> number");
} else if( /^\s?[\+\/\*\-]\s?$/i.test(arr[i]) ) { // operation
sEval += arr[i];
//console.log("> operation");
} else { // string
sEval += "\"" + arr[i].replace(/"/g, '\\"') + "\"";
//console.log("> string");
}
}
console.log("DEBUG:" + sEval);
return eval(sEval);
}
console.log(supereval(test1));
console.log(supereval(test2));
console.log(supereval(test3));
console.log(supereval(test4));
I've got a div remaining
remaining.innerHTML = "You have NUMBER remaining SECOND VARIABLE"
I know I can change innerHTML by just doing remaining.innerHTML = "new string" but I'm wondering using Javascript how would I inject a new value for NUMBER into the innerHTML without knowing NUMBER's index or value? However the non-variable contents e.g You have and remaining are constant.
Using string concatenation:
var number = 1;
var secondVariable = 2;
remaining.innerHTML = "You have " + number + " remaining " + secondVariable;
// results in "You have 1 remaining 2";
You can use the replace() method on the string, to replace your markers by their values.
For example, if you use this portion of code :
var str = "You have NUMBER remaining SECOND VARIABLE";
var new_str = str.replace('NUMBER', 150).replace('SECOND VARIABLE', 'plop');
The new_str variable will have as value :
"You have 150 remaining plop"
Which, applied to your specific case, would give something such as this :
remaining.innerHTML = "You have NUMBER remaining SECOND VARIABLE".replace('NUMBER', 150).replace('SECOND VARIABLE', 'plop');
remaining.innerHTML = "You have " + var1 + " remaining " + var2;
Something like
var no1 = 5;
var no2 = 10;
remaining.innerHTML = "You have " + no1 + " remaining " + no2;