1

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

7
  • Youre not pushing an array. Commented Aug 1, 2017 at 11:55
  • I am not getting what are you looking for. Commented Aug 1, 2017 at 11:55
  • @AnuragSinghBisht the output of the first code should be correct as per the output of the second code Commented Aug 1, 2017 at 11:55
  • @Jonasw hey man, what do you mean I am not pushing an array? words is an array pushing into the array `dates' Commented Aug 1, 2017 at 11:56
  • What does this supposed to do?: words[i] += " "; Commented Aug 1, 2017 at 11:56

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

5 Comments

Yep, that's what I thought.
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
@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.
what about this then array = $.map(array, function(value){ return value.replace(/ /g, ''); }); means it isn't working right?
@rob.m it is working. Array doesnt contain the whitespace youve added right before. However words does and were talking about words here.
0

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);

1 Comment

No need for jQuery... array = array.map(word => word.replace(/ /g, '') ), job done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.