-
Notifications
You must be signed in to change notification settings - Fork 21
Completed Homework excercises #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
phoneNumber.js
Outdated
let testPhoneNumber = function(phoneString){ | ||
console.log(globalRegex.test(phoneString)); | ||
}; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
@@ -30,6 +36,17 @@ console.log(testPhoneNumber('(206) 33-4444')); // should return false, missing a | |||
// the phone number. | |||
// Returns an object in the format {areaCode, phoneNumber} | |||
|
|||
function parsePhoneNumber(phoneString){ | |||
const result = globalRegex.exec(phoneString); |
There was a problem hiding this comment.
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}`};
};
There was a problem hiding this comment.
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.
No description provided.