如何访问循环内的先前值?
How to access previous values inside a loop?
我对 JS 编程有点陌生,如果这是一个明显的问题,我很抱歉。我有一个代码可以计算字符串中重复的字母。代码应该只输出使用两次以上的字母。
例如:
let str = "HELLOWORLD"
输出应为:L = 3, O = 3
我的代码输出:L =3, O = 2, O =2, L = 3
我想去掉第二个O和L
function repeat(){
let str = "helloworld";
let letters = "";
let count = "";
let output = "";
let x = "";
for (i = 0; i <str.length; i++){
letters = str.charAt(i);
count = str.split(letters).length - 1;
if (count >= 2 && x !== letters) {
output += letters +"=" + count + " ";
}
x = letters;
}
console.log(output);
}
repeat();
由于 x = letters;
代码,我只能删除出现在新字母之前的重复字母。我希望这些字符只出现一次,但我很难处理循环。我知道它看起来很乱并且需要改进,但我会在找到解决方案后继续努力。如有任何建议,我们将不胜感激!
你可以这样做:
var strs = "helloworld";
const output = strs.split('').reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
const convertToArray = Object.entries(output);
const filterMorethan2 = convertToArray.filter(([key, value]) => value >= 2);
const converBackObj = Object.fromEntries(filterMorethan2);
console.log(converBackObj)
编码愉快!!!
您可以将重复的字母保存在一个数组中,然后检查该字母是否已经存在。
function repeat(){
let str = "helloworld";
let letters = "";
let count = "";
let output = "";
let x = "";
let multipleChars = [];
for (i = 0; i <str.length; i++){
letters = str.charAt(i);
count = str.split(letters).length - 1;
if (count >= 2 && x !== letters && !multipleChars.includes(letters)) {
output += letters +"=" + count + " ";
multipleChars.push(letters);
}
x = letters;
}
console.log(output);
}
repeat();
一种常用方法是使用 an object to maintain a count of the letters - the letters are the property keys, and the value is the number that gets increased whenever that letter is found. We can then loop over that object 并仅记录那些值大于 2 的属性。
function repeat(str) {
// Create the object
const obj = {};
// Loop over the string
for (let i = 0; i < str.length; i++) {
const letter = str[i];
// If the letter (key) doesn't already exist
// on the object create it, and set the value to zero
// then immediately increase it by one
if (obj[letter] === undefined) {
obj[letter] = 0;
}
obj[letter] += 1;
}
// So we have an object that contains _all_ the
// letters, so now we loop over it to print out
// only those with values greater than two
// We can use a template string:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
for (let letter in obj) {
if (obj[letter] > 2) {
console.log(`${letter} = ${obj[letter]}`);
}
}
}
repeat('helloworld');
使用你的 js 工具箱:
let str = 'HelloWorld';
let a = Object.entries([...str].reduce((r,c)=>{
r[c]=(r[c] || 0) +1;
return r;
},{})).filter(e=>e[1]>2).sort(([,a],[,b]) => b-a).reduce((s,i,idx)=>(`${s}${idx>0 ? ", " : ""}${i.join('=')}`),'');
console.log(a);
我对 JS 编程有点陌生,如果这是一个明显的问题,我很抱歉。我有一个代码可以计算字符串中重复的字母。代码应该只输出使用两次以上的字母。
例如:
let str = "HELLOWORLD"
输出应为:L = 3, O = 3
我的代码输出:L =3, O = 2, O =2, L = 3
我想去掉第二个O和L
function repeat(){
let str = "helloworld";
let letters = "";
let count = "";
let output = "";
let x = "";
for (i = 0; i <str.length; i++){
letters = str.charAt(i);
count = str.split(letters).length - 1;
if (count >= 2 && x !== letters) {
output += letters +"=" + count + " ";
}
x = letters;
}
console.log(output);
}
repeat();
由于 x = letters;
代码,我只能删除出现在新字母之前的重复字母。我希望这些字符只出现一次,但我很难处理循环。我知道它看起来很乱并且需要改进,但我会在找到解决方案后继续努力。如有任何建议,我们将不胜感激!
你可以这样做:
var strs = "helloworld";
const output = strs.split('').reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
const convertToArray = Object.entries(output);
const filterMorethan2 = convertToArray.filter(([key, value]) => value >= 2);
const converBackObj = Object.fromEntries(filterMorethan2);
console.log(converBackObj)
编码愉快!!!
您可以将重复的字母保存在一个数组中,然后检查该字母是否已经存在。
function repeat(){
let str = "helloworld";
let letters = "";
let count = "";
let output = "";
let x = "";
let multipleChars = [];
for (i = 0; i <str.length; i++){
letters = str.charAt(i);
count = str.split(letters).length - 1;
if (count >= 2 && x !== letters && !multipleChars.includes(letters)) {
output += letters +"=" + count + " ";
multipleChars.push(letters);
}
x = letters;
}
console.log(output);
}
repeat();
一种常用方法是使用 an object to maintain a count of the letters - the letters are the property keys, and the value is the number that gets increased whenever that letter is found. We can then loop over that object 并仅记录那些值大于 2 的属性。
function repeat(str) {
// Create the object
const obj = {};
// Loop over the string
for (let i = 0; i < str.length; i++) {
const letter = str[i];
// If the letter (key) doesn't already exist
// on the object create it, and set the value to zero
// then immediately increase it by one
if (obj[letter] === undefined) {
obj[letter] = 0;
}
obj[letter] += 1;
}
// So we have an object that contains _all_ the
// letters, so now we loop over it to print out
// only those with values greater than two
// We can use a template string:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
for (let letter in obj) {
if (obj[letter] > 2) {
console.log(`${letter} = ${obj[letter]}`);
}
}
}
repeat('helloworld');
使用你的 js 工具箱:
let str = 'HelloWorld';
let a = Object.entries([...str].reduce((r,c)=>{
r[c]=(r[c] || 0) +1;
return r;
},{})).filter(e=>e[1]>2).sort(([,a],[,b]) => b-a).reduce((s,i,idx)=>(`${s}${idx>0 ? ", " : ""}${i.join('=')}`),'');
console.log(a);