Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

i do not understand the problem , i think i got the logic right.

the problem area is in my validate manifest function the else if

Your code so far


function  normalizeUnits(manifest){
  const copy = {...manifest};

  if(copy.unit=='lb'){
    copy.weight =  copy.weight * 0.45;
    copy.unit = 'kg'; 
  
    
  }
  
  


    return copy;
  }


function validateManifest (manifest){
const copy = {...manifest};
const empty = {};
const rules = {containerId: "number",
  destination: "string",
  weight: "number",
  unit: "string",
  hazmat:"boolean" };
  const allowedunits = ["kg","lb"];
 for (let key in rules) {
  if (copy[key] === "" || copy[key] === undefined) {
     empty[key] = "Missing";

     }else if(typeof copy[key] !== rules[key] || copy[key] === null || (rules[key] === "number" && copy[key] < 0) ||  (rules[key] === "string" && copy[key].trim().length === 0)||(key === "unit" && !allowedunits.includes(copy[key])) || (key === "hazmat" && typeof copy[key] !== "boolean")){

      empty[key] = "Invalid";
          }
    }
  return empty; 
}

const processManifest = (manifest) =>{
  const copy = {...manifest};
}

Your browser information:

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

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Hi @kimkiara520,

What are you doing to test your code? I don’t see any function calls.

What tests are you failing? Have you tried to see what your code returns compared to what is expected for the failing tests?

Happy coding!

thanks .

now my problem is that my code i working vs code but not on the codecamp

here is my current code :

function  normalizeUnits(manifest){

  const copy = {...manifest};



  if(copy.unit=='lb'){

    copy.weight =  copy.weight \* 0.45;

    copy.unit = 'kg'; 

  

    

  }

  

  




    return copy;

  }




function validateManifest (manifest){

const copy = {...manifest};

const empty = {};

const rules = {containerId:Number.isInteger("number"),

  destination: "string",

  weight:Number.isNaN("number"),

  unit: "string",

  hazmat:"boolean" };

  const allowedunits = \["kg","lb"\];

 for (let key in rules) {

  if (copy\[key\] === "" || copy\[key\] === undefined) {

     empty\[key\] = "Missing";

// problem is with my conditions 

     }else if(typeof copy\[key\] !== rules\[key\] || copy\[key\] === null || (rules\[key\] ==="number" && copy\[key\] < 0) ||  (rules\[key\] === "string" && copy\[key\].trim().length === 0)||(key === "unit" && !allowedunits.includes(copy\[key\])) || (key === "hazmat" && typeof copy\[key\] !== "boolean")||(key === "containerId" && Number.isNaN(copy\[key\])) || (key === "weight" && !Number.isInteger(copy\[key\]))){



      empty\[key\] = "Invalid";

          }

    }

  return empty; 



}




const processManifest = (manifest) =>{

  const copy = {...manifest};

}

i have managedto fix all my problems except one :Calling validateManifest() with { containerId: 0, destination: 405, weight: -84, unit: “pounds”, hazmat: “no” } should return the new object { containerId: “Invalid”, destination: “Invalid”, weight: “Invalid”, unit: “Invalid”, hazmat: “Invalid” } without mutating the source input. I don’t understand when container is number o it needs to be invalid

Would you post all of your updated code please so we can test?


When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').