Count repeated words using javascript
I am trying to solve this exercise but i a little bit stuck, this is what i have right now, i am trying to iterate over the string incrementing the index plus one per word
"Write a function to perform basic string compression using the counts
of repeated characters e.g "aabcccccaaa" would become "a2b1c5a3", if
the compressed string would not become smaller than the original, just
print the original"
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 0;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 0;
i++;
} else {
counter++;
i++;
}
}
console.log(result);
if (result.length < word.length)
console.log(result)
else
console.log(word);
}
console.log(countWords())
javascript
add a comment |
I am trying to solve this exercise but i a little bit stuck, this is what i have right now, i am trying to iterate over the string incrementing the index plus one per word
"Write a function to perform basic string compression using the counts
of repeated characters e.g "aabcccccaaa" would become "a2b1c5a3", if
the compressed string would not become smaller than the original, just
print the original"
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 0;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 0;
i++;
} else {
counter++;
i++;
}
}
console.log(result);
if (result.length < word.length)
console.log(result)
else
console.log(word);
}
console.log(countWords())
javascript
Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28
How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28
4
You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29
add a comment |
I am trying to solve this exercise but i a little bit stuck, this is what i have right now, i am trying to iterate over the string incrementing the index plus one per word
"Write a function to perform basic string compression using the counts
of repeated characters e.g "aabcccccaaa" would become "a2b1c5a3", if
the compressed string would not become smaller than the original, just
print the original"
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 0;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 0;
i++;
} else {
counter++;
i++;
}
}
console.log(result);
if (result.length < word.length)
console.log(result)
else
console.log(word);
}
console.log(countWords())
javascript
I am trying to solve this exercise but i a little bit stuck, this is what i have right now, i am trying to iterate over the string incrementing the index plus one per word
"Write a function to perform basic string compression using the counts
of repeated characters e.g "aabcccccaaa" would become "a2b1c5a3", if
the compressed string would not become smaller than the original, just
print the original"
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 0;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 0;
i++;
} else {
counter++;
i++;
}
}
console.log(result);
if (result.length < word.length)
console.log(result)
else
console.log(word);
}
console.log(countWords())
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 0;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 0;
i++;
} else {
counter++;
i++;
}
}
console.log(result);
if (result.length < word.length)
console.log(result)
else
console.log(word);
}
console.log(countWords())
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 0;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 0;
i++;
} else {
counter++;
i++;
}
}
console.log(result);
if (result.length < word.length)
console.log(result)
else
console.log(word);
}
console.log(countWords())
javascript
javascript
edited Mar 3 '17 at 18:48
Jonathan Portorreal
1,3762725
1,3762725
asked Mar 3 '17 at 17:25
Gilberto Langarica
175
175
Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28
How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28
4
You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29
add a comment |
Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28
How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28
4
You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29
Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28
Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28
How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28
How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28
4
4
You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29
You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29
add a comment |
6 Answers
6
active
oldest
votes
You can use the power of Regex, reduce in array and conditional ternary.
function compress(input) {
var re = /(.)1+|./gi;
var match = input.match(re);
var output = match.reduce(function (previousValue, currentValue) {
return previousValue + (currentValue.charAt(0) + currentValue.length);
}, "");
output = (output.length < input.length) ? output : input;
return output;
}
console.log(compress("aabcccccaaa"));
add a comment |
You have a couple of things going on.
First, you are incrementing your loop index twice. Remove the i++
from your if
statement.
Secondly, you need to initialize your counter to 1 not 0.
This code seems to work.
<script>
function countWords()
{
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for(var i = 0; i <= word.length; i++) {
if(word.substr(i, 1) === word.substr(i+1, 1)) {
counter++;
}
else {
result = result + word.substr(i, 1) + counter;
counter = 1;
}
}
console.log(result);
if(result.length < word.length)
console.log(result)
else
console.log(word);
}
countWords();
</script>
I changed the logic from!=
to===
just to make it more clear to me but I think the other way would work as well.
– clarmond
Mar 3 '17 at 17:53
add a comment |
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
add a comment |
It could be done with reduce function:
let word = 'aabcccccaaa';
let chars = word.split('');
let counter = 1;
let occur = 0;
let result = chars.reduce(function(prevValue, currValue, index, array) {
let isLastChar = array.length - 1 === index;
if (prevValue.substr(prevValue.length - 1) !== currValue) {
occur = counter;
counter = 1;
prevValue = prevValue + occur + currValue;
return isLastChar ? prevValue + 1 : prevValue
} else {
counter++;
return isLastChar ? prevValue + counter : prevValue;
}
});
add a comment |
Here's your code with these bugs fixed.
counter
starting at0
so the count would be off by one.- similarly to
1)
, settingcounter
back to0
each time instead of1
. - incrementing
i
inside the for loop so values were being skipped.
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
add a comment |
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42585019%2fcount-repeated-words-using-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the power of Regex, reduce in array and conditional ternary.
function compress(input) {
var re = /(.)1+|./gi;
var match = input.match(re);
var output = match.reduce(function (previousValue, currentValue) {
return previousValue + (currentValue.charAt(0) + currentValue.length);
}, "");
output = (output.length < input.length) ? output : input;
return output;
}
console.log(compress("aabcccccaaa"));
add a comment |
You can use the power of Regex, reduce in array and conditional ternary.
function compress(input) {
var re = /(.)1+|./gi;
var match = input.match(re);
var output = match.reduce(function (previousValue, currentValue) {
return previousValue + (currentValue.charAt(0) + currentValue.length);
}, "");
output = (output.length < input.length) ? output : input;
return output;
}
console.log(compress("aabcccccaaa"));
add a comment |
You can use the power of Regex, reduce in array and conditional ternary.
function compress(input) {
var re = /(.)1+|./gi;
var match = input.match(re);
var output = match.reduce(function (previousValue, currentValue) {
return previousValue + (currentValue.charAt(0) + currentValue.length);
}, "");
output = (output.length < input.length) ? output : input;
return output;
}
console.log(compress("aabcccccaaa"));
You can use the power of Regex, reduce in array and conditional ternary.
function compress(input) {
var re = /(.)1+|./gi;
var match = input.match(re);
var output = match.reduce(function (previousValue, currentValue) {
return previousValue + (currentValue.charAt(0) + currentValue.length);
}, "");
output = (output.length < input.length) ? output : input;
return output;
}
console.log(compress("aabcccccaaa"));
edited Mar 3 '17 at 19:40
Donald Duck
3,960123958
3,960123958
answered Mar 3 '17 at 18:59
Cleudson Cunha
212
212
add a comment |
add a comment |
You have a couple of things going on.
First, you are incrementing your loop index twice. Remove the i++
from your if
statement.
Secondly, you need to initialize your counter to 1 not 0.
This code seems to work.
<script>
function countWords()
{
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for(var i = 0; i <= word.length; i++) {
if(word.substr(i, 1) === word.substr(i+1, 1)) {
counter++;
}
else {
result = result + word.substr(i, 1) + counter;
counter = 1;
}
}
console.log(result);
if(result.length < word.length)
console.log(result)
else
console.log(word);
}
countWords();
</script>
I changed the logic from!=
to===
just to make it more clear to me but I think the other way would work as well.
– clarmond
Mar 3 '17 at 17:53
add a comment |
You have a couple of things going on.
First, you are incrementing your loop index twice. Remove the i++
from your if
statement.
Secondly, you need to initialize your counter to 1 not 0.
This code seems to work.
<script>
function countWords()
{
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for(var i = 0; i <= word.length; i++) {
if(word.substr(i, 1) === word.substr(i+1, 1)) {
counter++;
}
else {
result = result + word.substr(i, 1) + counter;
counter = 1;
}
}
console.log(result);
if(result.length < word.length)
console.log(result)
else
console.log(word);
}
countWords();
</script>
I changed the logic from!=
to===
just to make it more clear to me but I think the other way would work as well.
– clarmond
Mar 3 '17 at 17:53
add a comment |
You have a couple of things going on.
First, you are incrementing your loop index twice. Remove the i++
from your if
statement.
Secondly, you need to initialize your counter to 1 not 0.
This code seems to work.
<script>
function countWords()
{
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for(var i = 0; i <= word.length; i++) {
if(word.substr(i, 1) === word.substr(i+1, 1)) {
counter++;
}
else {
result = result + word.substr(i, 1) + counter;
counter = 1;
}
}
console.log(result);
if(result.length < word.length)
console.log(result)
else
console.log(word);
}
countWords();
</script>
You have a couple of things going on.
First, you are incrementing your loop index twice. Remove the i++
from your if
statement.
Secondly, you need to initialize your counter to 1 not 0.
This code seems to work.
<script>
function countWords()
{
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for(var i = 0; i <= word.length; i++) {
if(word.substr(i, 1) === word.substr(i+1, 1)) {
counter++;
}
else {
result = result + word.substr(i, 1) + counter;
counter = 1;
}
}
console.log(result);
if(result.length < word.length)
console.log(result)
else
console.log(word);
}
countWords();
</script>
edited Mar 3 '17 at 17:52
answered Mar 3 '17 at 17:44
clarmond
33217
33217
I changed the logic from!=
to===
just to make it more clear to me but I think the other way would work as well.
– clarmond
Mar 3 '17 at 17:53
add a comment |
I changed the logic from!=
to===
just to make it more clear to me but I think the other way would work as well.
– clarmond
Mar 3 '17 at 17:53
I changed the logic from
!=
to ===
just to make it more clear to me but I think the other way would work as well.– clarmond
Mar 3 '17 at 17:53
I changed the logic from
!=
to ===
just to make it more clear to me but I think the other way would work as well.– clarmond
Mar 3 '17 at 17:53
add a comment |
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
add a comment |
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
add a comment |
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
function countChars(word) {
if (!word) {
return word;
}
var arr = word.split('');
var currentChar = arr[0];
var count = 1;
var compression = "";
for (var i = 1; i < arr.length; i++) {
if (arr[i] === currentChar) {
count++;
} else {
compression += currentChar + count;
currentChar = arr[i];
count = 1;
}
}
compression += currentChar + count;
if (compression.length > word.length) {
return word;
}
return compression;
}
console.log(countChars("aabcccccaaa"));
answered Mar 3 '17 at 17:55
beta-developper
1,321814
1,321814
add a comment |
add a comment |
It could be done with reduce function:
let word = 'aabcccccaaa';
let chars = word.split('');
let counter = 1;
let occur = 0;
let result = chars.reduce(function(prevValue, currValue, index, array) {
let isLastChar = array.length - 1 === index;
if (prevValue.substr(prevValue.length - 1) !== currValue) {
occur = counter;
counter = 1;
prevValue = prevValue + occur + currValue;
return isLastChar ? prevValue + 1 : prevValue
} else {
counter++;
return isLastChar ? prevValue + counter : prevValue;
}
});
add a comment |
It could be done with reduce function:
let word = 'aabcccccaaa';
let chars = word.split('');
let counter = 1;
let occur = 0;
let result = chars.reduce(function(prevValue, currValue, index, array) {
let isLastChar = array.length - 1 === index;
if (prevValue.substr(prevValue.length - 1) !== currValue) {
occur = counter;
counter = 1;
prevValue = prevValue + occur + currValue;
return isLastChar ? prevValue + 1 : prevValue
} else {
counter++;
return isLastChar ? prevValue + counter : prevValue;
}
});
add a comment |
It could be done with reduce function:
let word = 'aabcccccaaa';
let chars = word.split('');
let counter = 1;
let occur = 0;
let result = chars.reduce(function(prevValue, currValue, index, array) {
let isLastChar = array.length - 1 === index;
if (prevValue.substr(prevValue.length - 1) !== currValue) {
occur = counter;
counter = 1;
prevValue = prevValue + occur + currValue;
return isLastChar ? prevValue + 1 : prevValue
} else {
counter++;
return isLastChar ? prevValue + counter : prevValue;
}
});
It could be done with reduce function:
let word = 'aabcccccaaa';
let chars = word.split('');
let counter = 1;
let occur = 0;
let result = chars.reduce(function(prevValue, currValue, index, array) {
let isLastChar = array.length - 1 === index;
if (prevValue.substr(prevValue.length - 1) !== currValue) {
occur = counter;
counter = 1;
prevValue = prevValue + occur + currValue;
return isLastChar ? prevValue + 1 : prevValue
} else {
counter++;
return isLastChar ? prevValue + counter : prevValue;
}
});
edited Mar 3 '17 at 19:24
answered Mar 3 '17 at 18:52
Zacol
785
785
add a comment |
add a comment |
Here's your code with these bugs fixed.
counter
starting at0
so the count would be off by one.- similarly to
1)
, settingcounter
back to0
each time instead of1
. - incrementing
i
inside the for loop so values were being skipped.
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
add a comment |
Here's your code with these bugs fixed.
counter
starting at0
so the count would be off by one.- similarly to
1)
, settingcounter
back to0
each time instead of1
. - incrementing
i
inside the for loop so values were being skipped.
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
add a comment |
Here's your code with these bugs fixed.
counter
starting at0
so the count would be off by one.- similarly to
1)
, settingcounter
back to0
each time instead of1
. - incrementing
i
inside the for loop so values were being skipped.
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
Here's your code with these bugs fixed.
counter
starting at0
so the count would be off by one.- similarly to
1)
, settingcounter
back to0
each time instead of1
. - incrementing
i
inside the for loop so values were being skipped.
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
function countWords() {
var word = "aabcccccaaa";
var result = "";
var counter = 1;
for (var i = 0; i <= word.length; i++) {
if (word[i] != word[i + 1]) {
result = result + word[i] + counter;
counter = 1;
} else {
counter++;
}
}
return result.length < word.length ? result : word;
}
console.log(countWords());
edited Mar 3 '17 at 19:49
answered Mar 3 '17 at 19:19
Jonathan Portorreal
1,3762725
1,3762725
add a comment |
add a comment |
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
add a comment |
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
add a comment |
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
var inputString = "Javascript",
searchChar, count = 0;
accurancesRepeat(inputString);
function accurancesRepeat(inputString) {
for (var i = 0; i < inputString.length; i++) {
searchChar = inputString[i];
for (var j = 0; j < inputString.length; j++) {
if (searchChar == inputString[j]) {
count += 1;
}
}
console.log("Accorances of character " + searchChar + " is... " + count);
count = 0;
}
}
edited Nov 21 '18 at 10:54
barbsan
2,20811122
2,20811122
answered Nov 21 '18 at 10:32
Regan Amburose
32
32
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f42585019%2fcount-repeated-words-using-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not a question. Define your problem.
– Fabian Klötzl
Mar 3 '17 at 17:28
How isn't your code working? What part do you need help with?
– qxz
Mar 3 '17 at 17:28
4
You might find this helpful: stackoverflow.com/help/how-to-ask
– let_the_coding_begin
Mar 3 '17 at 17:29