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)}
2 Answers 2
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"
answered Aug 1, 2017 at 11:57
Jonas Wilms
139k20 gold badges164 silver badges164 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
zer00ne
Yep, that's what I thought.
rob.m
Ok yeah just tried and works, will accept. Thanks. But why is it adding white space to months if I am doing it on words? That's why that map bit to remove white spaces, I didn't think that space was being added to months
Jonas Wilms
@rob.m youre adding it to words. But because of that the word isnt found in the months set, so months.has(word) is false.
rob.m
what about this then
array = $.map(array, function(value){ return value.replace(/ /g, ''); }); means it isn't working right?Jonas Wilms
@rob.m it is working. Array doesnt contain the whitespace youve added right before. However words does and were talking about words here.
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);
answered Aug 1, 2017 at 12:00
Álvaro Touzón
1,2301 gold badge8 silver badges21 bronze badges
1 Comment
Jeremy Thille
No need for jQuery...
array = array.map(word => word.replace(/ /g, '') ), job donelang-js
wordsis an array pushing into the array `dates'words[i] += " ";