JavaScript npm install Heroku,引用错误未定义

标签 javascript node.js heroku npm

document.getElementById("HC").innerHTML = String(hammingCode.encode("11"));


console.log("Encode 1111: ", hammingCode.encode("1111"));

我正在尝试使用This我的 JavaScript 代码中包含汉明代码 npm libary,但是我没有太多从 npm 安装的经验。 我已经完成了 npm install hamming-code 并成功安装,我相信,我的 package.json 也已更新为“hamming-code”:“0.0.2”。 当我开始输入 hammingCo... 它会提供示例、编码和解码等,但是当我尝试编码一个简单的字符串时,我收到控制台错误消息“未捕获( promise 中)ReferenceError:hammingCode 未定义”。该应用程序是通过 heroku 部署的。

我是否需要添加任何其他源,或包含“var hammingCode = require("hamming-code")”?我尝试过包含此内容,但仍然无法使其正常工作。

我有一个index.html,我的大部分JavaScript都在其中,我想在其中使用汉明码,还有一个index.js,我相信我的大部分服务器代码都在其中。 提前致谢。

最佳答案

您需要在 html 文件中包含汉明码脚本。例如检查下面的示例。

/**
 * hammingEncode - encode binary string input with hamming algorithm
 * @param {String} input - binary string, '10101'
 * @returns {String} - encoded binary string
 */
function hammingEncode(input) {
	if (typeof input !== 'string' || input.match(/[^10]/)) {
		return console.error('hamming-code error: input should be binary string, for example "101010"');
	}

	var output = input;
	var controlBitsIndexes = [];
	var controlBits = [];
	var l = input.length;
	var i = 1;
	var key, j, arr, temp, check;

	while (l / i >= 1) {
		controlBitsIndexes.push(i);
		i *= 2;
	}

	for (j = 0; j < controlBitsIndexes.length; j++) {
		key = controlBitsIndexes[j];
		arr = output.slice(key - 1).split('');
		temp = chunk(arr, key);
		check = (temp.reduce(function (prev, next, index) {
			if (!(index % 2)) {
				prev = prev.concat(next);
			}
			return prev;
		}, []).reduce(function (prev, next) { return +prev + +next }, 0) % 2) ? 1 : 0;
		output = output.slice(0, key - 1) + check + output.slice(key - 1);
		if (j + 1 === controlBitsIndexes.length && output.length / (key * 2) >= 1) {
			controlBitsIndexes.push(key * 2);
		}
	}

	return output;
}


/**
 * hammingPureDecode - just removes from input parity check bits
 * @param {String} input - binary string, '10101'
 * @returns {String} - decoded binary string
 */
function hammingPureDecode(input) {
	if (typeof input !== 'string' || input.match(/[^10]/)) {
		return console.error('hamming-code error: input should be binary string, for example "101010"');
	}

	var controlBitsIndexes = [];
	var l = input.length;
	var originCode = input;
	var hasError = false;
	var inputFixed, i;
	
	i = 1;
	while (l / i >= 1) {
		controlBitsIndexes.push(i);
		i *= 2;
	}

	controlBitsIndexes.forEach(function (key, index) {
		originCode = originCode.substring(0, key - 1 - index) + originCode.substring(key - index);
	});

	return originCode;
}

/**
 * hammingDecode - decodes encoded binary string, also try to correct errors
 * @param {String} input - binary string, '10101'
 * @returns {String} - decoded binary string
 */
function hammingDecode(input) {
	if (typeof input !== 'string' || input.match(/[^10]/)) {
		return console.error('hamming-code error: input should be binary string, for example "101010"');
	}

	var controlBitsIndexes = [];
	var sum = 0;
	var l = input.length;
	var i = 1;
	var output = hammingPureDecode(input);
	var inputFixed = hammingEncode(output);


	while (l / i >= 1) {
		controlBitsIndexes.push(i);
		i *= 2;
	}

	controlBitsIndexes.forEach(function (i) {
		if (input[i] !== inputFixed[i]) {
			sum += i;
		}
	});

	if (sum) {
		output[sum - 1] === '1' 
			? output = replaceCharacterAt(output, sum - 1, '0')
			: output = replaceCharacterAt(output, sum - 1, '1');
	}
	return output;
}

/**
 * hammingCheck - check if encoded binary string has errors, returns true if contains error
 * @param {String} input - binary string, '10101'
 * @returns {Boolean} - hasError
 */
function hammingCheck(input) {
	if (typeof input !== 'string' || input.match(/[^10]/)) {
		return console.error('hamming-code error: input should be binary string, for example "101010"');
	}

	var inputFixed = hammingEncode(hammingPureDecode(input));

	return hasError = !(inputFixed === input);
}

/**
 * replaceCharacterAt - replace character at index
 * @param {String} str - string
 * @param {Number} index - index
 * @param {String} character - character 
 * @returns {String} - string
 */
function replaceCharacterAt(str, index, character) {
  return str.substr(0, index) + character + str.substr(index+character.length);
}

/**
 * chunk - split array into chunks
 * @param {Array} arr - array
 * @param {Number} size - chunk size
 * @returns {Array} - chunked array
 */
function chunk(arr, size) {
	var chunks = [],
	i = 0,
	n = arr.length;
	while (i < n) {
		chunks.push(arr.slice(i, i += size));
	}
	return chunks;
}

/* 
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD.
        define(factory);
    } else if (typeof module === 'object' && module.exports) {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory();
    } else {
        // Browser globals (root is window)
        root.hammingCode = factory();
    }
}(this, function () {
    return {
      encode: hammingEncode,
      pureDecode: hammingPureDecode,
      decode: hammingDecode,
      check: hammingCheck
    };
})); */


console.log();
document.getElementById("code").innerHTML	 =
hammingEncode('101010101');
<div id="code">
</div>

关于JavaScript npm install Heroku,引用错误未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52129472/

相关文章:

javascript - 在 AngularJS 中将表单重置为初始值

javascript - 具有批量编辑网格的 Telerik MVC 网站可防止窗口关闭时未保存的更改

javascript - 使用 jquery 删除循环内输入标签的禁用属性

heroku - 如何使用 Nuxt.JS 应用程序访问 Heroku 环境变量

ruby-on-rails - 我将如何产生 Heroku 工作人员来分而治之关键字列表?

javascript - 无法解析主机 github.com

javascript - Nodejs中将Buffer数据对象转换为数组

javascript - 允许远程服务器访问本地主机上的 API 服务器

mysql - 我可以在 Sequelize 中定义 User 和 Server 模型之间的多对多和一对多关系吗?

heroku - 如何更新在heroku上将源作为github url的gem?