我有一個任務:計算文本中字母“a”的數量計算文本中字母“o”的數量寫出字母“a”和“o”的數量相乘的結果。
是否有可能以更短的方式解決此任務?
function countString(str, letter) {
let count = 0;
for (let i = 0; i < str.length; i ) {
if (str.charAt(i) == letter) {
count = 1;
}
}
return count;
}
const string = hello my name is Ola.toLowerCase()
const letterToCheck = "o"
const letterToCheckTwo = "a"
const result = countString(string, letterToCheck);
const resultTwo = countString(string, letterToCheckTwo);
const total = result resultTwo
console.log(total)
uj5u.com熱心網友回復:
用正則運算式函式match()會輸出所有匹配的條件。
const string = "hello my name is Ola"
const numberOfA = string.match(/a/gi);
const numberOfO = string.match(/o/gi);
console.log(numberOfA.length * numberOfO.length)
uj5u.com熱心網友回復:
你可以做這樣的事情使用map filter和reduce
const calculate = (string, letters) => letters
.map(l => string.split('').filter(c => c === l).length)
.reduce((res, item) => res * item)
const string = 'hello my name is Ola'.toLowerCase()
console.log(calculate(string, ['a', 'o']))
console.log(calculate(string, ['a', 'o', 'e']))
uj5u.com熱心網友回復:
您可以創建一個物件來存盤所有字符的計數,然后您可以使用該物件計算產品。
const str = "Avacado";
const charsCount = Array.prototype.reduce.call(
str,
(r, ch) => {
const lowerCh = ch.toLowerCase()
r[lowerCh] ??= r[lowerCh] || 0;
r[lowerCh] = 1;
return r;
},
{}
);
console.log(charsCount["o"] * charsCount["a"]);
注意: Array.prototype.reduce是一個泛型方法,因此它也可以與字串一起使用。
uj5u.com熱心網友回復:
有幾種不同的方法。在這篇文章中,我以一種允許您輕松擴展到不同字母、不同數量的字母和不同字串的方式進行了編程。如果您的問題永遠不需要改變,可能會有更好的方法。
在您當前的代碼中,您將遍歷字串兩次并計算其中出現的字母。您可以輕松地將函式傳遞給陣列,并且只遍歷字串一次:
function countString(str, letters) {
//rather than being a character, letter is now an array of characters
let count = 0;
for (let i = 0; i < str.length; i ) {
if (letters.includes(str.charAt(i))) {
count = 1;
}
}
return count;
}
const string = "hello my name is " Ola.toLowerCase()
const lettersToCheck = ["o", "a"]
const result = countString(string, lettersToCheck);
//iterate over the array to get the total
var product = 1;
for (int i = 0; i < result.length; i ){
product *= result[i];
}
console.log(product);
然而,不是迭代字串,另一種方法是使用正則運算式和.match(). 這種方法的迭代次數較少,盡管我不確定低級效率,因為很多比較都封裝在.match():
function countString(str, letters) {
//iterates over the array of letters rather than the string
for (var i = 0; i < letters.length; i ){
count = (str.match(new RegExp(letters[i], "g")) || []).length;
}
return count;
}
const string = "hello my name is " Ola.toLowerCase()
const lettersToCheck = ["o", "a"]
const result = countString(string, lettersToCheck);
//iterate over the array to get the total
var product = 1;
for (int i = 0; i < result.length; i ){
product *= result[i];
}
console.log(product);
如果正則運算式太多,您還可以使用.split()迭代字符而不是字串:
function countString(str, letters) {
//iterates over the array of letters rather than the string
for (var i = 0; i < letters.length; i ){
count = str.split(letters[i]).length - 1;
}
return count;
}
const string = "hello my name is " Ola.toLowerCase()
const lettersToCheck = ["o", "a"]
const result = countString(string, lettersToCheck);
//iterate over the array to get the total
var product = 1;
for (int i = 0; i < result.length; i ){
product *= result[i];
}
console.log(product);
有關更多資訊,請參閱此帖子
uj5u.com熱心網友回復:
您可以在每次短語更改時計算字母頻率,然后通過在頻率圖中指定字母來請求產品。
const letterFrequencyMap = (str) =>
str.toLowerCase().split('').reduce((acc, k) =>
acc.set(k, (acc.get(k) ?? 0) 1), new Map);
const computeProduct = (freq, ...keys) =>
keys.reduce((product, k) => product * (freq.get(k) ?? 1), 1);
// Main
const phrase = "It's about to be noon in a bit.";
const freq = letterFrequencyMap(phrase);
// a = 2; o = 4
console.log(computeProduct(freq, 'a', 'o')); // 8
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/493431.html
標籤:javascript 细绳
上一篇:根據下拉串列中的用戶選擇執行功能
下一篇:生成具有唯一值的物件陣列
