Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Alright, I have a large number of bugs poking through this woodwork, I’ve been trying to fix them using the Read-Search-Ask method for about 5 day after work. I’m probably being a major idiot :D. What am I doing wrong to not make steps 7, and 12(probably not calling this right) go through properly? If you could baby me and refuse to tell me the answer both that would probably be best :D.

Your code so far

let example = {
  containerId: 1,
  destination: "Monterey, California, USA",
  weight: 831,
  unit: "lb",
  hazmat: false
}
let example2 = { containerId: 1, destination: "Santa Cruz", weight: 304, unit: "kg", hazmat: false }
function normalizeUnits(manifest) {
    let changed = {...manifest}
  if (changed.unit=="lb") {
    changed.weight*=0.45
    changed.unit= "kg"
  return changed
} else { return changed}
}
function validateManifest(manifest) {
  let validated = {};
  if(manifest.hasOwnProperty("containerId")==false) {
    validated.containerId = "Missing";
  }
  if (manifest.hasOwnProperty("destination")==false) {
    validated.destination = "Missing";
  }
  if (manifest.hasOwnProperty("weight")==false) {
    validated.weight = "Missing";
  }
  if (manifest.hasOwnProperty("unit")==false) {
    validated.unit = "Missing";
  }
  if (manifest.hasOwnProperty("hazmat")==false) {
    validated.hazmat = "Missing";
  }
  if (manifest.containerId<= 0 | Number.isInteger(manifest.containerId)==false & validated.containerId!="Missing") {
    validated.containerId="Invalid";
  }
  if (typeof String(manifest.destination).trim()!="string"& validated.destination!="Missing") {
    validated.destination="Invalid";
  }
  if (manifest.weight <= 0 & validated.weight!="Missing") {
    validated.weight = "Invalid";
  }
  if (manifest.unit !== "kg"& validated.unit!="Missing") {
    validated.unit = "Invalid";
  }
  if (typeof manifest.hazmat!= "boolean"& validated.hazmat!="Missing") {
    validated.hazmat = "Invalid";
  }
  return validated
}
console.log(validateManifest(example2));
function processManifest(manifest){
  if (validateManifest(manifest)=="{}") {
    console.log(`Validation success: ${manifest.containerId}`) 
    console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`)
  } else {
    console.log(`Validation error: ${manifest.containerId}`)
    return manifest
  }

}
console.log(processManifest(example2));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

is this your most recent code? When I tried it a lot of tests failed, not just two.

also, please fix your indentation! (it’s hard to read messy code)

let example = {
  containerId: 1,
  destination: "Monterey, California, USA",
  weight: 831,
  unit: "lb",
  hazmat: false
}

let example2 = { 
  containerId: 1, 
  destination: "Santa Cruz", 
  weight: 304, unit: "kg", 
  hazmat: false }


function normalizeUnits(manifest) {
    let changed = {...manifest}
  if (changed.unit=="lb") {
    changed.weight*=0.45
    changed.unit= "kg"
  return changed
} else { 
  return changed}
}


function validateManifest(manifest) {
  let validated = {};

  /*Start of Missing*/
  
  if(manifest.hasOwnProperty("containerId")==false) {
    validated.containerId = "Missing";
  }
  if (manifest.hasOwnProperty("destination")==false) {
    validated.destination = "Missing";
  }
  if (manifest.hasOwnProperty("weight")==false) {
    validated.weight = "Missing";
  }
  if (manifest.hasOwnProperty("unit")==false) {
    validated.unit = "Missing";
  }
  if (manifest.hasOwnProperty("hazmat")==false) {
    validated.hazmat = "Missing";
  }

  /*comment seperating Missing if statements and Invalid if statements*/

  if (manifest.containerId<= 0 
  | Number.isInteger(manifest.containerId)==false 
  & validated.containerId!="Missing") {
    validated.containerId="Invalid";
  }
  if (typeof String(manifest.destination).trim()!="string"
  & validated.destination!="Missing") {
    validated.destination="Invalid";
  }
  if (manifest.weight <= 0 
  & validated.weight!="Missing") {
    validated.weight = "Invalid";
  }
  if (manifest.unit !== "kg"
  & validated.unit!="Missing") {
    validated.unit = "Invalid";
  }
  if (typeof manifest.hazmat!= "boolean"
  & validated.hazmat!="Missing") {
    validated.hazmat = "Invalid";
  }


  return validated
}


console.log(validateManifest(example2));


function processManifest(manifest){
  if (validateManifest(manifest)=="{}") {
    console.log(`Validation success: ${manifest.containerId}`) 
    console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`)
  } else {
    console.log(`Validation error: ${manifest.containerId}`)
    return manifest
  }

}


console.log(processManifest(example2));

Alright, I’ve formatted the if statements for greater readability where “and” and “or” statements are separate lines now. If there are any other indentation issues I can fix please let me know specifics, still pretty new. The total that aren’t going through are 7, 9 ,12, 13, 14(which I’m assuming will go with the others), 17 and up (not including 23).

Hi @OmnipotentSquirrel ,

Does it make sense to test for an invalid value before you determine if the property is missing?

Happy coding!

Hey @dhess , just saw this, and I don’t quite understand what you’re asking here. Are you saying that each property should have an invalid if then statement inside of the Missing if then statement?

That could work. The point is that it does not make sense to try to validate the value of a missing property.

This doesn’t seem to relate at all to some of the missing if statements themselves not going through, I’ll definitely remove this safeguard though.

Specifically, it seems as though test 6 is satisfied in that empty brackets are procured, but not exactly the empty object that test 7 is looking for. Even though I’m using the format of creating a new empty object and returning that object after I’ve run tests to add to it if something is “Missing”.

To help debug your code, I suggest testing your code against the failed tests you can and comparing what your code returns against what is expected.

Start with the first failed test you can run a test on — Test #10:

console.log(validateManifest({ containerId: 0, destination: 405, weight: -84, unit: "pounds", hazmat: "no" }));

Then move on to Tests 12, 13, 14, 18, 22, and 23, if necessary.