Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

ok i don’t know how to pass test 15, its the only test that fails regarding the processManifest function,

If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
Here;s my code so far: 
function normalizeUnits(manifest){
  let newManifest = {...manifest};
  if(newManifest.unit === "lb"){
    newManifest.weight = newManifest.weight * 0.45;
    newManifest.unit = "kg";
    return newManifest
  }



  return newManifest
}
console.log("normalizeUnits function","\n");
console.log(normalizeUnits({containerId: 1,destination: "Aruba", weight: 933,unit: "lb",hazmat: false}));
console.log("\n","\n");



function validateManifest(manifest){
  let newManifest = {...manifest};
  let errors = {};
  if(newManifest.hasOwnProperty("containerId") === false){
    errors.containerId = "Missing";
  }
 if(newManifest.containerId === null || newManifest.containerId <= 0 || (Number.isInteger(newManifest.containerId) === false && newManifest.hasOwnProperty("containerId"))){

  errors.containerId = "Invalid";
 }
  if(newManifest.hasOwnProperty("destination") === false){
    errors.destination = "Missing";
  }
  if(newManifest.hasOwnProperty("destination") && typeof(newManifest.destination) !== "string" || newManifest.hasOwnProperty("destination") && newManifest.destination.trim()==="" ){
    errors.destination = "Invalid"
  }

  if(newManifest.hasOwnProperty("weight") === false){
    errors.weight = "Missing";
  }
  if(newManifest.hasOwnProperty("weight") && newManifest.weight <= 0 || newManifest.hasOwnProperty("weight") && Number.isInteger(newManifest.weight) === false || newManifest.hasOwnProperty("weight") && Number.isNaN(newManifest.weight) === true){
    errors.weight = "Invalid";
  }

  if(newManifest.hasOwnProperty("unit") === false){
    errors.unit = "Missing";
  }

if(newManifest.unit !== "lb" && newManifest.hasOwnProperty("unit") && newManifest.unit !=="kg" && newManifest.hasOwnProperty("unit")){
  errors.unit = "Invalid";
}

  if(newManifest.hasOwnProperty("hazmat") === false){
    errors.hazmat = "Missing"
  }
if(newManifest.hasOwnProperty("hazmat") && newManifest.hazmat == true || newManifest.hasOwnProperty("hazmat") && newManifest.hazmat !== false){
  errors.hazmat = "Invalid"
}
return errors
}

console.log("story #6 and 7")

console.log(validateManifest({containerId: 1,destination: "Santa Cruz",weight: 304,unit: "kg",hazmat: false}))

console.log("\n");
//story #8/missing elements
console.log("story #8/missing elements");
console.log(validateManifest({}));

console.log("\n");

//story #9
console.log("story #9")
console.log(validateManifest({ containerId: null, destination: "Santa Cruz", weight: 304, unit: "kg", hazmat: false }))


console.log("\n");
console.log("story #10/all invalid elementf")
console.log({ containerId: "Invalid", destination: "Invalid", weight: "Invalid", unit: "Invalid", hazmat: "Invalid" });


 console.log("\n");
console.log("story #11/container Invalid");
console.log(validateManifest({containerId: -2}))
console.log("\n");
 console.log("story #12, call validateManifest with ");
console.log(validateManifest({containerId: 3.50}));

console.log("\n");

console.log(validateManifest({destination: " "}));


console.log("\n");
//story #12,call validateManifest with containerId: 3.50

console.log(validateManifest({containerId: 3.50}));

//story #13 call validateManifest with {destination: ". "}


console.log(validateManifest({destination: "  "}));

console.log("\n")
console.log("story #14 validateManifest with weight: NaN",);
console.log("\n");
console.log(validateManifest({weight: NaN}))

//story #15 unsolved

//story #16 is correct


//-----------------------------------
/*function processManifest(manifest){
let newManifest = validateManifest(manifest);
let normalizedManifest = normalizeUnits(manifest);
if(newManifest === "Missing" || newManifest.containerId === "Invalid"){
  console.log(`Validation error: ${containerId}`);
  console.log(newManifest);
  
}else{
  console.log(`Validation success: ${containerId}`);
  console.log(`Total weight: {normalizedManifest.weight}}kg`)
  
  }
}
 //processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false } should first log "Validation success: 55" and then log "Total weight: 180 kg"})
1
console.log("\n");

console.log(processManifest({containerId: 55,destination: "Carmel",weight: 400,unit: "lb",hamat:false}))
*/

Your code so far

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



  return newManifest
}
console.log("normalizeUnits function","\n");
console.log(normalizeUnits({containerId: 1,destination: "Aruba", weight: 933,unit: "lb",hazmat: false}));
console.log("\n","\n");



function validateManifest(manifest){
  let newManifest = {...manifest};
  let errors = {};
  if(newManifest.hasOwnProperty("containerId") === false){
    errors.containerId = "Missing";
  }
 if(newManifest.containerId === null || newManifest.containerId <= 0 || (Number.isInteger(newManifest.containerId) === false && newManifest.hasOwnProperty("containerId"))){

  errors.containerId = "Invalid";
 }
  if(newManifest.hasOwnProperty("destination") === false){
    errors.destination = "Missing";
  }
  if(newManifest.hasOwnProperty("destination") && typeof(newManifest.destination) !== "string" || newManifest.hasOwnProperty("destination") && newManifest.destination.trim()==="" ){
    errors.destination = "Invalid"
  }

  if(newManifest.hasOwnProperty("weight") === false){
    errors.weight = "Missing";
  }
  if(newManifest.hasOwnProperty("weight") && newManifest.weight <= 0 || newManifest.hasOwnProperty("weight") && Number.isInteger(newManifest.weight) === false || newManifest.hasOwnProperty("weight") && Number.isNaN(newManifest.weight) === true){
    errors.weight = "Invalid";
  }

  if(newManifest.hasOwnProperty("unit") === false){
    errors.unit = "Missing";
  }

if(newManifest.unit !== "lb" && newManifest.hasOwnProperty("unit") && newManifest.unit !=="kg" && newManifest.hasOwnProperty("unit")){
  errors.unit = "Invalid";
}

  if(newManifest.hasOwnProperty("hazmat") === false){
    errors.hazmat = "Missing"
  }
if(newManifest.hasOwnProperty("hazmat") && newManifest.hazmat == true || newManifest.hasOwnProperty("hazmat") && newManifest.hazmat !== false){
  errors.hazmat = "Invalid"
}
return errors
}

console.log("story #6 and 7")

console.log(validateManifest({containerId: 1,destination: "Santa Cruz",weight: 304,unit: "kg",hazmat: false}))

console.log("\n");
//story #8/missing elements
console.log("story #8/missing elements");
console.log(validateManifest({}));

console.log("\n");

//story #9
console.log("story #9")
console.log(validateManifest({ containerId: null, destination: "Santa Cruz", weight: 304, unit: "kg", hazmat: false }))


console.log("\n");
console.log("story #10/all invalid elementf")
console.log({ containerId: "Invalid", destination: "Invalid", weight: "Invalid", unit: "Invalid", hazmat: "Invalid" });


 console.log("\n");
console.log("story #11/container Invalid");
console.log(validateManifest({containerId: -2}))
console.log("\n");
 console.log("story #12, call validateManifest with ");
console.log(validateManifest({containerId: 3.50}));

console.log("\n");

console.log(validateManifest({destination: " "}));


console.log("\n");
//story #12,call validateManifest with containerId: 3.50

console.log(validateManifest({containerId: 3.50}));

//story #13 call validateManifest with {destination: ". "}


console.log(validateManifest({destination: "  "}));

console.log("\n")
console.log("story #14 validateManifest with weight: NaN",);
console.log("\n");
console.log(validateManifest({weight: NaN}))

//story #15 unsolved

//story #16 is correct


//-----------------------------------
/*function processManifest(manifest){
let newManifest = validateManifest(manifest);
let normalizedManifest = normalizeUnits(manifest);
if(newManifest === "Missing" || newManifest.containerId === "Invalid"){
  console.log(`Validation error: ${containerId}`);
  console.log(newManifest);
  
}else{
  console.log(`Validation success: ${containerId}`);
  console.log(`Total weight: {normalizedManifest.weight}}kg`)
  
  }
}
 //processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false } should first log "Validation success: 55" and then log "Total weight: 180 kg"})
1
console.log("\n");

console.log(processManifest({containerId: 55,destination: "Carmel",weight: 400,unit: "lb",hamat:false}))
*/

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.4 Safari/605.1.15

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md at main · freeCodeCamp/freeCodeCamp · GitHub

can you give clean code to test please? I am having trouble uncommenting processManifest to test it

Hi @dreyes61,

Good effort on the debugging!

It looks like this bit of code is your main issue in validateManifest(). Can you think of another way to make sure hazmat is a boolean?

Suggestions: I notice you are using newManifest.hasOwnProperty() repeatedly. This is redundant. Consider using an else statement for the invalid checks after each if statement that checks for a missing property. Also, you really don’t need to create a new manifest in this function since you are not making changes to it.

In processManifest(), this is not the way to check if manifest is not an empty object.

Happy coding!

Tell us what’s happening:

i can’t figure out why i keep failing test #7. here’s the error message and my updated code:

  1. If the input manifest object is valid, your validateManifest function should return an empty object {}.

Your code so far

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



  
console.log(normalizeUnits({ containerId: 68, destination: "Salinas", weight: 101, unit: "lb", hazmat: true }))

console.log("\n")


function validateManifest(manifest){

  let errors = {}
  if(manifest.hasOwnProperty("containerId") === false){
    errors.containerId = "Missing";

  }else if(Number.isInteger(manifest.containerId) === false || manifest.containerId <= 0 || Number.isNaN(manifest.container) === true){
    errors.containerId = "Invalid";
  }

  if(manifest.hasOwnProperty("destination") === false){
    errors.destination = "Missing";

  }else if(typeof manifest.destination !== "string" || manifest.destination.trim() === ""){
    errors.destination = "Invalid";
  }
   if(manifest.hasOwnProperty("weight") === false) {
    errors.weight = "Missing";

  }else if(manifest.weight <= 0 || Number.isInteger(manifest.weight) === false || Number.isNaN(manifest.weight) === true){
    errors.weight = "Invalid";
     }
      if(manifest.hasOwnProperty("unit") === false){
    errors.unit = "Missing"
  }else if(typeof manifest.unit !== "string" && manifest.unit !=="lb" || manifest.unit !=="kg"){
    errors.unit = "Invalid";
  }
  if(manifest.hasOwnProperty("hazmat") === false){
    errors.hazmat = "Missing";
  }else if(typeof manifest.hazmat !== "boolean"){
    errors.hazmat = "Invalid";
  }
  return errors
}

console.log("Test # 7\n seems to be satisfied but still getting an error.");



function processManifest(manifest){
    let newManifest = validateManifest(manifest);
    if(newManifest.containerId === "Invalid" || newManifest.containerId === "Missing"){
        console.log(`Validation error: ${manifest.containerId}`);
        console.log(newManifest);
    } else {
        let normalizedManifest = normalizeUnits(manifest);
        console.log(`Validation success: ${manifest.containerId}`);
        console.log(`Total weight: ${normalizedManifest.weight} kg`);
    }
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.4 Safari/605.1.15

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md at main · freeCodeCamp/freeCodeCamp · GitHub

Please do not create duplicate topics for the same challenge/project question(s). If you need more help then respond back to the original topic you created with your follow up questions and/or your updated code and question.

The duplicate topic has been unlisted.

Thank you.

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

please test

console.log(validateManifest({
    "containerId": 68,
    "destination": "Salinas",
    "weight": 45.45,
    "unit": "kg",
    "hazmat": true
}))

sorry i forgot about ```

i changed || to && between the Number.iInteger and Number.isNaN code, i get the empty object result but still get the same error.

```
else if(manifest.weight <= 0 || Number.isInteger(manifest.weight) === false && Number.isNaN(manifest.weight) === true){
    errors.weight = "Invalid";
```

what would you expect for that function call? what issues should the function find?

That’s not the issue.

ILM is trying to point out that weight can be a float or an integer. Your condition expects just an integer.