Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Idk where does the undefined log comes from when i call the processManifest function.

the task 7 also failed to registered even though the output its seem correct. help please

Your code so far

const normalizeUnits = manifest => {
  //lb ÷ 2.205 = kg
  const copy = {...manifest};
  if(manifest.unit !== 'kg'){
    copy.weight = manifest.weight * 0.45;
    copy.unit = "kg";
    return copy;
  }
  else return copy;
}

const validateManifest = manifest => {
  let copy = {};
  //containerId
  if(manifest.containerId === undefined){
    copy.containerId = "Missing";
  }
  else if(Number.isInteger(manifest.containerId) === false || manifest.containerId < 1){
    copy.containerId = "Invalid";
  };

  //destination
  if(manifest.destination === undefined){
    copy.destination = "Missing";
  }
  else if(typeof(manifest.destination) !== "string"  || manifest.destination.trim() === ""){
    copy.destination = "Invalid";
  };

  //weigt
  if(manifest.weight === undefined){
    copy.weight = "Missing"
  }
  else if(Number.isInteger(manifest.weight) === false || manifest.weight < 1){
    copy.weight = "Invalid";
  };

  //unit
  if(manifest.unit === undefined){
    copy.unit = "Missing";
  }
  else if(typeof(manifest.unit) !== "string" || manifest.unit !== "kg"){
    copy.unit = "Invalid";
  };
  
  //hazmat
  if(manifest.hazmat === undefined){
    copy.hazmat = "Missing";
  }
  else if(typeof(manifest.hazmat) !== "boolean"){
    copy.hazmat = "Invalid";
  };

  return copy;
}

const processManifest = manifest =>{
  let normalize = normalizeUnits(manifest);
  // console.log(normalize);
  let val = validateManifest(normalize);
  // console.log(val);

  if (validateManifest(manifest).unit == "Invalid"){
    normalize;
    if(Object.keys(val).length == 0){
      console.log(`Validation success: ${normalize.containerId}`);
      console.log(`Total weight: ${normalize.weight} Kg`);
    }
    else if(Object.keys(val).length > 0){
      console.log(`Validation error: ${normalize.containerId}`);
      console.log(val);
    }
  }
  else{
      console.log(`Validation error: ${manifest.containerId}`);
      console.log(val);
  }

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

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

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

// console.log(validateManifest({}))

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

// console.log(processManifest({ containerId: -88, destination: "Soledad", weight: NaN }))

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

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

this took me 3 days to debug and i’m hopeless to solve it

hello!

here you are trying to log the return value of calling processManifest(). SInce you are not really returning any value from processManifest, it logs undefined to the console.

Test 7 is failing because if a manifest has unit: “lb” it should be valid, but your validateManifest is currently invalidating it.

Thank you for your replies, the task 7 now valid. but i still can’t get over the task 18-21 even though i manage to log 2 value and the output is valid, but still not registered to 18-21 (i remove the console.log when calling processManifest

.

The logic you tried to use inside processManifest turned out to be a bit messy, though you are very close. Try something like this –

IF number of keys in val is 0
  NORMALIZE manifest,
  SHOW success message
ELSE
  SHOW error message

use normalizeUnits only if the manifest is valid.

Almost forgot - “kg” should be in lowercase here.

I JUST SOLVE IT, AND IT SOO OBVIOUSSS!!!

good luck for everyone who trying to solve it, this is very challenging !
p.s. that success alert is so satisfying !