Похожие чаты

// 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 ответов

39 просмотров

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.

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

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

а через ESC-код ?
Alexey Kulakov
29
30500 за редактор? )
Владимир
47
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
any reference of this implementation?
BitBuddha
29
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
11
Ⓐrtto, [4/23/24 7:02 PM] Please explain more fully how it is not working exactly, and what are the steps you are taking, and what error messages come or what happens. Ⓐrtto, ...
Ezza Kezza
2
sounds like people have lost their kaspa on tradeogre... does this mean tradeogre not trustworthy?
Ezza Kezza
15
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
11
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Карта сайта