get value of string before and after / in javascript - javascript

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]

Related

Input field values not arithmetically summing up

Goodday, please i have a code to calculate the efficiency of a generator. The problem is the input fields all add up until the last variable. If all values were 2+2+3+4 which normally sums up into 11 normally, this program doesn't do that instead it just adds the 4 as in 2+2+3+4 equals 74.
That's the formula for calculating the efficiency of a generator.
$('.efmit').on('click', function efficiency() {
var vI = $('.I').val();
var vV = $('.V').val();
var ia = $('.ia').val();
var If = $('.If').val();
var Ra = $('.Ra').val();
var closs = $('.closs').val();
var vi_combo = vI*vV;
var ias = (ia*ia)*Ra;
var iv = If*vV;
var cent = 100;
var result = vi_combo+ias + iv;
var finalR = result + closs;
window.alert(finalR);
})
jQuery val method like $('.closs').val() returns String type variable not Number type.
You can cast type of variable to solve the problem.
var closs = Number($('.closs').val());
The reason is your program treated your variable as a string
try converting them to integer by parsing them like this parseInt(yourVariable).

GET item from brackets in array javascript

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"

Locating MAX Number in a string for a list of records

I am relatively new to JavaScript and trying to get the following achievement.
Lets say we have the following data records.
Example data records:
A_ID_R1_V1
A_ID_R1_V2
A_ID_R2_V1
Basically I am looking for two results:
1.Create a string based on record A_ID_R1_V2.
Input = A_ID_R1_V2
Output = A_ID_R1_V3
My thoughts about this is so locate the MAX V<#> for A_ID_R1_ and then add 1.
2.Create a string based on record A_ID_R2_V1.
Input = A_ID_R2_V1
Output = A_ID_R3_V1
My thoughts about this is so locate the MAX R<#> for all records and then add 1.
Thanks a lot in advance!!!!!!!!
I have strong feeling that this is some kind of school homework...
Anyhow, this will do the job, but its not sophisticated nor very effective
function updateId(letter){
var pos = (letter === 'V')?3:2;
var parts = id.split('_');
var num = parseInt(parts[pos].substring(1,parts[pos].length));
parts[pos] = letter+(num+1);
id = parts.join('_');
logVal(id);
}
try this with regex: http://jsfiddle.net/mig1098/wh6n2oqL/
newEntry = function(entry){
var str2 = entry.replace(/\d+$/,'');
var num = parseInt(entry.replace(/A_ID_R(\d+)_V/,''));
num = num+1;
return str2+num;
}

Having trouble converting user input into a graphic bar

The code seems to work fine when inputting numbers 1-9 but anything above doesn't work, what could be the issue? Here is the code:
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = varkString[0];
var aural = varkString[1];
var read = varkString[2];
var kinesthetic = varkString[3];
var varkBar = 30*visual
document.writeln('<img src="bar_blue.png" width='+varkBar+' height="25"/>');{
}
Edit: Solved
You are parsing first character when you are getting visual, second on aural and third on read.
I belive that you want to use subStrings
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
when you are slpiting the string varkString the array will automatically constructed and assigned to subStrings.so use it like this:
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];

Length of a string (var)

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.

Categories