Kind of an odd request, I know. But I've been looking for a solution to this for quite a while now. This is the effect I'm looking for:
var myString = "Hello I am randomly capitalized"
Desired function result:
hElLO i aM rAnDOmlY caPiTAlizED
I imagine this is best done with javascript arrays in one way or another. Just looking for some creative ideas. Thank you!
Here's one way
myString.split('').map(function(c){
return c[Math.round(Math.random())?'toUpperCase':'toLowerCase']();
}).join('');
You could add this as a prototype method on the string object for easy access if desired:
String.prototype.toRandomCase = function() {
return this.split('').map(function(c){
return c[Math.round(Math.random())?'toUpperCase':'toLowerCase']();
}).join('');
}
Then access by
console.log(myString.toRandomCase());
A bit of an explanation of how this works:
String.split method is used to split the string into an array of individual characters.
Array.map function is used. This takes a callback function that is applied to each item in an array, and returns a new resulting array with each value returned by the map function.
Inside the map function
Math.round(Math.random()) is used for randomness
The result of that is used with a ternary operator to get toLowerCase or toUpperCase
Math.Round(Math.random())?'toLowerCase':'toUpperCase'
The result of the ternary operator is used to access the relevant function property by array deferencing of the character, then called. c[<ternary here>]()
Finally it uses Array.join method on the result of the map function call to join the resulting array back into a string.
Code golf (efficiency)
RobG's answer has a more efficient approach than mine (please upvote his answer)
String.prototype.toRandomCase = function() {
return this.toLowerCase().split('').map(function(c){
return Math.random() < .5? c : c.toUpperCase();
}).join('');
}
If anyone has suggestions for improving this further - please comment or edit this part of the answer
Well, based on Joel's answer…
myString.toLowerCase().split('').map(function(c){
return Math.random() < .5? c : c.toUpperCase();
}).join('');
Related
I want to access the first two digits of a number, and i have tried using substring, substr and slice but none of them work. It's throwing an error saying substring is not defined.
render() {
let trial123 = this.props.buildInfo["abc.version"];
var str = trial123.toString();
var strFirstThree = str.substring(0,3);
console.log(strFirstThree);
}
I have tried the above code
output of(above code)
trial123=19.0.0.1
I need only 19.0
How can i achieve this?
I would split it by dot and then take the first two elements:
const trial = "19.0.0.1"
console.log(trial.split(".").slice(0, 2).join("."))
// 19.0
You could just split and then join:
const [ first, second ] = trial123.split('.');
const result = [ first, second ].join('.');
I have added a code snippet of the work: (explanation comes after it, line by line).
function getFakePropValue(){
return Math.round(Math.random()) == 0 ? "19.0.0.1" : null;
}
let trial123 = getFakePropValue() || "";
//var str = trial123.toString();
// is the toString() really necessary? aren't you passing it along as a String already?
var strFirstThree = trial123.split('.');
//var strFirstThree = str.substring(0,3);
//I wouldn't use substring , what if the address 191.0.0.1 ?
if(strFirstThree.length >= 2)
console.log(strFirstThree.splice(0,2).join("."));
else
console.error("prop was empty");
Because you are using React, the props value was faked with the function getFakePropValue. The code inside is irrelevant, what I am doing is returning a String randomly, in case you have allowed in your React Component for the prop to be empty. This is to show how you an create minimal robust code to avoid having exceptions.
Moving on, the following is a safety net to make sure the variable trial123 always has a string value, even if it's "".
let trial123 = getFakePropValue() || "";
That means that if the function returns something like null , the boolean expression will execute the second apart, and return an empty string "" and that will be the value for trial123.
Moving on, the line where you convert to toString I have removed, I assume you are already getting the value in string format. Next.
var strFirstThree = trial123.split('.');
That creates an array where each position holds a part of the IP addrss. So 19.0.0.1 would become [19,0,0,1] that's thanks to the split by the delimiter . . Next.
if(strFirstThree.length >= 2)
console.log(strFirstThree.splice(0,2).join("."));
else
console.error("prop was empty");
This last piece of code uses the conditional if to make sure that my array has values before I try to splice it and join. The conditional is not to avoid an exception, since splice and join on empty arrays just returns an empty string. It's rather for you to be able to raise an error or something if needed. So if the array has values, I keep the first two positions with splice(0,2) and then join that array with a '.'. I recommend it more than the substr method you were going for because what if you get a number that's 191.0.0.1 then the substr would return the wrong string back, but with splice and join that would never happen.
Things to improve
I would strongly suggest using more human comprehensible variables (reflect their use in the code)
The right path for prop value checking is through Prop.Types, super easy to use, very helpful.
Happy coding!
So, for my own knowledge, and because I love looking at things from a different perspective...
I have the following bite of Javascript code, which for this problem's intents and purposes will only ever receive strings formatted like this: "wordone wordtwo":
function inName(inputName) {
return inputName.split(" ")[1].toUpperCase();
}
However, this only returns half of what I want ("WORDTWO"). I desire to return the original string with a single change: the space-separated second word returned through the toUpperCase(); and then re-concatenated to the untouched first word.
I also want to unnecessarily run all of the operations on the return line. My brain says this is possible, given how as the compiler reads the line from left to right and makes adjustments to the available member functions based on what has resolved. Also everything in Javascript is an object, correct?
Help me out for my own curiosity's sake, or bash me over the head with my own misconceptions.
Here is a solved version of the above question using 'normal' statements:
function inName(inputName) {
var nameArray=inputName.split(" ");
nameArray[1]=nameArray[1].toUpperCase();
return nameArray.join(" ");
}
One line with substr, indexOf and a variable on the fly ;-)
function inName(inputName) {
return inputName.substr(0, (index = inputName.indexOf(' '))) + inputName.substr(index).toUpperCase();
}
Here's another option which avoids the regular expression:
function inName(inputName) {
return inputName.split(' ').map(function(v,i){return i?v.toUpperCase():v;}).join(' ');
}
This does the same split as the original code, then maps the parts to a function which returns the value at index 0 unchanged but the value at index 1 in upper case. Then the two results are joined back together with a space.
As others have said, a longer, clearer version is better in practice than trying to come up with a clever one-liner. Defining a function inside the return statement feels like cheating anyway ;-)
Something like this almost seems like it belongs on Code Golf, but here's my take:
function inName(inputName) {
return inputName.replace(/ .*/,function(m) {return m.toUpperCase();});
}
Interesting. Here is my take on the problem
function justDoIt(str){
return [str = str.split(" ") , str.pop().toUpperCase()].join(" ");
}
Creates a new array, str is split and reassigned as an array, and the first item of the new array, then the second new array item pops the last word, makes it uppercase, puts it into the new array. Then joins the array [["wordOne"],"WORDTWO"].join(" ")
My question has two parts. I'm trying to check whether a date is a palindrome or not. In the following code, I continuously get the result of "Not a palindrome" even if the string is in fact a palindrome.
function isPalindrome(str){
var rev = str.reverse;
if(str === rev){
console.log("This is a palindrome.");
}
else{
console.log("Not a palindrome");
}
}
isPalindrome("2002");
The second part to my question is: if I wanted the function to take two arguments function isPalindrome(start_date, end_date)and have it check the dates between for palindrome years and then return those years chronologically, how do I do that? I'm not asking for anyone to actually do it for me. I'm just asking for an explanation on how to accomplish it.
Thanks in advance.
It could be something with the reverse function you are using. You could output the value of rev to see what's going one.
I would suggest you use this: How do you reverse a string in place in JavaScript?
I'm not familiar with any string reverse() function in anybody's native javascript implementation. But here's something I wrote a while back that does the palindrome thing, fwiw:
String.prototype.reverse = function (){
//Another way to reverse a string is to use Array's reverse:
// "this.split('').reverse().join('')";
//but that's boring, and recursion is fun!
if (this.length < 2) { return this.toString() };
return this.slice(-1) + this.slice(0,-1).reverse();
}
String.prototype.is_palindrome = function (){
return this.toString() === this.reverse();
}
This checks whether a string is a palindrome.
As for the second part of your question, I'm not sure how to do that off the top of my head. I would start by seeing what's natively available via javascript's Date object. Check MDN. You would only have to handle the years, so I'd just figure out the year range first and iterate over that, checking for palindromes along the way.
Are you stripping out the non-numeric characters first?
var strippedDate = str.replace(/[^\d]/g, "");
return strippedDate == strippedDate.reverse();
well, I am more of a PHP person, and my JS skills are close to none when it comes to any JS other than simple design related operations , so excuse me if I am asking the obvious .
the following operations would be a breeze in PHP (and might also be in JS - but I am fighting with unfamiliar syntax here ...)
It is some sort of input validation
var ar = ["BRS201103-0783-CT-S", "MAGIC WORD", "magic", "Words", "Magic-Word"];
jQuery(document).ready(function() {
jQuery("form#searchreport").submit(function() {
if (jQuery.inArray(jQuery("input:first").val(), ar) != -1){
jQuery("#contentresults").delay(800).show("slow");
return false;
}
This question has 2 parts .
1 - how can I make it possible for the array to be case insensitive ?
E.g. - BRS201103-0783-CT-S will give the same result as brs201103-0783-ct-s AND Brs201103-0783-CT-s or MAGIC magic Magic MaGIc
basically i need something like ignoreCase() for array , but I could not find any reference to that in jQuery nor JS...
I tried toLowerCase() - but It is not working on the array (ittirating??) and also, would it resolve the mixed case ?
2 - How can I make the function to recognize only parts or
combinations of the elements ?
E.g. - if one types only "word" , I would like it to pass as "words" , and also if someone types "some word" it should pass (containing "word" )
Part 1
You can process your array to be entirely lowercase, and lowercase your input so indexOf() will work like it's performing a case insensitive search.
You can lowercase a string with toLowerCase() as you've already figured out.
To do an array, you can use...
arr = arr.map(function(elem) { return elem.toLowerCase(); });
Part 2
You could check for a substring, for example...
// Assuming you've already transformed the input and array to lowercase.
var input = "word";
var words = ["word", "words", "wordly", "not"];
var found = words.some(function(elem) { return elem.indexOf(input) != -1; });
Alternatively, you could skip in this instance transforming the array to be all lowercase by calling toLowerCase() on each elem before you check indexOf().
some() and map() aren't supported in older IEs, but are trivial to polyfill. An example of a polyfill for each is available at the linked documentation.
As Fabrício Matté also pointed out, you can use the jQuery equivalents here, $.map() for Array.prototype.map() and $.grep() with length property for Array.prototype.some(). Then you will get the browser compatibility for free.
To check if an array contains an element, case-insensitive, I used this code:
ret = $.grep( array, function (n,i) {
return ( n && n.toLowerCase().indexOf(elem.toLowerCase())!=-1 );
}) ;
Here is a fiddle to play with
array match case insensitive
Is there an alternative, faster method of returning the position/index of part of an array within another array (where multiple values match)? It's called a lot within my pathfinding algorithm so could do with being as fast as possible.
My current function is:
// Haystack can be e.g. [[0,1,278.9],[4,4,22.1212]]
function coordinate_location_in_array(needle,haystack){
for(n in haystack){
if(haystack[n][0]==needle[0] && haystack[n][1]==needle[1]) return n;
}
return false;
}
// Needle of [0,1]: returns 0
// Needle of [4,4]: returns 1
// Needle of [6,7]: returns false
Edit:
I've been messing around a bit and come up with a (rather ghastly) string manipulation-based method (thereby avoiding the costly for loop). I think it's still slightly slower. Could anybody benchmark these methods?
function coordinate_location_in_array(needle,haystack) {
var str1 = ':' + haystack.join(':');
var str2 = str1.replace(':'+needle[0]+','+needle[1],'*').split('*')[0];
if(str2.length == str1.length) return false;
var preceedingElements = str2.match(/:/g);
return preceedingElements!=null?preceedingElements.length:0;
}
Perhaps with some improvements this second method might provide some performance gain?
Edit 2:
Bench marked all 3 described methods using jsperf.com (initial method is fastest):
http://jsperf.com/finding-matched-array-within-array/3
Edit 3:
Just replaced the for(..in..) loop with a for(..;..;..) loop (since I know that the haystack array will never have "gaps") and performance seems to have significantly improved:
function coordinate_location_in_array(needle,haystack){
for(var n=0;n<haystack.length;n++){
if(haystack[n][0]==needle[0] && haystack[n][1]==needle[1]) return n;
}
return false;
}
I've updated the jsperf page to include this latest method.
If the "haystack" isn't sorted then there isn't a way to make it faster. Not knowing how the elements in a collection are ordered makes finding something out of it linear by nature, because you just have to check each thing.
If you are using this function over the same "haystack" over and over, you could sort the collection, and use the sorting to make it quicker to find the "needle" (look up different sorting and search algorithms to find one that fits your need best, such as using binary search to find the "needle" after haystack is sorted.)
i don't know if its faster, but you can do something like:
[1,2,3,4].slice(0,2).toString() == [1,2].toString()
in your case it would be:
function coordinate_location_in_array(needle,haystack){
for(n in haystack){
if(haystack[n].slice(0,2).toString() == needle.toString()) return n
}
return false;
}
Also found this post, which covers comparison of JS arrays: compare-two-arrays-javascript-associative
Cheers
Laidback
Using a for(..;..;..) loop rather than a for(..in..) loop made the biggest difference.
(See Edit 3 at the end of the question)
Seems to me this is just a substring search but with numbers instead of characters being the components of the string. As such, Boyer-Moore could be applicable, especially if your needles and haystacks get big.