2016-08-09 74 views
-3

我在一家大學的電臺工作,我們想要一種方法來編號我們的物理音樂。我想把藝術家,發行名稱和年份,並計算成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? } 
+2

歡迎SO!如果你展示了你已經放在一起的代碼,這將是有幫助的。您可能還想閱讀http://stackoverflow.com/help/mcve – silencedmessage

+0

使用['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

+0

另一方面,在計算之前,您可能需要考慮從歌曲標題的開頭刪除諸如「the」之類的詞,因此,例如「愛的力量」和「愛的力量」會得到相同的結果。 – nnnnnn

回答

0

鑑於您對Javascript相當陌生,我會盡我所能避免使用複雜的API。

本質上是去除所有的空間,你將不得不做一個string.replace(...)。 API採用正則表達式模式。所以你會想要做一些像/\s/g這樣的事情來說我想「查找字符串中的所有空格」,第二個參數就是'',即用「'替換它。

之後,您將要遍歷無空格字符串值中的每個字符,只需執行一個簡單的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);

+0

希望這有助於。讓我知道你是否想進一步討論這個 –

+0

他已經編寫了'.replace()'函數。它被稱爲'easyread()'。所以代碼應該是'let nospace = easyread(test)' – slebetman

+0

我並不完全理解for循環中的條件,但其餘部分是可管理的。但是,我們現在如何從這個循環中獲得和雙射(「t」)+雙射(「w」)+ ... +雙射(「u」)+雙射(「b」)? –

相關問題