how add number to an array in javascript? [duplicate] - javascript

This question already has answers here:
How to append something to an array?
(30 answers)
Closed 8 years ago.
I am trying to add a number to an array .
code:
var year = mm[2].value+1;
but it results undefined.
can you guys how to do this?

You need to parse the string to an integer before you add to it:
var dt = "12/02/2012";
var mm = de.split("/");
var year = parseInt(mm[2])+1;

Related

how to make a list of numbers in a given string, javascript? [duplicate]

This question already has answers here:
get all numbers in a string and push to an array (javascript)
(2 answers)
Closed 5 years ago.
Here is a string: (1 AND 2) OR 30 AND (4 AND 5).
I need a list of numbers included in this string.
Please help me out.
Use match:
var numbers = "(1 AND 2) OR 30 AND (4 AND 5)"
var formatted = numbers.match(/\d+/g)
console.log(formatted); // [1,2,30,4,5]

Javascript only read certain char from input [duplicate]

This question already has answers here:
How to return part of string before a certain character?
(5 answers)
Closed 6 years ago.
var name = prompt("Enter Name,Barry");
document.write(name);
I want to do exactly this, but only print the first three letters of the 'name'
var name = prompt("Enter Name,Barry").slice(0, 3);
document.write(name)
Try this out it may help.

How to format string in JS? [duplicate]

This question already has answers here:
JavaScript .replace only replaces first Match [duplicate]
(7 answers)
Closed 6 years ago.
I would like to have the result :
28,12,2016
From this string "28/12/2016"
I tried :
("28/12/2016").replace('/',',');
==>"28,12/2016"
I don't know how to delete the second /and the " "
use split and join method
var a="28/12/2016";
var ans=a.split("/").join(",");
console.log(ans);

format phone no is us style eg: (xxx)xxx-xxx [duplicate]

This question already has answers here:
Formatting the phone number
(3 answers)
Closed 6 years ago.
How do I format plain 10 digit phone no. to us style (xxx)-xxx-xxxx using javascript regular expression.
This should do it: ^(\d{3})(\d{3})(\d{4})$
Here is a working example:
var testNumber = '1234567890';
var regex = /^(\d{3})(\d{3})(\d{4})$/;
var sub = '($1)-$2-$3';
var usNumber = testNumber.replace(regex, sub);
alert(usNumber);

parse string and store elements to an array [duplicate]

This question already has answers here:
How to use split?
(4 answers)
Closed 9 years ago.
I have a string likes below
a_b_c_d
I hope to decode it to an array t as
t[0]->a
t[1]->b
t[2]->c
t[3]->d
Just wonder if there ia function of javascript can parse the string directly.
var string = "a_b_c_d";
var t = string.split('_');
DEMO FIDDLE
Just split the string
var t = "a_b_c_d".split("_");

Categories

Resources