Colorful resistors

Discussion thread for Colorful resistors.

I seem to have issues with completing this problem. Even though my output is shown to be correct in another editor I used, Project Lovelace’s automated testing does not recognise my output as correct. Does this have something to do with the returned value solution from the function resistance(band_colors) ? I also had some inconvenience formatting the printed “Input resistor band colors”, as Javascript returns an array of strings in the format "string0,string1,string2..." and not ["string0", "string1", string2"...] as required in the problem description.

digits = {
  "black": 0,
  "brown": 1,
  "red": 2,
  "orange": 3,
  "yellow": 4,
  "green": 5,
  "blue": 6,
  "violet": 7,
  "grey": 8,
  "white": 9
}

multiplier = {
  "pink": 0.001,
  "silver": 0.01,
  "gold": 0.1,
  "black": 1,
  "brown": 10,
  "red": 100,
  "orange": 10 ** 3,
  "yellow": 10 ** 4,
  "green": 10 ** 5,
  "blue": 10 ** 6,
  "violet": 10 ** 7,
  "grey": 10 ** 8,
  "white": 10 ** 9
}

tolerance = {
  "none": 0.2,
  "silver": 0.1,
  "gold": 0.05,
  "brown": 0.01,
  "red": 0.02,
  "green": 0.005,
  "blue": 0.0025,
  "violet": 0.001,
  "grey": 0.0005
}

function resistance(band_colors) {
  // Removed "let n_bands = colors.length;" as it is not used

  let nominal_R = 0;
  let minimum_R = 0;
  let maximum_R = 0;

  // Your code goes here!

  if (band_colors.length == 4) {
    // Print and format the input band colors
    console.log("Input resistor band colors: \[\"" + band_colors[0] + "\", " + band_colors[1] + "\", \""+ band_colors[2] + "\", \"" + band_colors[3] + "\"\]");
    // Calculate nominal resistance from band colors
    nominal_R = ( 100 * digits[band_colors[0]] + 10 * digits[band_colors[1]] ) * multiplier[band_colors[2]];
    
    // Seperate case for 5-band color codes
  } else if (band_colors.length == 5) {
    console.log("Input resistor band colors: \[\"" + band_colors[0] + "\", " + band_colors[1] + "\", \""+ band_colors[2] + "\", \"" + band_colors[3] + "\", \""+ band_colors[4] + "\"\]");
    nominal_R = ( 100 * digits[band_colors[0]] + 10 * digits[band_colors[1]] + digits[band_colors[2]] ) * multiplier[band_colors[3]];
  }

  //Calculate minimum and maximum resistance values using tolerance value
  minimum_R = nominal_R * (1 - tolerance[band_colors[band_colors.length - 1]] );
  maximum_R = nominal_R * (1 + tolerance[band_colors[band_colors.length - 1]] );
  
  
  // Print output
  console.log("Output nominal resistance: " + nominal_R);
  console.log("Output minimum resistance: " + minimum_R);
  console.log("Output maximum resistance: " + maximum_R);

  solution = [nominal_R, minimum_R, maximum_R];
  return solution;
}

// Tests for the resistance() function
resistance(["green", "blue", "yellow", "gold"]);
resistance(["red", "orange", "violet", "black", "brown"]);
1 Like

Hey @LECAGO sorry about those issues, thanks for bringing them up! I tried running your code and discovered that our Javascript support is missing some key features, like returning to you with an error message when the code checker encounters an error. Were you able to see the results of the test cases or did the “Submit code” button turn into a spinner that kept spinning?

When I tried submitting your code the big issue was the first test case which tests the one-band black resistor with zero resistance. With the input ['black'] you returned [0, None, None] which caused our code checker to crap out as it expected 3 numbers, which is totally our fault, you were right! @baz is this easy to fix so users get back some sort of “output type is invalid” error?

After adding some code to handle the band_colors.length == 1 case I now see the results of the test cases. You pass all the five-band resistor test cases but fail the four-band resistor test cases, I’ll let you compare your output and the expected output to figure out your last bug :slight_smile:

And yeah I agree formatting an array of strings like that can be a bit of a pain. Maybe try band_colors.toString() or band_colors.join(", ") if you want spaces after the commas.

Let us know if this helps, and we’ll make sure to fix these issues on our end!

Hey @LECAGO sorry about those issues, thanks for bringing them up! I tried running your code and discovered that our Javascript support is missing some key features, like returning to you with an error message when the code checker encounters an error. Were you able to see the results of the test cases or did the “Submit code” button turn into a spinner that kept spinning?

No errors were returned by the compiler, I was not able to see the results of the test cases, and the “Submit code” button did turn into a spinner that keeps spinning. I had already brought up this issue in my previous post “Feedback from a beginner user”, in the first bullet point.

When I tried submitting your code the big issue was the first test case which tests the one-band black resistor with zero resistance. With the input ['black'] you returned [0, None, None] which caused our code checker to crap out as it expected 3 numbers, which is totally our fault, you were right! @baz is this easy to fix so users get back some sort of “output type is invalid” error?

When I was attempting this problem earlier today I didn’t notice that one-band resistors were specified. I skimmed through the “input” requirements as I was under the impression that all resistors were either 4- or 5-band. A few minutes of research later, I ended up at the wikipedia page for a zero-ohm resistor (!!!). Welp, learning is fun!

After adding some code to handle the band_colors.length == 1 case I now see the results of the test cases. You pass all the five-band resistor test cases but fail the four-band resistor test cases, I’ll let you compare your output and the expected output to figure out your last bug :slight_smile:

Haha I see the issue now, the first two bands of a four-band resistor indicate the 10’s and the 1’s values, unlike the 5-band resistor.

Thanks for your help; the problem is now complete (7/7 test cases passed).