Link to home
Start Free TrialLog in
Avatar of Jon Imms
Jon ImmsFlag for United States of America

asked on

Uncaught SyntaxError: Missing initializer in const declaration

I am getting the following error in my JS file, "Uncaught SyntaxError: Missing initializer in const declaration" on line 13,14.

The function determines a users' state based on phone-number entered on an html form.

function getState(arecode) {

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
        return;
    }

  

    // Ensure we don't parse strings starting with 0 as octal values
    const thisarea = parseInt(areacode, 10); 
    const country;
    const state;

    

    // Code blocks alphabetized by state
    if (thisarea == 205 || 938 || 251 || 334) {
        state = 'AL';
        }
    else if (thisarea == 907 || 250 ) {
        state = 'AK';
        }
    else if (thisarea == 480 || 602 || 520 || 928) {
        state = 'AZ';
        }
    else if (thisarea == 501 || 479 || 870) {
        state = 'AR';
        }
    else if (thisarea == 213 || 310 || 424 || 619 || 714 || 415 || 949 || 916 || 408 || 669 || 805 || 510 || 909 || 760 || 209 || 626 || 650 || 951 || 562 || 707 || 925 || 661 || 559 || 530 || 541 || 831 || 858 || 323) {
        state = 'CA';
        }
    else if (thisarea == 720 || 719 || 970) {
        state = 'CO';
        }
    else if (thisarea == 203 || 959) {
        state = 'CT';
        }
    else if (thisarea == 302) {
        state = 'DE';
        }
    else if (thisarea == 786 || 407 || 561 || 813 || 904 || 754 || 727 || 850 || 239 || 321 || 941 || 352 || 772 || 386 || 863 || 785) {
        state = 'FL';
        }
    else if (thisarea == 678 || 770 || 404 || 912 || 762 || 229 || 423 || 478) {
        state = 'GA';
        }
    else if (thisarea == 808) {
        state = 'HI';
        }
    else if (thisarea == 208) {
        state = 'ID';
        }
    else if (thisarea == 847 || 630 || 773 || 312 || 708 || 618 || 217 || 779 || 309 || 872 || 815) {
        state = 'IL';
        }
    else if (thisarea == 463 || 219 || 765 || 574 || 930 || 260) {
        state = 'IN';
        }
    else if (thisarea == 515 || 319 || 712 || 563 || 641) {
        state = 'IA';
        }
    else if (thisarea == 913 || 785 || 316 || 620) {
        state = 'KS';
        }
    else if (thisarea == 859 || 502 || 270 || 606 || 513) {
        state = 'KY';
        }
    else if (thisarea == 504 || 318 || 337 || 985 || 225) {
        state = 'LA';
        }
    else if (thisarea == 207) {
        state = 'ME';
        }
    else if (thisarea == 410 || 240) {
        state = 'MD';
        }
    else if (thisarea == 617 || 978 || 351 || 781 || 339 || 413) {
        state = 'MA';
        }
    else if (thisarea == 248 || 734 || 616 || 313 || 586 || 517 || 989 || 269 || 810 || 231 || 906) {
        state = 'MI';
        }
    else if (thisarea == 612 || 651 || 952 || 763 || 507 || 218 || 320) {
        state = 'MN';
        }
    else if (thisarea == 601 || 662 || 228) {
        state = 'MS';
        }
    else if (thisarea == 314 || 816 || 573 || 417 || 636 || 660 || 479) {
        state = 'MO';
        }
    else if (thisarea == 406) {
        state = 'MT';
        }
    else if (thisarea == 704 || 336 || 910 || 828 || 252 || 919 || 984 || 651) {
        state = 'NC';
        }
    else if (thisarea == 701) {
        state = 'ND';
        }
    else if (thisarea == 402 || 308) {
        state = 'NE';
        }
    else if (thisarea == 702 || 775 || 541) {
        state = 'NV';
        }
    else if (thisarea == 603) {
        state = 'NH';
        }
    else if (thisarea == 732 || 201 || 551 || 908 || 609 || 862 || 973 || 856 || 203 || 606) {
        state = 'NJ';
        }
    else if (thisarea == 505 || 575) {
        state = 'NM';
        }
    else if (thisarea == 718 || 212 || 917) {
        state = 'NY';
        }
    else if (thisarea == 614 || 330 || 216 || 513 || 740 || 937 || 440 || 419 || 567) {
        state = 'OH';
        }
    else if (thisarea == 918 || 405 || 580 || 304) {
        state = 'OK';
        }
    else if (thisarea == 541) {
        state = 'OR';
        }
    else if (thisarea == 610 || 717 || 267 || 814 || 724 || 412 || 570 || 240 || 878) {
        state = 'PA';
        }
    
    else if (thisarea == 401) {
        state = 'RI';
        }
    else if (thisarea == 843 || 803 || 864) {
        state = 'SC';
        }
    else if (thisarea == 605 || 701) {
        state = 'SD';
        }
    else if (thisarea == 615 || 423 || 865 || 901 || 731 || 931 || 456) {
        state = 'TN';
        }
    else if (thisarea == 469 || 281 || 817 || 512 || 210 || 430 || 530 || 956 || 915 || 254 || 806 || 361 || 979 || 409 || 830 || 940 || 936 || 432 || 319 || 325 || 575 || 214) {
        state = 'TX';
        }
    else if (thisarea == 801 || 435) {
        state = 'UT';
        }
    else if (thisarea == 802) {
        state = 'VT';
        }
    else if (thisarea == 757 || 703 || 804 || 540 || 434 || 276) {
        state = 'VA';
        }
    else if (thisarea == 202) {
        state = 'DC';
        }
    else if (thisarea == 206 || 425 || 509 || 360 || 253 || 564) {
        state = 'WA';
        }
    else if (thisarea == 304) {
        state = 'WV';
        }
    else if (thisarea == 608 || 262 || 715 || 920 || 414 || 274) {
        state = 'WI';
        }
    else if (thisarea == 307) {
        state = 'WY';
        }
    else if (thisarea == 204 || 226 || 236 || 249 || 250 || 289 || 306 || 343 || 365 || 367 || 403 || 416 || 418 || 431 || 437 || 438 || 450 || 506 || 514 || 519 || 548 || 579 || 581 || 587 || 604 || 613 || 639 || 647 || 705 || 709 || 778 || 780 || 782 || 807 || 819 || 825 || 867 || 873 || 902 || 905) {
        state = 'Canada';
    }
    else {
        state = 'none';
    }

    return state;
}
    return country;

Open in new window

Avatar of Norie
Norie

Jon

You don't really need to declare state but if you do you should use let to declare it rather than const.

Also, in the if/else if statements you need to repeat thisarea like this, otherwise you''ll always return 'AL'.
if (thisarea == 205 || thisarea == 938 || thisarea == 251 || thisarea == 334) {
        return 'AL';

Open in new window


P.S. You might want to consider something like this to check if the areacode is in the list of areacodes for the state.
    if ([205, 938, 251, 334].indexOf(thisarea)!==-1) {
        state = 'AL';

Open in new window

A const "variable" cannot change later. Thus you must assign it a value.

But: in your method country and state, which are declared in line 13 and 14 are your result variables. So you need to declare them as var.

When having such a control flow, it is always a good idea to initialize your result variables with the outcome when nothing matches. E.g.

function getState(arecode) {
    var state = '<none>';

    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
    }
    else {
        const thisarea = parseInt(areacode, 10);

        if (thisarea == 205 || 938 || 251 || 334) {
            state = 'AL';
        }
        else if (thisarea == 907 || 250 ) {
            state = 'AK';
        }
        else if (thisarea == 480 || 602 || 520 || 928) {
            state = 'AZ';
        }
    }

    return state;
}

Open in new window

It's called single point of return.
Avatar of Jon Imms

ASKER

hi @ste5an

I thought your solution was working but not valid.

If i do this, It always puts AL as the state, regardless what is entered.
function getState(arecode) {
    var state = '<none>';

    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
    }
    else {
        const thisarea = parseInt(areacode, 10);

        if (thisarea == 205 || 938 || 251 || 334) {
            state = 'AL';
        }
        else if (thisarea == 907 || 250 ) {
            state = 'AK';
        }
        else if (thisarea == 480 || 602 || 520 || 928) {
            state = 'AZ';
        }
    }

    return state;
}

Open in new window


I tried this way with @Norie but i just get <none>
function getState(arecode) {
    var state = '<none>';

    if (typeof arecode !== 'string') {
        console.log('Must pass the arecode as a string.');
    }
    else {
        const thisarea = parseInt(areacode, 10);

        if (thisarea == 205 || thisarea == 938 || thisarea == 251 || thisarea == 334) {
            state = 'AL';
        }
        else if (thisarea == 907 || thisarea == 250 ) {
            state = 'AK';
        }
        else if (thisarea == 480 || thisarea == 602 || thisarea == 520 || thisarea ==  928) {
            state = 'AZ';
        }
    }

    return state;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much.  I'm understanding it too