When I run the following:
var months = new Set(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
var string = "27 - 28 August 663 CE";
var words = string.split(" ");
for (var i = 0; i < words.length - 1; i++) {
words[i] += " ";
}
var array = words;
array = $.map(array, function(value){
return value.replace(/ /g, '');
});
const dates = {
days : [],
months : [],
years : [],
suffixes : []
}
for (const word of words) {
if (months.has(word)) {
dates.months.push(word);
} else if (+word < 32) {
dates.days.push(+word);
} else if (+word < 2200) {
dates.years.push(+word);
} else if (/\w+/.test(word)) {
dates.suffixes.push(word);
}
}
console.log(array);
console.log(dates);
The output is incorrect:
Object {days: Array(2), months: Array(0), years: Array(1), suffixes: Array(2)}
While if I run:
var months = new Set(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
var words = ["27","-","28","August","663","CE"];
const dates = {
days : [],
months : [],
years : [],
suffixes : []
}
for (const word of words) {
if (months.has(word)) {
dates.months.push(word);
} else if (+word < 32) {
dates.days.push(+word);
} else if (+word < 2200) {
dates.years.push(+word);
} else if (/\w+/.test(word)) {
dates.suffixes.push(word);
}
}
console.log(dates);
The output is correct:
Object {days: Array(2), months: Array(1), years: Array(1), suffixes: Array(1)}
jsFiddle
Cause youre adding a whitespace in your first code, which is completely unneccessary:
words[i] += " ";
And cause of that
"january "
isnt found in the array (or Set), as it only contains:
"january"
Hi you are using the "words" array even "array", and it has withe spaces.
This demo give expected result:
var string = "27 - 28 August 663 CE";
var months = new Set(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
var words = string.split(" ");
for (var i = 0; i < words.length - 1; i++) {
words[i] += " ";
}
const dates = {
days : [],
months : [],
years : [],
suffixes : []
};
var array = words;
array = array.map(array, function(value){
return value.replace(/ /g, '');
});
console.log(array, 'array');
console.log(words);
for (word of array) {
if (months.has(word)) {
dates.months.push(word);
} else if (+word < 32) {
dates.days.push(+word);
} else if (+word < 2200) {
dates.years.push(+word);
} else if (/\w+/.test(word)) {
dates.suffixes.push(word);
}
}
console.log(dates);
Related
I am creating dynamic labels for the chart js by supplying range of the month . It is working good if i selected start month and end month in ascending order but it is not working in the case where i selected start month = december and end month = march.
Here is my code ,
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March']; // here i give the lower and upper limit for the label
for (var i = monthNames.indexOf(monthn[0]); i <= monthNames.indexOf(monthn[1]); i++) {
monthArr.push(monthNames[i]);
}
return monthArr;
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March'];
if (monthNames.indexOf(monthn[1]) < monthNames.indexOf(monthn[0])) {
monthNames.unshift(monthNames.splice(monthNames.indexOf(monthn[0]), 1)[0]);
for (var i = monthNames.indexOf(monthn[0]); i <= diff; i++) {
monthArr.push(monthNames[i]);
}
} else {
for (var i = monthNames.indexOf(monthn[0]); i <= monthNames.indexOf(monthn[1]); i++) {
monthArr.push(monthNames[i]);
}
}
return monthArr;
Try to validate the index between the two months
Here is an example with your code and a very small changes.
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March']; // here i give the lower and upper limit for the label
// make sure that StartMonth is smaller then Endmonth
var startMonth = monthNames.indexOf(monthn[0]) <= monthNames.indexOf(monthn[1]) ? monthNames.indexOf(monthn[0]) : monthNames.indexOf(monthn[1]);
// make sure that EndMonth is bigger then StartMonth
var endMonth = monthNames.indexOf(monthn[0]) <= monthNames.indexOf(monthn[1]) ? monthNames.indexOf(monthn[1]) : monthNames.indexOf(monthn[0]);
for (var i = startMonth; i <= endMonth; i++) {
monthArr.push(monthNames[i]);
}
// Add the missing months
if (startMonth -1 >0)
{
for (var i = 0; i <= startMonth -1; i++) {
monthArr.push(monthNames[i]);
}
}
console.log(monthArr)
I have two array which looks like
var monthNames = [ "January", "January", "January", "April", "April", "December", "August", "August", "November", "November", "November", "December" ];
var monthRange = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
Now I am wondering how should I work in JS so that I would get a new array back, again with 12 elements in it (one for each month) and get a count of it. So it will be like this -
[3, 0, 0, 2, 0, 0, 0, 2, 0, 0, 3, 1]
Here count is in the order of the monthRange which gives count for each month in monthNames .
So here
January : 3,
April: 2,
December: 1,
August: 2,
November: 3,
December : 1
This would do it:
var counts = monthRange.map(function(val) {
var count = 0;
for (var i = 0, j = monthNames.length; i < j; i++) {
if (val === monthNames[i]) count++;
}
return count;
});
You can map over the monthRange and then filter the monthNames to return the number of occurrences of each month:
monthRange.map(function(month) {
return monthNames.filter(function(n) { return n === month }).length;
});
Using underscore you can do it like this:
_.reduce(array, function(memo, month) {
memo[month] = (memo[month] === undefined ? 0 : memo[month]) + 1;
return memo
}, {})
In your case, it can look something like
var countMonths = function(array, m) {
return _.reduce(array, function(memo, month) {
memo[month] = (memo[month] === undefined ? 0 : memo[month]) + 1;
return memo
}, m)
}
var memo = countMonths(monthNames, {})
memo = countMonths(monthRange, memo)
Try Object to keep track of frequency:
var freq = function(list) {
var o = {};
var l = list.length;
var v;
while (v = list[--l])
o[v] = o[v] !== undefined ? o[v] + 1 : 1;
return o;
};
console.log(freq(["January", "January", "January", "April", "April", "December", "August", "August", "November", "November", "November", "December"]));
alert(JSON.stringify(freq(["apple", "mangoe", "apple"])));
Open console...
I have to two dates from and to. I want to get all of the month names between these two dates.
Following is my code
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
}
above code works for following inputs
diff('September 2013', 'December 2013');
but it does not work for this
diff('September 2013', 'February 2014');
How can I make it work?
Mine is better: http://jsfiddle.net/kS73f/8/
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var arr = [];
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var fromYear = datFrom.getFullYear();
var toYear = datTo.getFullYear();
var diffYear = (12 * (toYear - fromYear)) + datTo.getMonth();
for (var i = datFrom.getMonth(); i <= diffYear; i++) {
arr.push(monthNames[i%12] + " " + Math.floor(fromYear+(i/12)));
}
return arr;
}
console.log(diff('September 2013', 'March 2014'));
You're going to have to do a more manual method than slice. Here's a starting point you can determine how to handle cases as mentioned in comments.
function diff(from, to) {
var result = [];
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
if(datFrom < datTo) {
var month = datFrom.getMonth();
var toMonth = datTo.getMonth() + 1 + ((datTo.getYear() - datFrom.getYear())*12); //toMonth adjusted for year
for(; month < toMonth; month++) { //Slice around the corner...
result.push(monthNames[month % 12]);
}
}
return result;
}
diff('September 2013', 'February 2014'); //=["September", "October", "November", "December", "January", "February"]
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr;
if(datFrom > datTo) {
return diff(to, from);
}
var fromYear = datFrom.getFullYear();
var toYear = datTo.getFullYear();
if(fromYear === toYear) {
return monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
} else {
var arr = addYear(monthNames.slice(datFrom.getMonth(), new Date('1 December ' + fromYear)), fromYear);
for(var i = 1; i < (toYear - fromYear); i++) {
arr = arr.concat(addYear(monthNames, fromYear + i));
}
return arr.concat(addYear(monthNames.slice(new Date('1 January ' + fromYear).getMonth(), datTo.getMonth() + 1), toYear));
}
}
function addYear(arr, year) {
var updatedArr = [];
for(var i = 0; i < arr.length; i++) {
updatedArr[i] = arr[i] + ' ' + year;
}
return updatedArr;
}
Than try console.log(diff('September 2013', 'February 2015')) to test it.
The following modifies the original function as little as possible, if that helps the OP from a comprehension standpoint.
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
if (!arr.length) {
arr = monthNames.slice(datFrom.getMonth(), 12);
arr = arr.concat(monthNames.slice(0, datTo.getMonth() + 1));
}
return arr;
}
console.log(diff('December 2013', 'February 2014')); //["September", "October", "November", "December", "January", "February"]
Lazy answer:
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var mFrom = new Date('1 ' + from).getMonth();
var mTo = new Date('1 ' + to).getMonth();
mTo = mTo < mFrom ? mTo + 12 : mTo;
return monthNames.slice(mFrom, mTo + 1);
}
alert(diff('September 2013', 'December 2013'));
alert(diff('September 2013', 'February 2014'));
I am learning JS and am having trouble figuring this out. I would like to ask if any of you can spot my error. I would like to use a while loop to return the names of all of the months january through december without using an array, is this possible? Thank you.
window.onload = function() {
document.getElementById("months").innerHTML = getMonth(11);
};
for(var month=0; month < 11; month++)
function getMonth(month) {
var monthName;
if (month == 11) {
monthName = "December";
}
return monthName;
}
function getMonth()
{
var x="",month=0;
while (month<11)
{
x=x + month + "<br>";
i++;
}
document.getElementById("months").innerHTML=x;
}
http://jsfiddle.net/priswiz/xuJRc/
without an array:
function getMonth(month) {
switch(month){
case 0: return "January";
case 1: return "February";
//...
case 11: return "December";
default: return "Not a valid Month";
}
}
But thats a painful way to do it.
With array:
function getMonth(month){
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
return monthNames[month];
}
I have no idea what you'd want to do with a while loop without an array. What would you be iterating over?
I have an array
var months = ["January", "February", "March", "April", \
"May", "June", "July", "August", "September", "October", \
"November", "December"];
I have strings like "Nov", "October", "Jun", "June", "Sept", "Sep" etc. The point is, the string can be a substring of one of the months.
What is the best way to compare the string to be a substring of the array elements? How do I find out the index of the month?
I am looking at javascript/jQuery.
I know I can just loop through the array checking against each element using search and break when found. I want something better.
var month_index = function(target) {
target = target.toLocaleLowerCase();
return jQuery.inArray(true, jQuery.map(months, function(s) {
return s.toLocaleLowerCase().indexOf(target) > -1;
}))
};
var index_of_october = month_index("oct");
var substr = 'nov', months = ["december", "november", "feb"];
var index = months.indexOf( months.filter(function(v){ return v.indexOf(substr) >= 0;})[0]);
alert(index)
http://jsfiddle.net/KeMe9/
Put your array items in lower case
var txt = "Sep";
var found = false;
txt = txt.toLowerCase();
for (var i = 0; i < 12; i++) {
if (months[i].indexOf(txt) > -1) {
found = true;
break;
}
}