javascript-exercises icon indicating copy to clipboard operation
javascript-exercises copied to clipboard

Bug - 13_caesar: Test results appear correct but Fail

Open kgroening05 opened this issue 4 years ago • 4 comments

Complete the following REQUIRED checkboxes:

  • [X] I have thoroughly read and understand The Odin Project Contributing Guide
  • [X] The title of this issue follows the Bug - location of bug: brief description of bug format, e.g. Bug - Exercises: File type incorrect for all test files

The following checkbox is OPTIONAL:

  • [ ] I would like to be assigned this issue to work on it

1. Description of the Bug:

Running the tests on my code results in results that seem to pass but are counted as fail.

image

2. How To Reproduce:

This is my code for the project:

const caesar = function(string, shiftValue) {
    let encodedString = "";
    for (let index = 0; index < string.length + 1; index++) {
        let letterUnicode = string.charCodeAt(index);
        if (letterUnicode >= 65 && letterUnicode <= 90){ // Capital letters
            letterUnicode += shiftValue;
            while (letterUnicode >90){ // Deal with overlapping into other unicodes
                letterUnicode -= 26;
            } 
            while (letterUnicode <65){
                letterUnicode += 26;
            }
        }    
        else if(letterUnicode >= 97 && letterUnicode <= 122){ // Lowercase letters
            letterUnicode += shiftValue;
            while (letterUnicode > 122){ // Deal with overlapping into other unicodes
                letterUnicode -= 26;
            }
            while (letterUnicode < 97){
                letterUnicode += 26;
            }
        }
        encodedString += String.fromCharCode(letterUnicode);
    }
    return encodedString;
};

3. Expected Behavior:

The tests results for all of the tests appear correct, but they all fail. I haven't modified the .spec.js file in any way other than removing .skip from all the tests.

Haven't seen this kind of issue with the previous exercises.

4. Desktop/Device:

  • Device: Lenovo Laptop
  • OS: Windows 10
  • Node version: v18.12.1
  • Jest version: 8.19.2

5. Additional Information:

I'm wondering if there's some quirk to the fromCharCode method. Maybe there are some invisible characters being added. Or perhaps the terminal is hiding something and making the results appear identical when they are actually different in some way.

kgroening05 avatar Nov 20 '22 17:11 kgroening05

I'm wondering if there's some quirk to the fromCharCode method. Maybe there are some invisible characters being added. Or perhaps the terminal is hiding something and making the results appear identical when they are actually different in some way.

After spending way too much time trying to figure this out and getting nowhere because I was doing it purely via the console (which lies) @nguyenlekhtn pointed out on Discord that your code also inserts a null character, so the tests are successfully failing

Asartea avatar Jan 29 '23 11:01 Asartea

You are correct, In your loop, you’re iterating from 0 to string.length + 1, which means you’re going one index beyond the end of the string. probably this should be the correct version of code

const caesar = function(string, shiftValue) { let encodedString = ""; for (let index = 0; index < string.length; index++) { let letterUnicode = string.charCodeAt(index); if ((letterUnicode >= 65 && letterUnicode <= 90) || (letterUnicode >= 97 && letterUnicode <= 122)) { // Alphabetic characters let base = (letterUnicode < 97) ? 65 : 97; letterUnicode = ((letterUnicode - base + shiftValue) % 26) + base; } encodedString += String.fromCharCode(letterUnicode); } return encodedString; };

vinayrewatkar avatar Jan 09 '24 14:01 vinayrewatkar

Lol

Jovanysfive avatar Jan 09 '24 16:01 Jovanysfive

Closing as it seems to be resolved + lack of activity. @Jovanysfive if you'd like to elaborate on your comment please feel free, though

thatblindgeye avatar Feb 04 '24 18:02 thatblindgeye