Adding percentages with two letters in strings with JavaScript arrays

时间:2015-09-01 22:31:09

标签: javascript arrays percentage addition

I'm using the following table with percentages that show whether or not the letter after a certain one will be a vowel or a consonant.

a: C= 90%, V=  7% 
b: C= 32%, V= 65% 
c: C= 40%, V= 59% 
d: C= 17%, V= 77% 
e: C= 79%, V= 14% 
f: C= 31%, V= 68% 
g: C= 35%, V= 57% 
h: C=  8%, V= 85% 
i: C= 82%, V= 16% 
j: C=  0%, V= 98% 
k: C= 18%, V= 71% 
l: C= 26%, V= 74% 
m: C= 14%, V= 83% 
n: C= 49%, V= 47% 
o: C= 79%, V= 15% 
p: C= 43%, V= 57% 
q: C=  0%, V=100% 
r: C= 31%, V= 64% 
s: C= 61%, V= 36% 
t: C= 28%, V= 67% 
u: C= 90%, V=  9% 
v: C=  1%, V= 99% 
w: C= 22%, V= 71% 
x: C= 15%, V= 72% 
y: C= 58%, V= 24% 
z: C=  7%, V= 91%

For example, there is a 58% chance that the letter after y will be a consonant.

I have an array with the following values:

var arr = ["Cookie", "Bird", "Flower"];

I need for JavaScript to go through each element and take each work and add up percentages if they are not in the majority. For example:


Cookie:

CO <- Because a C going to an O is within the majority, 59%, nothing will be added.

OO <- Because an O going to an O (Vowel to vowel) is not in the majority (only 15% of cases) 79% will be added

OK <- Because its an O going to a K (vowel to consonant), nothing will be added.

KI <- Because its a K going to a I (consonant to vowel), nothing will be added.

IE <- Because an I going to an E (Vowel to vowel) is not in the majority (only 16% of cases) 82% will be added


In the end, it should be 79% + 82% = 161. I have the following code that works on a string, not an array.

var str = 'Cookie';
for (var i = 0, len = str.length; i < len; i++) {
  console.log(str[i] + str[i + 1]);
}

This gives the following output in the console:

CO 
OO
OK
KI
IE

So I have the basic functionality with the string down, but how would I get it to work with an array and add the percentages?

3 个答案:

答案 0 :(得分:1)

我们可以使用Array.prototype.reduce来获得非常干净的prev,当前变量,以便我们可以非常轻松地比较转换。您只需要检查当前字符是否实际上是元音。然后检查转换的概率,如果它高于50,则使用与转换相反的方法并将其添加到累加器。就是这样。

但有一件事,我并没有涵盖一系列字符串的迭代。主要是因为我没有在对象中拥有整个统计信息,所以我只能提供一个工作字符串。

&#13;
&#13;
var options = {
  c: {
    C: 40,
    V: 59
  },
  o: {
    C: 79,
    V: 15
  },
  k: {
    C: 18,
    V: 71
  },
  i: {
    C: 82,
    V: 16
  },
  e: {
    C: 79,
    V: 14
  }
}

var vowels = {
  a : true,
  e : true,
  i : true,
  o : true,
  u : true
  }

function isVowel(chr){
  if(vowels[chr] !== undefined){
    return true;
    } else {
      return false;
      }
  }

var results = document.getElementById('results');
var string = 'Cookie';
var acc = 0;
Array.prototype.reduce.call(string, function(prev, current) {
  if (prev != '') {
    var key = isVowel(current) ? 'V' : 'C';
    var keyOpp = key == 'V' ? 'C' : 'V';
    var letter = options[prev.toLowerCase()];
    var transition = letter[key];
    results.innerHTML += (prev + current + '\n').toUpperCase();
    var opp = letter[keyOpp];
    if(transition < 50){
       acc += opp;
      }
  }
  return current;
}, '');

results.innerHTML += 'Sum : ' + acc;
&#13;
<pre id="results"></pre>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

To apply a function to an array, you can use the ES5 Array.prototype.forEach like so:

arr.forEach(function () {
    for (var i = 0, len = str.length; i < len; i++) {
      console.log(str[i] + str[i + 1]);
    }
}

This is all well and good when just printing to console, but since you seem to want to be mapping from your words to scores, you should check out Array.prototype.map.

As for checking whether or not you've hit a consonant or a vowel, you should declare var vowels = ['a', 'e', 'i', 'o', 'u'] and use Array.prototype.indexOf to check whether you have a vowel. You can then use boolean operators to find out whether vowels or consonants are in majority and create your score function from there.

Update: x in object is better than Array.prototype.indexOf

You should store vowels as keys in a JavaScript object:

vowels = {'a': true, 'e': true, 'i': true, 'o': true, 'u': true}

Then 'a' in vowels returns true and 'b' in vowels returns false. This is much faster than calling indexOf on an array.

Note that the value you assign doesn't really matter here, but it does allow for evaluating vowels['a'] instead of 'a' in vowels, though I believe this will be a more expensive operation, but perhaps you'll want to conditionally discount e for whatever reason: you can then set vowels['e'] = false.

答案 2 :(得分:0)

Here's one way to iterate an array in JavaScript, very similar to how you iterated the string.

var array = ['Cookie','Bird','Flower'];
for (var j = 0, j = array.length; j++) {
  var str = array[j];
  //...
}

Within this loop, you can iterate through the string and apply your logic

var array = ['Cookie','Bird','Flower'];
for (var j = 0, j = array.length; j++) {
  var str = array[j];

  for (var i = 0, i < str.length; i++) {
     // your logic  
  }
}
相关问题