-2

My code is going like this:

function printPixel(nameImage, xpos, ypos) {
    var someImg = new SimpleImage(nameImage);
    var pix1 = someImg.getPixel(xpos,ypos);
    return pix1.getRed();
}
console.log(pix1)

printPixel("drewgreen.png",10, 10);
printPixel("drewgreen.png",250, 500);

The output always tells me that "pix1 is not defined", could you help me with my code? P.S: I edit the code in an online interpreter and the pngs have been uploaded already

2 Answers2

1

I solved the problem by the code following:

function printPixel(nameImage, xpos, ypos) {
    var someImage = new SimpleImage(nameImage) ;
    print("red is " + someImage.getRed(xpos,ypos));
    print("green is " + someImage.getGreen(xpos,ypos));
    print("blue is " + someImage.getBlue(xpos,ypos));
}
printPixel("drewgreen.png",10, 10);
printPixel("drewgreen.png",250, 500);

and here is another same question that I don't know why it goes like this:

function sumPixel(nameOfImage, xpos, ypos) {
    var theImage = new SimpleImage(nameOfImage) ;
    var redNumber = theImage.getRed(xpos,ypos);
    var blueNumber = theImage.getBlue(xpos,ypos);
    var greenNumber = theImage.getGreen(xpos,ypos);
    return redNumber + blueNumber + greenNumber;
}
sumPixel("drewgreen.png",10, 10);
sumPixel("drewgreen.png",250, 500);

It turns out the same thing: nothing was displayed, anyone can help me with this?

1

OK, I solved the second problem again by myself, just paste it here to mark it or share with you guys:

function sumPixel(nameOfImage, xpos, ypos) {
    var theImage = new SimpleImage(nameOfImage) ;
    var redNumber = theImage.getRed(xpos,ypos);
    var blueNumber = theImage.getBlue(xpos,ypos);
    var greenNumber = theImage.getGreen(xpos,ypos);
    return redNumber + blueNumber + greenNumber;
}

var answer = sumPixel("drewgreen.png", 250, 500);
print(answer);
answer = sumPixel("drewgreen.png",10, 10);
print(answer);

I found both of the two problems is related to lack of print. No print order, so it show nothing.