Add commas to input and output - javascript

I have been trying to get the input to be considered as currency and have commas. I have the output showing currency with a decimal but it is missing the comma. I have tried to get these two things working together but I keep running into errors. Any help is appreciated!!
function calculate(){
var savingsVal = ($('#savingsVal').val());
var totalSavings = 0;
if (filterValues(savingsVal)) {
totalSavings = savingsVal * 0.15;
}
$('#totalSavings').val('$' + totalSavings);
}
function filterValues(eVal){
if (eVal.length == 0)
{
$('.errorMessage').text('Insert value');
$('.equipValCont').addClass('has-error');
return false;
}
else if(!$.isNumeric(eVal)){
$('.errorMessage').text('Please, enter only numbers');
$('.equipValCont').addClass('has-error');
return false;
} else{
$('.equipValCont').removeClass('has-error');
return true;
}
}
$('#calculator').submit(function(e){
e.preventDefault();
calculate();
});

With numeral.js this is as simple as:
var string = numeral(1000).format('0,0');
Not sure if you're opposed to using a library here, but numeral.js is wonderful.

For your validation, instead of checkingelse if(!$.isNumeric(eVal)){ use regex to validate your value. You must consider "," as a valid since your currency will allowed "," . below as sample
var currencyStr= "1,123.20";
if (/^\d{0,4}(\.\d{0,2})?$/.test(currencyStr))
alert("currency is valid");

you should change
if (/^\d{0,4}(\.\d{0,2})?$/.test(currencyStr))
alert("currency is valid");
to
if (/^(\d{0,3})(,\d{0,3})*(\.\d{0,2})?$/.test(currencyStr))
alert("currency is valid");
I hope this helps you
UPDATE
if you want add the correct commas to output, try something like this:
function formatStr( str ) {
str = str.replace( /,/g, "" );
var result = str.match( /^([^\.]+)(\.\d{0,2})?$/ ),
newStr = "",
integerStr = result[1] ? result[1] : "",
floatStr = result[2] ? result[2] : "",
j = 0;
for ( var i = integerStr.length - 1; i >= 0; i-- ) {
if ( j == 3 ) {
newStr = integerStr[i] + "," + newStr;
j = 1;
}
else {
newStr = integerStr[i] + newStr;
j++;
}
}
newStr = newStr + floatStr;
console.log( newStr );
return newStr;
}
formatStr( "12.34" );
formatStr( "123" );
formatStr( "1234.56" );
formatStr( "12345.6" );

Related

How do I write a function that converts all consonant strings at the start of each word in a sentence into an "r" until it runs into a vowel?

SCOOBY DOO CHALLENGE
You are required to write a function that replaces all consonants at
the start of a word until it runs into a vowel with an "r". For
example, the word "scooby" would become "rooby" and “xylophone” would
become “rophone”
This is the function that I'm using but it only operates on a single word in my sentences. How can I make it operate on every word.
function scooby_doo () {
if (text.value.length == 0) {
result.value = "no input given!";
} else if (text.value.length > 0) {
var set = text.value.split(" ");
for (i = 0; i < set.length; i++) {
result.value = set[i].replace (/^[^aeiou]+/i, "r");
}
 } else {
result.value = "";
}
return;
}
I believe you could achieve that with no explicit loops:
str = 'scooby doo loves his xylophone show';
str.replace(/(^| |\n)[^aeiou]+/ig, '$1r');
// outputs "rooby roo roves ris rophone row"
Is this what you are looking for as the output?
I think that in your code you override the previous value when you use :
result.value = set[i].replace ( /^[^aeiou]+/i, "r" );
I dind't know the type of result but you can try this solution:
function scooby_doo (text) {
var result = [];
if ( text.length == 0 )
{
return 'no input given';
} else if ( text.length > 0 ) {
var set = text.split ( " " );
for ( i = 0; i < set.length; i++ ) {
result.push(set[i].replace ( /^[^aeiou]+/i, "r" ));
}
} else {
return '';
}
return result.join(' ');
}
console.log('scooby xylophone'):
"rooby rophone"

How can I uppercase names with a dash -, apostrophe ' or space?

I have the following code which works well to convert data entered into the "Firstname" field in our data enrollment software application to uppercase and return the converted value back to the application.
However, it doesn't handle names with "-", "'" or spaces in them, for example Anne-Marie, Jean Jacques, O’Brian. Could someone please help me in adding a few lines of code to handle these name types as well as preserving my original code which works for standard names without these characters in? Here is my code.
var tc_event = changeValue();
function changeValue() {
// Parse the JSON string for script information.
var tcInfo = JSON.parse(TC_Info);
/* FROM ENGINEERING: The “TC_Info” variable contains the user id and IP address of the user running the script.
* We have at least one customer that wanted that information */
var userId = tcInfo.userId;
var ipAddress = tcInfo.ipAddress;
// Parse the JSON string for fields and properties.
var tcData = JSON.parse(TC_Event);
// The following several lines of code loops over the workflow fields passed in to the script and saves references to the fields named “Lastname” and “LastnameUppercase”
var Lastname, LastnameUppercase, Firstname, Firstname1stUppercase;
// Iterate through parsed JSON.
for (var index in tcData) {
// Fetch each field i.e each key/value pair.
var field = tcData[index];
// Find the fields to process.
if (field.name === 'Lastname') {
Lastname = field;
} else if (field.name === 'LastnameUppercase') {
LastnameUppercase = field;
} else if (field.name === 'Firstname') {
Firstname = field;
} else if (field.name === 'Firstname1stUppercase') {
Firstname1stUppercase = field;
} else if (field.name === 'PersNr') {
PersNr = field;
} else if (field.name === 'TikNr') {
TikNr = field;
}
}
// Were the fields found? If so, proceed.
if (Lastname && LastnameUppercase && Firstname && Firstname1stUppercase && PersNr && TikNr) {
// This line of code states the LastnameUppercase field value will be the Lastname field value in uppercase
LastnameUppercase.value = Lastname.value.toUpperCase();
Firstname1stUppercase.value = Firstname.value.charAt(0).toUpperCase() + Firstname.value.slice(1);
var strLtr = PersNr.value.substring(0, 2);
var strNum = PersNr.value.substring(2, 6);
if (strLtr === '00') {
strLtr = 'A';
} else if (strLtr === '01') {
strLtr = 'M';
} else if (strLtr === '31') {
strLtr = 'B';
} else if (strLtr === '71') {
strLtr = 'F';
}
TikNr.value = strLtr + strNum;
}
// Return the updated fields and properties.
return JSON.stringify(tcData);
}
This will capitalize both the firstName that do not contain symbols and the ones that do:
function capitalize(name) {
let capitalizedName = '';
const nameSplit = name.split(/\W/g);
const symbols = name.match(/\W/g);
for(let i = 0; i< nameSplit.length; i++) {
capitalizedName += nameSplit[i][0].toUpperCase() +
nameSplit[i].slice(1)
if(i < nameSplit.length -1) capitalizedName += symbols[i];
}
return capitalizedName
}
I have used this function successfully:
function capitalizeName(str) {
var result = str.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); });
return result.replace(/\s\s+/g, ' ');
}
calling the function:
capitalName = capitalizeName(lowerCaseName)
Looks like you should change
Firstname1stUppercase.value = Firstname.value.charAt(0).toUpperCase() + Firstname.value.slice(1);
to
var delimiter = ''; //char value
if(Firstname.value.indexOf(' ') != -1){ //name has a space
delimiter = ' ';
}else if(Firstname.value.indexOf('-') != -1){ //name has -
delimiter = '-';
}else if(Firstname.value.indexOf('\'') != -1){ //name has a '
delimiter = '\'';
}
Firstname1stUppercase.value = Firstname.split(delimeter).map(function(val) {
return val.charAt(0).toUpperCase() + val.slice(1);
}).join(delimeter);
The last line is what you were doing but written for any separating character be it a space, apostrophe, or hyphen.
You could split by non alphabetic letters, like this:
text.split(/[^A-Za-z]/);
inspired from here: Split string by non-alphabetic characters
Now, let's implement the function you need:
function myUpperCase(input) {
var parts = input.split(/[^A-Za-z]/);
var output = parts[0];
for (var i = 1; i < parts.length; i++) {
if (parts[i].length) parts[i] = parts[i][0].toUpperCase() + parts[i].substring(1);
output += input[output.length] + parts[i];
}
return output;
}

display the recursion line by line

I am trying to make a function in javascript that would expand/split a string with dashes and show the process ( line by line ) using recursion.
for example, the string "anna" would become:
expand("anna") = expand("an")+"---"+expand("na") ->
"a"+"---"+"n"+"---"+"n"+"---"+"a"
and the desired output would be:
anna
an---na
a---n---n---a
I have achieved doing the following so far (I know it might not be the solution I am looking):
expand("anna") = an+"---"+expand("na")
= an+"---"+n+"---"+expand("a");
= an+"---"+n+"---+"a"
the output I am getting is:
an---n---a
I can't seem to concatenate the head though to do the first example.
My javascript function of expand is as follows:
function expand(word) {
if (word.length<=1) {
return word;
} else {
mid = word.length/2;
return word.substr(0,mid) + " " + expand(word.substr(mid,word.length));
}
}
document.write(expand("anna"));
I would need some tips to do this, otherwise (if it's the wrong stackexchange forum), please guide me where to post it.
this is my crazy attempt
var Word = function(str) {
this.isSplitable = function() {
return str.length > 1;
}
this.split = function() {
var p = Math.floor(str.length / 2);
return [
new Word(str.substr(0,p)),
new Word(str.substr(p,p+1))
];
}
this.toString = function() {
return str;
}
}
var expand = function(words) {
var nwords = [];
var do_recur = false;
words.forEach(function(word){
if(word.isSplitable()) {
var splitted = word.split();
nwords.push(splitted[0]);
nwords.push(splitted[1]);
do_recur = true;
}else{
nwords.push(word);
}
});
var result = [];
nwords.forEach(function(word){
result.push( word.toString() );
});
var result = result.join("--") + "<br/>";
if(do_recur) {
return result + expand(nwords);
}else{
return "";
}
}
document.write( expand([new Word("anna")]) );
This is what you need
expand = function(word) {
return [].map.call(word, function(x) {return x+'---'}).join('')
};
The joy of functional programming.
And with added code to deal with last character:
function expand(word) {
return [].map.call(word, function(x, idx) {
if (idx < word.length - 1)
return x+'---';
else return x
}).join('')
}
As I said that it is impossible to display the "process" steps of recursion while using recursion, here is a workaround that will output your desired steps:
var levels = [];
function expand(word, level) {
if (typeof level === 'undefined') {
level = 0;
}
if (!levels[level]) {
levels[level] = [];
}
levels[level].push(word);
if (word.length <= 1) {
return word;
} else {
var mid = Math.ceil(word.length/2);
return expand(word.substr(0, mid), level+1) + '---' + expand(word.substr(mid), level+1);
}
}
expand('anna');
for (var i = 0; i < levels.length; i++) {
console.log(levels[i].join('---'));
}
to see all steps the best that I whold do is:
function expand(word) {
if (word.length<=1) {
return word;
} else {
var mid = word.length/2;
var str1 = word.substr(0,mid);
var str2 = word.substr(mid,word.length);
document.write(str1 + "---" + str2 + "<br></br>");
return expand(str1) + "---" + expand(str2);
}
}
document.write(expand("anna"));
You have to return the two parts of the string:
function expand(word) {
output="";
if (word.length<=1) {
output+=word;
return output;
} else
{
var mid = word.length/2;
output+=word.substr(0,mid)+"---"+word.substr(mid)+" \n";//this line will show the steps.
output+=expand(word.substr(0,mid))+"---"+expand(word.substr(mid,word.length-1))+" \n";
return output;
}
}
console.log(expand("anna"));
Edit:
I added the output var and in every loop I concatenate the new output to it.
It should do the trick.
Hope the problem is in your first part. According to your algorithm, you are splitting your string anna in to two parts,
an & na
so you need to expand both parts until the part length is less than or equal to one. so your required function is the below one.
function expand(word) {
if (word.length<=1) {
return word;
} else {
mid = word.length/2;
return expand(word.substr(0,mid)) + " --- " + expand(word.substr(mid,word.length));
}
}
document.write(expand("anna"));

Add Comma Function in JavaScript is Too Greedy

How can I modify the function below to make it stop adding commas once we hit the decimal marker?
addCommas = function(number) {
if(number === undefined) {
return '';
}
while(/(\d+)(\d{3})/.test(number.toString())) {
number = number.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return number;
}
addCommas(0.123456); // Outputs 0.123,456, should output 0.123456
My method is to split the number into fractional and integer parts...
addCommas = function(number)
{
if (number === undefined)
return '';
var parts = number.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
document.write( addCommas(0.123456) + "<br />" );
document.write( addCommas(123456.123456) + "<br />" );
document.write( addCommas(123456) + "<br />" );
Just add ^[^.]* to the front of the pattern:
addCommas = function(number) {
if(number === undefined) {
return '';
}
while(/^[^.]*(\d+)(\d{3})/.test(number.toString())) {
number = number.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return number;
}
You only have to do that for the .test() call.
I would break the string in two prior to using the regular expression. Such as:
RenanCommas = function(number) {
if(number === undefined) {
return '';
}
var parts = (number + "").split(".");
var integerPart = parts[0];
var decimalPart = parts.length > 1 ? parts[1] : "0";
while(/(\d+)(\d{3})/.test(integerPart.toString())) {
integerPart = integerPart.replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return integerPart + "." + decimalPart;
}
Notice that you could also have decimalPart as an empty string. In that case, you could check whether or not it is empty before using it. If you use an empty string instead of "0", the last line would be something like:
return integerPart + (decimalPart ? ("." + decimalPart) : "");
And when you run it:
RenanCommas(12121241243.1123131232); // outputs "12,121,241,243.112312"
This seems to be working:
addCommas = function(number) {
return number.replace(/(\.\d+$)|(?!^)(?=(?:\d{3})+(?=\.|$))/g, function($0, $1) {
return ($1)?$1:','; });
}
Examples:
addCommas('0.123456');
"0.123456"
addCommas('987654.123456');
"987,654.123456"
addCommas('987654321');
"987,654,321"

How to replace only first sequential occurences (fuzzymatch)?

I'm trying to write "fuzzy" match and I can't find a way to solve this problem:
Data in: makrusakkk, query: mrk, expected result: <b>m</b>ak<b>r</b>usa<b>k</b>kk.
RegExp: "makrusakkk".match(/(m).*?(r).*?(k)/i) returns ["makrusak", "m", "r", "k"].
So the question is: is there a way to get the expected result using RegExp?
I think using regular expression for such problem makes things just more complicated. The following string and loop based solution would lead to the result:
function fuzzySearch(query, input) {
var inds = patternMatches(query, input);
if(!inds) return input;
var result = input;
for(var i = inds.length - 1; i >= 0; i--) {
var index = inds[i];
result = result.substr(0,index) +
"<b>" + result[index] + "</b>" +
result.substr(index+1);
}
return result;
}
function patternMatches(query, input) {
if(query.length <= 0) {
return [];
} else if(query.length == 1) {
if(input[0] == query[0]) return [0];
else return [];
} else {
if(input[0] != query[0])
return false;
var inds = [0];
for(var i = 1; i < query.length; i++) {
var foundInd = input.indexOf(query[i], inds[i-1]);
if(foundInd < 0) {
return [];
} else {
inds.push(foundInd);
}
}
return inds;
}
}
var input = "makrusakkksd";
var query = "mrk";
console.log(fuzzySearch(query, input));
console.log(patternMatches(query, input));
Here's a live demo too: http://jsfiddle.net/sinairv/T2MF4/
Here you will need for:
function search_for_it(txt, arr){
for(i=0;i<arr.length;i++){
var reg = new RegExp(arr[i], "i");
txt = txt.replace(reg, "<b>"+arr[i]+"</b>");
}
return txt;
}
search_for_it("makrusakkk", ["m","r","k"]);
//return "<b>m</b>a<b>k</b><b>r</b>usakkk"
PS: Your expected result is incorrect. There is a k after the first a.
is there a way to get an expected result using RegExp?
There is.
"makrusakkk".replace(/(m)(.*?)(r)(.*?)(k)/i, '<b>$1</b>$2<b>$3</b>$4<b>$5</b>'​​​​​​​)
I feel vaguely dirty for this, but...regardless; here's one way to do it:
$('#s').keyup(
function(e) {
var w = e.which;
if (w == 8 || w == 46) {
return false;
}
var listElems = $('ul:first li'),
search = $(this).val().replace(/w+/g, ''),
r = search.split(''),
rString = [];
$.each(r, function(i, v) {
rString.push('(' + v + ')');
});
var reg = new RegExp(rString.join('(\\d|\\D)*'), 'gi');
listElems.each(
function() {
if (!$(this).attr('data-origtext')) {
$(this).attr('data-origtext', $(this).text());
}
$(this).html($(this).attr('data-origtext').replace(reg, '<b>$&</b>'));
});
});​
JS Fiddle demo.
It could, almost certainly, benefit from quite some simplification though.
References:
attr().
:first selector.
join().
keyup().
push().
RegExp().
replace().
split().
text().
val().

Categories

Resources