Похожие чаты

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

18 просмотров

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.

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

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

я не магистр хаскеля, но разве не может лейзи тип конвертнуться в не-лейзи запросив вычисление содержимого прям при инициализации?
deadgnom32 λ madao
100
в сях есть множество как в питоне? для удаление дубликатов
Linus
25
Yeh scammer hai kya ? 🙄 mujhre bhi solana invest ke liye bol re thi kal
Amit
17
If arrr gets implemented on cake wallet. Would that mean arrr/xmr swaps possible in cake wallet like atomic swaps on komodowallet ?
Keks Meister 🏴‍☠️
18
читать файл максимально быстро? странный вопрос))
zamtmn
53
Уважаемые. Вопрос. Есть ПС (рязань 5600Г+4060Ти). Запускаю игры, через усб карту захвата сигнал "забираю" на ОБС, установленной на Эйр м1. Всё это добро передается на твич, в ...
UglyKoyote
8
Hi. Is there something wrong in koala wallet or is it just my new phone? Balance is 0.00
Jani Ginman
12
30 BCH videos in Hausa Language successfully delivered. In December 2023, I created a Flipstarter to produce Bitcoin Cash videos in the Hausa language for greater outreach an...
Munnir
5
@AMP_Whale do we earn anvil inu for staking it for 4 years?
Drunken Barbie
11
Hello team of Kadena, every day 100 000 coins more on market, no news no partnership, how do you think that someone will notice Kadena with so small volume and on the 247th pl...
jeronim jurakovic
8
Карта сайта