我在大学广播电台工作,我们想要一种用数字来编目我们的物理音乐的方法。我想拍摄艺术家,发行名称和年份,并将其计算为9位数字。
我已经编码了一些东西。
函数双射(字母)将读取单个字符,参考var alpha = [" 0"," 1"," 2",...& #34; 9"""" b",..." X"" Y"&#34 ; z"],并返回提供的字符的索引。因此,例如,双射(c)= 13。
function bijection(character){
var alpha = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var position = alpha.indexOf(character) + 1;
return position;
}
函数easyread(string)将采用一串字符,并删除空格。因此,easyread("双门影院俱乐部")将返回twodoorcinemaclub。
function easyread(noun){
var out = noun.split(' ').join('');
return out;
}
函数标签(number1,number2,number3)将取三个数字,计算每个mod 1000,并将它们组合成一个9位整数。例如,label(1001,1002,1003)返回100200300。
function label(sum1,sum2,year){
var label = 1000000*(sum1%1000) + 1000*(sum2%1000) + year%1000;
return label;
我现在想编写一个名为sigma的函数,它将采用一个没有空格的字符串,逐个字符地分解它,然后将它们的索引相加。所以,我通过easyread运行一个字符串,将其插入sigma,然后在字符串中的每个字符的迭代中引用双射,并做一些数学运算来返回每个双射(字符)的总和。我会采取任何西格玛输出,并让它成为标签的输入。尽管如此,我还是失去了如何使sigma发生。有什么帮助吗?
更新
这是我到目前为止所得到的。
function sigma(noun){
var domain = easyread(noun) //removes spaces from input noun
for(let n = 0; n<domain.length; n++) {
//start with n = 0, and stop when n is incrementally larger than the length of our spaces-removed string.
let iterative = domain[n]; //not sure what this is bijection(iterative);
//tells the computer to calculate the bijection of each character
} //return sum of all bijection(iterative), somehow? }
答案 0 :(得分:0)
鉴于您对Javascript
不熟悉,我会尽量避免使用复杂的API。
基本上要删除所有空格,你必须做string.replace(...)
。 API采用正则表达式模式的位置。因此,您需要执行/\s/g
之类的操作来表示我想“查找字符串中的所有空格”,第二个参数只是''
,即用''替换它。 / p>
之后你会想要遍历无空间字符串值中的每个字符,只需做一个简单的for-loop
并在每个循环中调用你的bijection()
函数。
请考虑以下代码。
示例:强>
"use strict";
function bijection(char) {
// Assuming this is your bijection fn
console.log("converting character: " + char + " into value..." );
return 10; // assuming every character is value of 10.
}
let test = "one which may have spaces ";
let nospace = test.replace(/\s/g, "");
let totalValue = 0;
for(let n = 0; n < nospace.length; n++) {
let char = nospace[n];
totalValue += bijection(char);
}
console.log("Total Value is => " + totalValue);