Cargo Manifest Validator - Build a Cargo Manifest Validator

Test 15: All tests passed but stuck at test 15. Could anyone identify the issue?

const manifest = { destination: “Watsonville”, hazmat: true };

const normalizeUnits = (manifest) => {

const newManifest = {…manifest};

if(newManifest.unit === ‘lb’){

newManifest.weight = newManifest.weight \* 0.45;

newManifest.unit = 'kg'

}

return newManifest;

};

const validateManifest = (manifest) => {

let result = {};

if(manifest.hasOwnProperty(‘containerId’)){

const key = 'containerId';

if(manifest\[key\] === null || Number.isInteger(manifest\[key\]) && Number(manifest\[key\]) <= 0 || manifest\[key\]?.toString()?.includes('.')){

  result\[key\] = 'Invalid';

} 

}else {

result\['containerId'\] = 'Missing';

}

if(manifest.hasOwnProperty(‘destination’)){

const key = 'destination';

if(!manifest\[key\]?.toString()?.trim() || Number.isInteger(manifest\[key\])) {

        result\[key\] = 'Invalid';

} 

} else {

result\['destination'\] = 'Missing';

}

if(manifest.hasOwnProperty(‘weight’)){

const key = 'weight';

if(Number.isNaN(manifest\[key\]) || !Number.isInteger(manifest\[key\]) || Number(manifest\[key\]) < 0){

  result\[key\] = 'Invalid';

} 

} else {

result\['weight'\] = 'Missing';

}

if(manifest.hasOwnProperty(‘hazmat’)){

const key = 'hazmat';

if(typeof manifest\[key\] !== 'boolean'){

  result\[key\] = 'Invalid';

} 

} else {

result\['hazmat'\] = 'Missing';

}

if(manifest.hasOwnProperty(‘unit’)){

const key = 'unit';

if(manifest\[key\]?.toLowerCase() !== 'lb' && manifest\[key\]?.toLowerCase() !== 'kg'){

      result\[key\] = 'Invalid';

  }

} else {

result\['unit'\] = 'Missing';

}

return result;

};

const processManifest = (manifest) => {

if(manifest === null || manifest === undefined) manifest = {};

const isValidate = validateManifest(manifest);

if(Object.values(isValidate).length === 0) {

const normalizedWeight = normalizeUnits(manifest);

console.log(\`Validation success: ${manifest.containerId}\`);

console.log(\`Total weight: ${normalizedWeight.weight} kg\`) 

} else {

console.log(\`Validation error: ${manifest.containerId}\`);

console.log(isValidate);

}

};

processManifest(manifest);

can you please format your code and share a link to the project?

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 (').

Unable to edit post, tried multiple times.

You can just post your formatted code in a reply to this post.

const manifest = { destination: “Watsonville”, hazmat: true };

const normalizeUnits = (manifest) => {

const newManifest = {…manifest};

if(newManifest.unit === ‘lb’){

newManifest.weight = newManifest.weight \* 0.45;

newManifest.unit = 'kg'

}

return newManifest;

};

const validateManifest = (manifest) => {

let result = {};

if(manifest.hasOwnProperty(‘containerId’)){

const key = 'containerId';

if(manifest[key] === null || Number.isInteger(manifest[key]) && Number(manifest[key]) <= 0 || manifest[key]?.toString()?.includes('.')){

  result[key] = 'Invalid';

} 

} else {

result['containerId'] = 'Missing';

}

if(manifest.hasOwnProperty(‘destination’)){

const key = 'destination';

if(!manifest[key]?.toString()?.trim() || Number.isInteger(manifest[key])) {

        result[key] = 'Invalid';

} 

} else {

result['destination'] = 'Missing';

}

if(manifest.hasOwnProperty(‘weight’)){

const key = 'weight';

if(Number.isNaN(manifest[key]) || !Number.isInteger(manifest[key]) || Number(manifest[key]) < 0){

  result[key] = 'Invalid';

} 

} else {

result['weight'] = 'Missing';

}

if(manifest.hasOwnProperty(‘hazmat’)){

const key = 'hazmat';

if(typeof manifest[key] !== 'boolean'){

  result[key] = 'Invalid';

} 

} else {

result['hazmat'] = 'Missing';

}

if(manifest.hasOwnProperty(‘unit’)){

const key = 'unit';

if(manifest[key]?.toLowerCase() !== 'lb' && manifest[key]?.toLowerCase() !== 'kg'){

      result[key] = 'Invalid';

  }

} else {

result['unit'] = 'Missing';

}

return result;

};

const processManifest = (manifest) => {

if(manifest === null || manifest === undefined) manifest = {};

const isValidate = validateManifest(manifest);

if(Object.values(isValidate).length === 0) {

const normalizedWeight = normalizeUnits(manifest);

console.log(`Validation success: ${manifest.containerId}`);

console.log(`Total weight: ${normalizedWeight.weight} kg`) 

} else {

console.log(`Validation error: ${manifest.containerId}`);

console.log(isValidate);

}

};

processManifest(manifest);


something went wrong, there are many \ in the code your posted that I am sure should not be there

please try again

It came automatically when I select </> for code, especially for single quote. I think its formatted for single tick in code, but in my actual code it’s not there!

Else than this any check for logic, whats wrong that test 15 not getting passed?

I can’t debug your code because of the multiple syntax issues, I am not able to respond right this

you know what, please use the Ask for Help button

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Edited for ‘\’ , the syntax issue came after I post selecting </> for code, because in my original code it’s note like this . No syntax issues and tests are running successfully, just stuck at test 15.

when you ask for help, also please include the link to the challenge. The ask for help button does also that automatically

there are other characters other than \ that cause syntax issues, if you please use the button we can do it quick

I already went for this and tried multiple times to create help from code, but the fCC edit window to create a help for my topic/code is getting hanged and unable to post from there. So I choose this path.

then please try creating the code block before putting the code in it

or put it in a gist (gist.github.com)

Try testing your code with this function call:

console.log(processManifest({ containerId: "0", destination: "Watsonville", weight: 0, unit: "pound", hazmat: 0 }));

Is the object printed in the console what you expect?

Added check for this case but still again all tests cases passes and stuck at test 15.

please post your updated code

const manifest = { containerId: '0', destination: "Watsonville", weight: 0, unit: "pound", hazmat: 0 }

const normalizeUnits = (manifest) => {
  const newManifest = {...manifest};
  if(newManifest.unit === 'lb'){
    newManifest.weight = newManifest.weight * 0.45;
    newManifest.unit = 'kg'
  }
  return newManifest;
};

const validateManifest = (manifest) => {
  let result = {};

  if(manifest.hasOwnProperty('containerId')){
    const key = 'containerId';
    if(manifest[key] === null || !Number.isInteger(manifest[key]) || (Number.isInteger(manifest[key]) && Number(manifest[key]) <= 0) || manifest[key]?.toString()?.includes('.')){
      result[key] = 'Invalid';
    } 
  } else {
    result['containerId'] = 'Missing';
  }

  if(manifest.hasOwnProperty('destination')){
    const key = 'destination';
    if(!manifest[key]?.toString()?.trim() || Number.isInteger(manifest[key])) {
            result[key] = 'Invalid';
    } 
  } else {
    result['destination'] = 'Missing';
  }

  if(manifest.hasOwnProperty('weight')){
    const key = 'weight';
    if(Number.isNaN(manifest[key]) || !Number.isInteger(manifest[key]) || Number(manifest[key]) < 0){
      result[key] = 'Invalid';
    } 
  } else {
    result['weight'] = 'Missing';
  }

  if(manifest.hasOwnProperty('hazmat')){
    const key = 'hazmat';
    if(typeof manifest[key] !== 'boolean'){
      result[key] = 'Invalid';
    } 
  } else {
    result['hazmat'] = 'Missing';
  }

  if(manifest.hasOwnProperty('unit')){
    const key = 'unit';
    if(manifest[key]?.toLowerCase() !== 'lb' && manifest[key]?.toLowerCase() !== 'kg'){
          result[key] = 'Invalid';
      }
  } else {
    result['unit'] = 'Missing';
  }

  return result;
};

const processManifest = (manifest) => {
  if(manifest === null || manifest === undefined) manifest = {};
  const isValidate = validateManifest(manifest);
  if(Object.values(isValidate).length === 0) {
    const normalizedWeight = normalizeUnits(manifest);
    console.log(`Validation success: ${manifest.containerId}`);
    console.log(`Total weight: ${normalizedWeight.weight} kg`) 
  } else {
    console.log(`Validation error: ${manifest.containerId}`);
    console.log(isValidate);
  }
};

processManifest(manifest);

Should you be manipulating the value before you validate?

Is 0 a positive number and a valid weight?

Your validation checks for hazmat and unit are flipped so the resulting validation object has out-of-order properties.

Happy coding!