Quality Assurance Projects - Metric-Imperial Converter

Tell us what’s happening:

Describe your issue in detail here. Exactly what is says on the tin. It throws a type error saying that convertHandler.convert is not a function.

**Your code so far** This is the link.

Your browser information:

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

Challenge: Quality Assurance Projects - Metric-Imperial Converter

Link to the challenge:

The tests/1_unit-tests.js has one test (from your code on the Replit):

assert.equal(convertHandler.getNum("4gal"),'4')

When I run the test, the test fails with an AssertionError.

My bad. I realise it was not in a test. Rather, to debug please use the main front end.

What is it you were trying to do when you get the error? Where are you seeing the error - in Node console or browser console or etc.

Node I believe. Here is my code

function ConvertHandler() {
  
  this.getNum = function(input) {
    let result = input.match(/[a-zA-Z]+|[^A-Za-z]+/g)
    console.log("🚀 ~ file: convertHandler.js ~ line 5 ~ ConvertHandler ~ result", result)
    if(isNaN(result[0]) == false) {
    	return Number(result[0]);
	}else{
		fraction = result[0].split('/')
		if(fraction.length < 3){
			if(fraction.length == 1){
				return 1
			}else{
				return Number(eval(result[0]))
			}
		}else{
			throw new Error("Invalid conversion")
		}
	}
  };
  
  this.getUnit = function(input) {
    let result=input.match(/[a-zA-Z]+|[^A-Za-z]+/g)
    console.log("🚀 ~ file: convertHandler.js ~ line 12 ~ ConvertHandler ~ result", result)
    if(result.length == 1){
		throw new Error("No conversion unit")
	}else if(result[1].toLowerCase() == 'gal' || result[1].toLowerCase() == 'l' || result[1].toLowerCase() == 'kg' || result[1].toLowerCase() == 'lbs' || result[1].toLowerCase() == 'km' || result[1].toLowerCase() == 'mi'){
		return result[1];
	}else{
	 	throw new Error("Invalid conversion unit")
  	};
  }
    this.getReturnUnit = function(input) {
    let result = input.split(/[a-zA-Z]+|[0-9]+/g)[1].toLowerCase()
    console.log("🚀 ~ file: convertHandler.js ~ line 17 ~ ConvertHandler ~ result", result)
		if(result == 'gal'){
			return 'L'
		}else if(result == 'l'){
			return 'gal'
    }else if(result == 'lbs'){
			return 'Kg'
		}else if(result == 'kg'){
			return 'lbs'
    }else if(result == 'mi'){
			return 'Km'
		}else if(result == 'km'){
			return 'gal'
		}
  this.spellOutUnit = function(result) {
	 //let result = input.match(/[a-zA-Z]+|[^a-zA-Z]+/g)[1].toLowerCase()
		if(result == 'gal'){
			return 'gallons'
		}else if(result == 'l'){
			return 'litres'
    }else if(result == 'lbs'){
			return 'pounds'
		}else if(result == 'kg'){
			return 'kilograms'
    }else if(result == 'mi'){
			return 'miles'
		}else if(result == 'km'){
			return 'kilometers'
		}
	};
  
  this.convert = function(input) {
    const galToL = 3.78541;
    const lbsToKg = 0.453592;
    const miToKm = 1.60934;
	let number = Number(input.split(/[a-zA-Z]+[^A-Za-z]+/) [0].toLowerCase())
	console.log("🚀 ~ file: convertHandler.js ~ line 53 ~ ConvertHandler ~ number", number)
	let result = input.split(/[a-zA-Z]+|[0-9]+/g)[1].toLowerCase()
	console.log("🚀 ~ file: convertHandler.js ~ line 55 ~ ConvertHandler ~ result", result)
	if(result == 'gal'){
		return number*galToL 
	}else if(result == 'l'){  
		return number/galToL
	}else if(result == 'lbs'){
		return lbsToKg*number
	}else if(result == 'kg'){
		return number/lbsToKg
}else if(result == 'mi'){
		return miToKm*number
	}else if(result == 'km'){
		return number/miToKm
	}
  };
  
  this.getString = function(input) {
		let number = Number(input.split(/[a-zA-Z]+[^A-Za-z]+/) [0].toLowerCase())
    let result = input.split(/[a-zA-Z]+|[0-9]+/g)[1].toLowerCase()
    return this.getNum(input)+' '+this. spellOutUnit(this.getUnit(input))+' converts to '+this.convert(input)+' '+this.spellOutUnit(this.getReturnUnit(input))


  };
  
}
		}
module.exports = ConvertHandler;

this.getReturnUnit = function(input) { …

This function doesn’t have a closing brace } to end the function. Hence the error.

Year I noticed. Maybe it is not a good idea to use repl.it on a phone.

Whenever I run my code I get an error saying that when ever I use a convertHandler functions in my Functional Tests it is a “unexpected token”. desptite convertHandler being an object and all the functions are one level deep. I also use assert as a function to test for one of two possibilities.
My code is here:

can you please give the link to the editor not the live app? it’s not possible to see your code in the live app

3 of the 4 example usages don’t work in the input field in the app you posted

Whoops: here it is: boilerplate-project-metricimpconverter-1 - Nix (beta) Repl - Replit

Whenever I run my code I get an error saying that whenever I use a convertHandler function in my Functional Tests it is an “unexpected token”. Despite convertHandler being an object and all the functions are one level deep. I also use assert as a function to test for one of two possibilities.
My code is here: boilerplate-project-metricimpconverter-1 - Nix (beta) Repl - Replit

This error is normally a syntax error (like a typo or missing parentheses or an unexpected character, etc.). Do you have a stack trace (entire detailed error from console; it might have a line number in the code where the error is happening)?

First of all, remember I am quite skilled with javascript I know to check this kind of stuff. Secondly, I checked everything as in the description. The reason I am posting is that from experience this is ABSURD. Either way, if you want an error message here it is:
~/boilerplate-project-metricimpconverter-1$ n n
pm test

fcc-imperial-metric-converter-boilerplate@1.0.0 test
mocha ./tests/ --ui tdd

/home/runner/boilerplate-project-metricimpconverter-1/tests/2_functional-tests.js:80
assert(assert.include(res.text, “Not Found”) || assert.throws({convertHandler.getString(‘invalidgal’)}));
^

SyntaxError: Unexpected token ‘.’
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions…js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.exports.requireOrImport (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/mocha/lib/esm-utils.js:33:34)
at async singleRun (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/mocha/lib/cli/run-helpers.js:156:3)

This is the error from the browser console:

Error: No conversion unit
    at ConvertHandler.getUnit (/home/runner/boilerplate-project-metricimpconverter-1/controllers/convertHandler.js:31:13)
    at /home/runner/boilerplate-project-metricimpconverter-1/routes/api.js:12:79
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/layer.js:95:5)
    at /home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/index.js:335:12)
    at next (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/home/runner/boilerplate-project-metricimpconverter-1/node_modules/body-parser/lib/types/urlencoded.js:91:7)

The error is thrown from:

  this.getUnit = function (input) {
    let result = input.match(/[a-zA-Z]+|[^A-Za-z]+/g);
    console.log(
      "?? ~ file: convertHandler.js ~ line 12 ~ ConvertHandler ~ result",
      result
    );
    if (result.length == 1) {
      throw new Error("No conversion unit");
   ...

I am actually trying to assert for that with chai.throws() (don’t mind that error)

Is the syntax you are using for that correct?

I just found out that the syntax I was using was not the best.

Whenever I run my code I get an error saying that whenever I use a convertHandler function in my Functional Tests it is an “unexpected token”. Despite convertHandler being an object and all the functions are one level deep. I also use assert as a function to test for one of two possibilities.
My code is here: boilerplate-project-metricimpconverter-1 - Nix (beta) Repl - Replit

I have another issue Although all my tests are passing according to fcc the unit tests and the functional tests are not complete can you please guide me to fix this.

When I cloned your project and run npm run test, I see the following:

  19 passing (220ms)
  1 failing

  1) Functional Tests
       Invalid inputs
         convert an invalid input:

      Uncaught AssertionError: expected 'invalid unit' to equal 'Invalid unit'
      + expected - actual

      -invalid unit
      +Invalid unit
      
      at /home/runner/boilerplate-project-metricimpconverter-1/tests/2_functional-tests.js:53:22
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:716:12)
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:916:18)
      at IncomingMessage.emit (node:events:539:35)
      at endReadableNT (node:internal/streams/readable:1345:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)

What are you not understanding about the AssertionError error shown?