Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
find consecutive occurrences of 1
say i have an string
var str = "11101111110";
so my result would be
from 011111101111110
to 01111101011111010
can anyone help my generate a code that would get that result?
Simply replace it using regex
var str = "011111101111110";
str = str.replace(/(1{5})/g, '$10');
console.log(str);
var str = "011111101111110";
var count = 0;
for(var i = 0; i < str.length; i++){
if(count == 5){
str = str.substring(0,i) +"0"+str.substring(i);
}
if(str.charAt(i) == 1){
count++;
} else {
count = 0;
}
}
if(count == 5){
str = str + "0";
}
console.log(str);
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I want to add one space in between each outcome in this for loop,
for (var i = -52; i <= 1066; i++) {
document.write(i)
}
You can add to do it
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
Or just add to space in the write
for (var i = -52; i <= 1066; i++) {
document.write(i+" ");
}
this should help you out.
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
or
for (var i = -52; i <= 1066; i++) {
document.write(' ' + i)
}
Browsers are designed to ignore whitespace such as spaces, typing in multiple ' ' (spaces) will be reduced to a single space. If you're looking to add multiple spaces in Javascript the easiest way is to use  . Such tags are called entities and numerous characters can be printed using entities. More can be found here
for(var i = 0;i < n;i++)
{
document.write(" ")
}
for (var i = 52; i <= 1066; i++) {
document.write(i+'\t') }
Try this:
let result = [];
for (let i = -52; i <= 1066; i++) {
result.push(i);
}
document.write(result.join(" "));
Suggestion: avoid using document.write at best, especially in a big for loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I wanted to make a basic word counter based on the amount of whitespaces in a sentence, but for some reason it doesn't work.
function countWords(str) {
if (typeof str == "string" && str.length > 0) {
var counter = 1;
for (var i; i < str.length; i++) {
if (str[i] === " ") {
counter += 1;
}
}
return counter;
} else {
throw Error("You must input a string.");
}
}
console.log(countWords("hello World"));
This throws 1 instead of 2.
You shouldn't use a loop for this. You would rather just split the string by space and take the resulting array's length
let countWords = str => str.split(' ').length;
console.log(countWords("foo bar"));
Initialize i to zero.
Replace for (var i; with for (var i=0;
You must initilize the counter inside the for, like var i = 0; here is your code
function countWords(str) {
if (typeof str=="string" && str.length>0) {
var counter=1;
for (var i;i<str.length;i++) {
if (str[i]===" ") {
counter+=1;
}
}
return counter;
}
else {
throw Error("You must input a string.");
}
}
countWords("hello World");
Or you can count words with str.split(" ").length
Your for loop was wrong
function countWords(str) {
if (typeof str=="string" && str.length>0) {
var counter=1;
for (var i = 0;i<str.length;i++) {
if (str[i]===" ") {
counter+=1;
}
}
return counter;
}
else {
throw Error("You must input a string.");
}
}
var str = "hello World this is me";
console.log(countWords(str));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to find the longest even word from a sentence.
I've tried this code for finding the largest word.
But I need the even word.
Can anyone please help me?
function FindlongestWord(input) {
var arrWords = input.split(' ');
var wordlength = 0;
var word = '';
arrWords.forEach(function(wrd) {
if (wordlength < wrd.length) {
wordlength = wrd.length;
word = wrd;
}
});
return word;
}
Using modulus operator in the if statement
arrWords.forEach(function(wrd) {
if (wordlength < wrd.length && wrd.length % 2 == 0) {
wordlength = wrd.length;
word = wrd;
}
});
function FindlongestWord(input) {
var arrWords = input.split(' ');
var wordlength = 0;
var word = '';
arrWords.forEach(function(wrd) {
if (wordlength < wrd.length && !wrd.length%2) {
wordlength = wrd.length;
word = wrd;
}
});
return word;
}
Alternative solution.
const longestWord = (str) => str.split(' ').filter(v => !(v.length % 2))
.sort((a,b) => b.length - a.length)[0];
console.log(longestWord('hi there hello four longer longestt'));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Hey I am trying to print out #, by increasing the amount of # for each line. like this:
#
##
###
####
#####
Here is the for loop that I have tried to work this out with:
var printout = "";
for(var i=0;i<5;i++) {
printout+= "#" + " <br>";
You can add one more variable for incrementing #
var printout = "", a = '';
for (var i = 0; i < 5; i++) {
printout += (a += '#') + " <br>";
}
document.body.innerHTML += printout;
If you're just trying to print them:
for (var i=0; i < 5; i++) {
console.log("#".repeat(i+1))
}
Check this out, output will be in HTML.
var printout = "";
for(var i=0;i<5;i++) {
for(var j=0; j<=i; j++)
{
printout += "#";
}
printout += " <br>";
}
var pvar = document.getElementById("check");
pvar.innerHTML = printout;
<p id="check">
</p>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working on a piece of code which make an array full of numbers with the amount of numbers i want, but instead of this beeing static with 16 numbers i tried to change 16 to a variable but the math.floor/randome cant read it it only spits out Not a number wierdly enough.
EDIT: with 16 put in it works, but i cant use a variable (declared in the same function ofc)after i console.log the variable it shows it as a number but then my browser freezes
Is There anyone who knows how to change this
while(arr.length < pictures.length) {
var randomenumber = Math.floor((Math.random()* 16));
if(arr.indexOf(randomenumber) > -1) {
continue;
}
arr[arr.length] = randomenumber;
}
//cheat sheet
for(var i = 0; i < arr.length ; i++) {
document.write(arr[i]);
document.write("<br/>");
}
I don't see any issues if you use var length = 16 and Math.floor((Math.random() * length)).
Working snippet:
var arr = [], length = 16;
while(arr.length < length) {
var randomenumber = Math.floor((Math.random() * length));
if(arr.indexOf(randomenumber) > -1) {
continue;
}
arr[arr.length] = randomenumber;
}
//cheat sheet
for(var i = 0; i < arr.length ; i++) {
document.write(arr[i]);
document.write("<br/>");
}
You will need to parse it in integer using parseInt.
var num = 16;
var randomenumber = Math.floor((Math.random() * parseInt(num)));