Completed Homework excercises#12
Completed Homework excercises#12miguerurso wants to merge 2 commits intoUW-JavaScript-310B:masterfrom
Conversation
| let testPhoneNumber = function(phoneString){ | ||
| console.log(globalRegex.test(phoneString)); | ||
| }; | ||
|
|
There was a problem hiding this comment.
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.
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.
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.
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); |
There was a problem hiding this comment.
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.
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.