I've tried without success to get the length of the "time" (hours, value is 1-24):
var time2 = Math.floor(startTimeT);
Try 1:
var startTimeTC1L = time2.length;
Try 2:
var startTimeTC1L = time2.toString().length;
alert(startTimeTC1L);
I just want to get a "1" or "2" as returned value (0-9 == 1, 10-24 == 2), but it's not working?
You can simply use, Assuming startTimeT is a number. Converts to string then simply use lenght property
var startTimeT = 15;
var strVar = startTimeT+''; //Convert to string
var length = strVar.length;
alert(length)
Your 2nd try works,
here http://jsfiddle.net/naeemshaikh27/144ex55c/
Here is another much simple approach http://jsfiddle.net/naeemshaikh27/144ex55c/1/
because you just want to get 1 or 2 you can simply use
var time2 = Math.floor(24);
alert(time2);
if(time2<10)
{
startTimeTC1L=1;
}
else{
startTimeTC1L=2;
}
alert(startTimeTC1L);
Use as
var startTimeT = 15554;
var len = startTimeT.toString().length;
alert(len)
Demo
Assuming that you are working with date, how about something like this:
Paste it in firebug's console or use nodejs on the command line.
var hours = ""+(new Date()).getHours();
hours[0] === "0" ? console.log(1) : console.log(hours.length)
I hope it helps.
Related
var result="stackoverflow";
var string="22342st";
if(more than 3 letters)
{
var new=data;
}
1)how I can get variable if only have more than
using match() (function ) in javascript
2)what if I do getting variable in for time delay
some (because of TCP connection)
Does anyone have an idea?
you can use .length property to check string length is greater than 3 or not.
var result = "stackoverflow";
if(result.length > 3)
{
//Your business logic goes here
}
if you want to check only character counts in string is greater than three then you can try below code
var input = "45749ce";
var result = input.match(/[a-zA-Z]/gi);
if(result.length > 3)
{
//Your business logic goes here
console.log(`${result} length is greater than 3`);
}
That should work
var myRegEx = /[a-zA-Z(.+)]{3}/g;
var myString="22342st";
if(myString.match(myRegEx)!==null)
{
}
Also
myString.match(myRegEx)
will return an array of matched values, so you can work with them if needed.
To find the more than three letter in variable using match you have to need regex
([a-zA-Z0-9)]){3,}
code how looks like
var result="stackoverflow";
var string="22342st";
var res = string.match(/([a-zA-Z0-9)]){3,}/g);
if(res!=null){
//here what you want
}
In the following function, I don't understand why the counter function only fires once (the figure goes up by a single increment, I want it to count up to homeFigTwo).
function effectFour() {
var homeFigOne = parseFloat($('.home .figure').text());
var homeFigTwo = 23.99;
var plusFigOne = parseFloat($('.home-plus .figure').text());
var plusFigTwo = 28.49;
var homeInc = homeFigOne < homeFigTwo ? .01 : -.01;
var plusInc = plusFigOne < plusFigTwo ? .01 : -.01;
function counterOne(){
if (homeFigOne === homeFigTwo){
return
}else{
homeFigOne = (homeFigOne + homeInc).toFixed(2);
$('.home .figure').text(homeFigOne);
window.setTimeout(counterOne, 100);
}
}
counterOne();
}
This can be seen in context here: http://codepen.io/timsig/pen/NdvBKN.
Many thanks for any help.
toFixed() has a Return value of
A string representing the given number using fixed-point notation.
This means that on the second time that this happens:
homeFigOne = (homeFigOne + homeInc).toFixed(2);
What's really going on is: "16.00" = "16.00" + 0.01 which, in fact, does not possess a toFixed method, as that whole sentence is what.
So what you want is to parseFloat the result of homeFigOne again, because whenever you toFixed it you set it to a string again.
homeFigOne = (parseFloat(homeFigOne) + homeInc).toFixed(2)
Your recursion is working as expected, but on your second call an error is thrown. This is because you convert homeFigOne to a string by using toFixed.
So it basically does this:
first call: values are 15.99 23.99 (both numbers)
second call: values are "16.00" 23.99 (a string and a number)
As the toFixed method is not defined for Strings an exception is thrown. As this happens async in a anonymous function, you prob. didn't noticed.
So my suggestion is to first make the increment, and only cast for your html element:
function effectFour() {
var homeFigOne = parseFloat($('.home .figure').text());
var homeFigTwo = 23.99;
var plusFigOne = parseFloat($('.home-plus .figure').text());
var plusFigTwo = 28.49;
var homeInc = homeFigOne < homeFigTwo ? .01 : -.01;
var plusInc = plusFigOne < plusFigTwo ? .01 : -.01;
function counterOne(){
if (homeFigOne === homeFigTwo){
return
}else{
homeFigOne = homeFigOne + homeInc;
$('.home .figure').text(homeFigOne.toFixed(2));
window.setTimeout(counterOne, 100);
}
}
counterOne();
}
edit:
+ as you are dealing with floats you are better of with >= instead of === for your end criterium
I need some help. Here I have a string.
n[0] = '3(10)';
The task is to get only 10 from brackets. How to do it in javascript?
You can solve this with Regex :
This will do :
var a= '3(10)'.match(/\((.*?)\)/)
alert(a[1]) ;//10
The captured group will appear in the second index of the array (1)
Regarding your other comment/question :
I have a[0] = '3(10,5) 7(9,4)'; 10 and 9 are chances the task is to
get the number (3 or 7) with a bigger chance (10)
var finalNumber=-1;
var finalChance=-1;
var a = '3(10,5) 7(9,4)';
var m=a.match(/(\d+?)\((\d+?)\,/g);
for (var i=0;i<m.length;i++)
{
var number=m[i].match(/(\d+)\(/)[1]
var chance=m[i].match(/\((\d+)\,/)[1]
if (+chance>+finalChance)
{
finalChance=chance;
finalNumber=number;
}
}
console.log(finalNumber)
Jsbin
Use split() function to split your string with brackets two times :
var first_split = n[0].split(')')[0]; //first_split will return "3(10"
var result = first_split.split('(')[1]; //second split will return "10";
//To reduce the code you can do it in 1 line like this
var result = n[0].split(')')[0].split('(')[1]; // result = "10"
suppose i have 10 localstorage like blow:
localStorage.setItem("item01","this is a value/100");
localStorage.setItem("item02","this new is a value/110");
localStorage.setItem("item03","this is a good value/120");
localStorage.setItem("item04","this is a nice value/130");
I need a java script code to check if the key of for example item01 is not 0 then put the data of item01 before / to xyz and the data after / to rfv variable.
I would suppose you split the data-string along the character "/". Something like:
var lsData = localStorage.getItem("item01");
var dataArray = lsData.split("/");
if(dataArray.length === 2){
var xyz = dataArray[0];
var rfv = dataArray[1];
}
else{
... error-code (could not split exactly)...
}
This is an answer to your question "get value of string before and after / in javascript".
Like I stated in the comments you can split a string into an array of substrings using a delimiter.
jsfiddle
var str= "this is a value/100";
var temp=str.split("/");
var xyz = temp[0]; //->this is a value
var rfv = temp[1]; //->100
alert('xyz = '+xyz+'\nrfv = '+rfv);
this should do it...
I'm assuming you know how to get the localStorage value, so I'm just posting an example.
var a = "this is a value/100"
var b = /^(.*?)\/(\w+)/.exec(a);
var text = b[1]
var value = b[2]
I don't get it.
I can't increment the Tweet-ID ...
Here is a demo: http://jsbin.com/idupoq/1/edit
glb = {};
glb.lastTweetId = 0;
getTweets();
function getTweets()
{
console.info('# LAST ID');
console.log(glb.lastTweetId);
console.info('# TEST 1');
glb.lastTweetId++;
console.log(glb.lastTweetId);
console.info('# TEST 2');
glb.lastTweetId = glb.lastTweetId+1;
console.log(glb.lastTweetId);
console.info('# TEST 3, OK IS INT BUT PARSE AGAIN ');
glb.lastTweetId = parseInt(glb.lastTweetId);
glb.lastTweetId++;
console.log(glb.lastTweetId);
$.getJSON('http://search.twitter.com/search.json?q=%23wwm&since_id='+glb.lastTweetId+'&include_entities=true&result_type=mixed&lang=de&callback=?', function(data, textStatus)
{
if(data.results.length > 0)
{
glb.lastTweetId = data.results[0]['id'];
}
glb.tm= setTimeout('getTweets();',5000);
});
}
Thanks in advance!
This happens because the received ID is out of range of Number format, e.g.
271567725082578940 + 1 = 271567725082578940
You should use special libraries to work with large numbers. Some examples:
https://github.com/jtobey/javascript-bignum
http://jsfromhell.com/classes/bignumber
As others have said already, it is because of Number cannot express 271567725082578941. If all you ever want to do to this number is to increase it by one, then the following function should be all you need:
function stringInc(v){
var digits = v.toString().split('');
var i = digits.length-1;
while (digits[i]==9 && i>0){
digits[i] = 0;
i--;
}
digits[i] = 1+parseInt(digits[i]);
return digits.join('');
}
If you expect to want to do something more with the number, then you might be better off using a BigNumber library as suggested by VisioN.
Either way, you should note that you cannot read the tweet id from data.results[0]['id'], because that is interpreted as a Number and rounded to 271567725082578940. You need to use data.results[0]['id_str'].
See updated jsbin here: http://jsbin.com/idupoq/19/. Notice the console is logging the result from the server:
...
"geo":null,
"id": 271580395022217200,
"id_str":"271580395022217216",
"iso_language_code":"de"
...
So the value 271567725082578940 that you have been observing is incorrect as well.
Dirty but short
http://jsbin.com/idupoq/18/edit
glb.lastTweetId = ''+data.results[0]['id']+'';
var lastTwoDig = parseInt(glb.lastTweetId.substr(glb.lastTweetId.length-2));
var startDigit = glb.lastTweetId.substring(0, glb.lastTweetId.length-2);
lastTwoDig++;
if(lastTwoDig==01){ lastTwoDig = '01'; }
console.log(glb.lastTweetId);
console.log(' '+startDigit+''+lastTwoDig+' ');