FCC challenge not accepting code, but works fine on vscode

Trying to finish this challenge. I’ve done the code in vscode (in strict) and the return value is what is should be, however when I copy/paste it to the fcc site, it does not pass the challenge.
Its probably something stupid, however I’ve been going over and over it and cant find the issue.
code:

'use strict';

function alphabetUpper(x) {

    switch(x) {

        case 'N':resultArray.push('A');

        break;

        case 'O':resultArray.push('B');

        break;

        case 'P':resultArray.push('C');

        break;

        case 'Q':resultArray.push('D');

        break;

        case 'R':resultArray.push('E');

        break;

        case 'S':resultArray.push('F');

        break;

        case 'T':resultArray.push('G');

        break;

        case 'U':resultArray.push('H');

        break;

        case 'V':resultArray.push('I');

        break;

        case 'W':resultArray.push('J');

        break;

        case 'X':resultArray.push('K');

        break;

        case 'Y':resultArray.push('L');

        break;

        case 'Z':resultArray.push('M');

        break;

        case 'A':resultArray.push('N');

        break;

        case 'B':resultArray.push('O');

        break;

        case 'C':resultArray.push('P');

        break;

        case 'D':resultArray.push('Q');

        break;

        case 'E':resultArray.push('R');

        break;

        case 'F':resultArray.push('S');

        break;

        case 'G':resultArray.push('T');

        break;

        case 'H':resultArray.push('U');

        break;

        case 'I':resultArray.push('V');

        break;

        case 'J':resultArray.push('W');

        break;

        case 'K':resultArray.push('X');

        break;

        case 'L':resultArray.push('Y');

        break;

        case 'M':resultArray.push('Z');

        break;

    }

}

const resultArray = [];

let result = '';

function rot13(str) {

    let characters = str.split('');

    for (let i=0; i<characters.length; i++) {

        if (characters[i].match(/[a-z]/i)) {

            alphabetUpper(characters[i]);

        } else {

            resultArray.push(characters[i]);

        }

    }

    for (let i=0; i<resultArray.length; i++) {

        result = result + resultArray[i];

    }

    return result;

}

//end

Many thanks!

can you please share the link for this challenge?

So how you would test it is by copying ALL test cases into the code-window, console.log them and see what is happening.

The error is quite simple: It’s called “global variables” → every time the function is called, it’s adding to “result” and the “result_array”. Meaning every call includes ALL PREVIOUS results in the array and double that in the result.

1 Like

that makes sense. Thanks, I didn’t think about that.

Yeah it’s a hard to spot error - and a good reminder as to why you should NEVER use global variables :wink:

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.