Skip to content

Conversation

miguerurso
Copy link

No description provided.

Comment on lines 10 to 13
let testPhoneNumber = function(phoneString){
console.log(globalRegex.test(phoneString));
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, here we have a function for the reusable code . . . however it does not return any values! It simply triggers a side effect of calling console.log(). So later if you want the value that regex text returns, you'll just get undefined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you code on lines 28 and 29 . . . they might console log from within the function, but the console logs themselves (as in the ones called on 28 and 29) are undefined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So first fix is to make this function return a value, not console log it.

phoneNumber.js Outdated

const phoneNumberObject = {areaCode:`${areaCode}`, phoneNumber:`${phoneNumber}`};

console.log(phoneNumberObject);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember, console.log() is just a side effect to print stuff . . . it does not return any value. Typically it is only really used in the development process, and not generally prevalant in production code.

You want to return the phoneNumberObject from the function, then later console.log() that value rather that log it here.

phoneNumber.js Outdated
// Returns an object in the format {areaCode, phoneNumber}

function parsePhoneNumber(phoneString){
const result = globalRegex.exec(phoneString);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay this is really tricky I'll admit, because the exec() method is 'iterable' on the Regex object you created for the globalRegex. That means that when you call 2 phone number checks, it is hitting the same global regex pattern twice, but the second time around it basically thinks it's already iterated everything there.

SO one way around this is to use the global value you set, but make a brand new Regex object inside the scope of the function so that it does not persist, and ensure that the data you parse is only relevant to the latest call's parameters . . . something like this:

function parsePhoneNumber(phoneString){
    const newRegex = new RegExp(globalRegex)
    const result = newRegex.exec(phoneString);
    const areaCode = result[1];
    const phoneNumber = `${result[2]}${result[3]}`;
    return {areaCode:`${areaCode}`, phoneNumber:`${phoneNumber}`};
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and since I just want to return the object itself, may as well skip setting it to a variable, it's kind of just an extra step.

@miguerurso miguerurso changed the title added responses to phone.js Completed Homework excercises Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants