Getting an error message trying to run code

Doing an exercise for a course in which I need to change a group of pixels in the top right corner of 2 different images into another color using a function. They gave me part of the code, but I needed to write the body of the function. When I run the code, I get an error from the first print statement saying “Cannot read properties of undefined (reading ‘getString’)”. Can anybody help? Here is the code:

function topRightCorner(cornerWidth, cornerHeight, someImage, red, green, blue) {

for (var pixel of someImage.values()){
    if (pixel.getX() >= (someImage.getWidth()- cornerWidth) & pixel.getY() <= cornerHeight){
        pixel.setRed(red);
        pixel.setGreen(green);
        pixel.setBlue(blue);
    }
}

}

var picture = new SimpleImage(“chapel.png”);
var result = topRightCorner(30, 60, picture, 255, 255, 0);
print(result);
var picture2 = new SimpleImage(“smalllion.jpg”);
var result2 = topRightCorner(125, 20, picture2, 255, 0, 0);
print(result2);

Can you log the result variable to the console and see if it’s defined before the print call?

So I’m very new to this and don’t know what you mean by logging it to the console lol. But the result variable was only created in the line before the print call. It should call the topRightCorner function and assign the values in parentheses to the variables in the function and then execute the code in the function, if that makes sense.

I understand what it should do, but if you don’t know for sure then you should check. Something is undefined, that’s what your error says.

Add console.log(result) before that print(result) and check the console.

Ok so after I figured out how to look at the console, it is showing the console log line as undefined. So I’m assuming the result variable is undefined, but it should be defined and I don’t understand why it’s not.

It looks like you’re not returning anything from topRightCorner. Did you intend to return something?

Apparently I just needed to add “return someImage” at the end of the function and it worked. I’m just dumb lol. Thank you for the help!

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