Похожие чаты

// MY SOLUTION: `<?php declare(strict_types=1); // define("COLORS", ['Black', 'Brown', 'Red', 'Orange', 'Yellow', 'Green',

'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);
}
}
`

4 ответов

40 просмотров

Why you declare const inside a function?

Reny Ramos
Why you declare const inside a function?

Besides that, even if the const worked, the function would fail anyway it would never match.

wannabe-lokesh Автор вопроса
Reny Ramos
Besides that, even if the const worked, the functi...

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!

wannabe lokesh
Thanks. just solved it. but I saw community soluti...

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.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта