我在一家大學的電臺工作,我們想要一種方法來編號我們的物理音樂。我想把藝術家,發行名稱和年份,並計算成9位數字。輸入一個英文單詞,找回一個整數
我已經編碼了一些東西。函數bijection(字母)將讀取單個字符,參考var alpha = [「0」,「1」,「2」,...「9」,「a」,「b」,... 「x」,「y」,「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;
}
功能標籤(數字1,數字,number3的)將採取三個數字,評估每個MOD 1000,並且將它們結合在一起以9位整數。例如,標籤(1001,1002,1003)返回100200300.
function label(sum1,sum2,year){
var label = 1000000*(sum1%1000) + 1000*(sum2%1000) + year%1000;
return label;
我現在想寫一個叫西格瑪功能,這將需要一個字符串,沒有空格,按字符打破它的性格,然後添加上漲指數。所以,我會通過easyread運行一個字符串,將它插入到sigma中,然後在字符串中的每個字符的迭代中引用bijection,並執行一些數學運算以返回每個雙射(字符)的總和。我會採取任何西格瑪輸出,並將其作爲標籤的輸入。不過,我迷失在如何讓西格瑪發生。任何幫助?
更新:
這是我走到這一步。
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? }
歡迎SO!如果你展示了你已經放在一起的代碼,這將是有幫助的。您可能還想閱讀http://stackoverflow.com/help/mcve – silencedmessage
使用['split'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)將字符串拆分爲一個數組,['map'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)將字母轉換爲數字和['reduce'](https://developer.mozilla。org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)將它們彙總在一起。 – Xufox
另一方面,在計算之前,您可能需要考慮從歌曲標題的開頭刪除諸如「the」之類的詞,因此,例如「愛的力量」和「愛的力量」會得到相同的結果。 – nnnnnn