Resistor Color Duo

I really do not know why this code fails the tests:

function decodedValue(colors: string[]): number {
    if (!colors || colors.length < 2) {
        throw new Error('At least two colors required');
    }

    const [firstColor, secondColor] = colors.slice(0, 2);
    
    const firstNum = colorToDigit(firstColor);
    const secondNum = colorToDigit(secondColor);

    if (firstNum === undefined || secondNum === undefined) {
        throw new Error('Invalid color codes provided');
    }

    const resValue = firstNum === 0 ? secondNum : (firstNum * 10 + secondNum);
    return resValue;
}

function colorToDigit(value: string): number | undefined {
    switch (value.toLowerCase()) {
        case 'black':   return 0;
        case 'brown':   return 1;
        case 'red':     return 2;
        case 'orange':  return 3;
        case 'yellow':  return 4;
        case 'green':   return 5;
        case 'blue':    return 6;
        case 'violet':  return 7;
        case 'grey':    return 8;
        case 'white':   return 9;
        default:        return undefined;
    }
}

@Hoda96 What language is this? :slight_smile:

What language and what’s the failure/error message :slight_smile:

Looks like JavaScript, but it’s not JavaScript. So it must be TypeScript.

1 Like

highlight.js agrees (it’s inferred, OP did not include the language name in the tags):

<code class="hljs language-typescript" data-highlighted="yes">
1 Like

Took the liberty of running this code, it passes all the tests if you properly export the decodedValue function. Currently, the test script is attempting to import the decodedValue function from your main source file, but your main source file does not export anything.

In Javascript, and by extension Typescript, a module must export a value for another module to import it: JavaScript modules - JavaScript | MDN

Going by how far along the track you seem to be, you probably already know this and just missed the export keyword, but I thought I should include that source anyway.

TL;DR:

Replace:

function decodedValue(colors: string[]): number {

with

export function decodedValue(colors: string[]): number {
1 Like

I am trying to solve this problem using c. In the editor, no boilerplate functions are given. not getting what to do, what is the input, expected output.

Thank you for reporting, I think you are right. I made a separate post for it: [C] Missing boilerplate for resistor-color-duo

1 Like

Taking a look at the test file might give you an idea of what function the tests expect.

2 Likes