'Blue', 'Violet', 'Grey', 'White'], true);
function colorCode(string $color): int {
const COLORS = ['Black', 'Brown', 'Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Violet', 'Grey', 'White'];
return COLORS[$color];
}
?>`
`
TEST ERROR
ResistorColorTest::testColors
ParseError: syntax error, unexpected token "const"
/mnt/exercism-iteration/ResistorColor.php:7
`
`
// THE TEST:
class ResistorColorTest extends PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass(): void
{
require_once 'ResistorColor.php';
}
public function testColors(): void
{
$this->assertEquals(COLORS, [
"black",
"brown",
"red",
"orange",
"yellow",
"green",
"blue",
"violet",
"grey",
"white"
]);
}
public function testBlackColorCode(): void
{
$this->assertEquals(colorCode("black"), 0);
}
public function testOrangeColorCode(): void
{
$this->assertEquals(colorCode("orange"), 3);
}
public function testWhiteColorCode(): void
{
$this->assertEquals(colorCode("white"), 9);
}
}
`
Why you declare const inside a function?
Besides that, even if the const worked, the function would fail anyway it would never match.
Thanks. just solved it. but I saw community solutions, this question has not many different answers one of few different I remember was return array_flip(COLORS)[$color); Well I just forgot I can't create a constant either way: const or define() inside a function() in PHP. This exercise blown my mind for like a day now, it's a shame for me and I will surely remember it until I do something bit dumb silly shit than this LoL. I have a doubt. the same guy with that above solution, the return statment, made an 'array', something like const COLORS = [0 => "black", 1=> "brown", ] .. elements and so on, the list already mentioned. I thought array/associated array keys must be strings like ["0" => "black"]. anyways, forgive me. but should he just write an array? like ["black", "brown"] what's the point? array indexes starts with 0 anyway!
You can specify the position, it could be numeric or string, so in a numeric it could be for something like this [0 => 'asd', 5 => 'ddd'], when the numbers are not linear, but if you are going to just do it incremental then is not necesary.
Обсуждают сегодня