Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Code works, the Chckmark does not. Please help so I could complete this lab :).

Your code so far

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

  if(copy.unit == "lb")
  {
    copy.unit = "kg";
    copy.weight *= 0.45;
    return copy;
  }
  else if(copy.unit == "kg")
  {
    return copy;
  }
  else
  {
    return "Invalid unit measurement";
  }
}

function validateManifest (manifest)
{

  let copyEmpty = {...manifest}
  if (Object.keys(copyEmpty).length == 0)
  {
    copyEmpty.containerId = "Missing";
    copyEmpty.destination = "Missing";
    copyEmpty.weight = "Missing";
    copyEmpty.unit = "Missing";
    copyEmpty.hazmat = "Missing";

    return copyEmpty;
  }

  let copyValid = {...manifest}
  if (copyValid.containerId > 0 && typeof copyValid.containerId == "number")
  {
    if (copyValid.destination != null && typeof copyValid.destination == "string" && copyValid.destination.trim().length != 0)
    {
      if (copyValid.weight >= 0 && typeof copyValid.weight == "number")
      {
        if (copyValid.unit == "lb" || copyValid.unit == "kg")
        {
          if (copyValid.hazmat == true || copyValid.hazmat == false)
          {
            return new Object();
          }
        }
      }
    }
  }

  function checkNaNorNull (value)
  {
    if (Number.isNaN(value) || null == value)
    {
      return true;
    }
    else
    {
      return false
    }
  }

  let copy = {...manifest}

  if ((copy.containerId <= 0 || typeof copy.containerId != "number" || copy.containerId % 1 != 0 || checkNaNorNull(copy.containerId)) && Object.hasOwn(copy, "containerId"))
  {
    copy.containerId = "Invalid";
  }
  else if (!Object.hasOwn(copy, "containerId"))
  {
    copy.containerId = "Missing";
  }
  else
  {
    delete copy.containerId;
  }

  if ((typeof copy.destination != "string" || copy.destination.trim().length == 0) && Object.hasOwn(copy, "destination"))
  {
    copy.destination = "Invalid";
  }
  else if (!Object.hasOwn(copy, "destination"))
  {
    copy.destination = "Missing";
  }
  else
  {
    delete copy.destination;
  }

  if ((copy.weight < 0 || typeof copy.weight != "number" || checkNaNorNull(copy.weight)) && Object.hasOwn(copy, "weight"))
  {
    copy.weight = "Invalid";
  }
  else if (!Object.hasOwn(copy, "weight"))
  {
    copy.weight = "Missing";
  }
  else
  {
    delete copy.weight;
  }

  if (copy.unit == "kg" || copy.unit == "lb")
  {
    delete copy.unit;
  }
  else if (!Object.hasOwn(copy, "unit"))
  {
    copy.unit = "Missing";
  }
  else
  {
    copy.unit = "Invalid";
  }

  if (copy.hazmat == true || copy.hazmat == false)
  {
    delete copy.hazmat;
  }
  else if (!Object.hasOwn(copy, "hazmat"))
  {
    copy.hazmat = "Missing";
  }
  else
  {
    copy.hazmat = "Invalid";
  }

  return copy;
}

function processManifest (manifest)
{
  let processValid = {...manifest}

  if (Object.keys(validateManifest(processValid)).length == 0)
  {
    console.log(`Validation success: ${processValid.containerId}`);
    if (processValid.unit == "lb")
    {
      let weightKg = normalizeUnits(processValid);
      return  console.log(`Total weight: ${weightKg.weight} kg`);
    }
    else
    {
      return console.log(`Total weight: ${processValid.weight} kg`)
    }
  }
  else
  {
    console.log(`Validation error: ${processValid.containerId}\n`)
  }
  return console.log(validateManifest(processValid));
}

processManifest({containerId: 1});

Your browser information:

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

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

Consider this object

   { containerId: -6,
     destination: 123,
     weight: 0,
     unit: 'pounds',
     hazmat: true }

what should validateManifest return?

is the weight valid?

what about this object?

 destination: ' ', weight: NaN, unit: 'kg', hazmat: 1 }

is the hazmat valid?

I fixed up the issues you pointed out, but I still can’t finish the lab..

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

  if(copy.unit == "lb")
  {
    copy.unit = "kg";
    copy.weight *= 0.45;
    return copy;
  }
  else if(copy.unit == "kg")
  {
    return copy;
  }
  else
  {
    return "Invalid unit measurement";
  }
}

function validateManifest (manifest)
{

  let copyEmpty = {...manifest}
  if (Object.keys(copyEmpty).length == 0)
  {
    copyEmpty.containerId = "Missing";
    copyEmpty.destination = "Missing";
    copyEmpty.weight = "Missing";
    copyEmpty.unit = "Missing";
    copyEmpty.hazmat = "Missing";

    return copyEmpty;
  }

  let copyValid = {...manifest}
  if (copyValid.containerId > 0 && typeof copyValid.containerId == "number")
  {
    if (copyValid.destination != null && typeof copyValid.destination == "string" && copyValid.destination.trim().length != 0)
    {
      if (copyValid.weight > 0 && typeof copyValid.weight == "number")
      {
        if (copyValid.unit == "lb" || copyValid.unit == "kg")
        {
          if (copyValid.hazmat === true || copyValid.hazmat === false)
          {
            return new Object();
          }
        }
      }
    }
  }

  function checkNaNorNull (value)
  {
    if (Number.isNaN(value) || null == value)
    {
      return true;
    }
    else
    {
      return false
    }
  }

  let copy = {...manifest}

  if ((copy.containerId <= 0 || typeof copy.containerId != "number" || copy.containerId % 1 != 0 || checkNaNorNull(copy.containerId)) && Object.hasOwn(copy, "containerId"))
  {
    copy.containerId = "Invalid";
  }
  else if (!Object.hasOwn(copy, "containerId"))
  {
    copy.containerId = "Missing";
  }
  else
  {
    delete copy.containerId;
  }

  if ((typeof copy.destination != "string" || copy.destination.trim().length == 0) && Object.hasOwn(copy, "destination"))
  {
    copy.destination = "Invalid";
  }
  else if (!Object.hasOwn(copy, "destination"))
  {
    copy.destination = "Missing";
  }
  else
  {
    delete copy.destination;
  }

  if ((copy.weight <= 0 || typeof copy.weight != "number" || checkNaNorNull(copy.weight)) && Object.hasOwn(copy, "weight"))
  {
    copy.weight = "Invalid";
  }
  else if (!Object.hasOwn(copy, "weight"))
  {
    copy.weight = "Missing";
  }
  else
  {
    delete copy.weight;
  }

  if (copy.unit == "kg" || copy.unit == "lb")
  {
    delete copy.unit;
  }
  else if (!Object.hasOwn(copy, "unit"))
  {
    copy.unit = "Missing";
  }
  else
  {
    copy.unit = "Invalid";
  }

  if (copy.hazmat === true || copy.hazmat === false)
  {
    delete copy.hazmat;
  }
  else if (!Object.hasOwn(copy, "hazmat"))
  {
    copy.hazmat = "Missing";
  }
  else
  {
    copy.hazmat = "Invalid";
  }

  return copy;
}

function processManifest (manifest)
{
  let processValid = {...manifest}

  if (Object.keys(validateManifest(processValid)).length == 0)
  {
    console.log(`Validation success: ${processValid.containerId}`);
    if (processValid.unit == "lb")
    {
      let weightKg = normalizeUnits(processValid);
      return  console.log(`Total weight: ${weightKg.weight} kg`);
    }
    else
    {
      return console.log(`Total weight: ${processValid.weight} kg`)
    }
  }
  else
  {
    console.log(`Validation error: ${processValid.containerId}\n`)
  }
  return console.log(validateManifest(processValid));
}

Is the linebreak \n something you are asked to add?