Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I've been figuring out how to have a typing effect that is word per word instead of letter per letter. So far, all links I've searched are offering letter per letter typewriter effect.
https://macarthur.me/typeit/
https://github.com/mattboldt/typed.js/
Is this possible to achieve? Or have you done something similar?
This is pretty easy.
Just register an interval, split your text, reverse the array and pop the last item. The one you append to the text-container then. Done.
JS
var myText = "Some text you want to be typed automagically..";
var myWords = myText.split(" ").reverse();
var ntrvl = setInterval(function() {
addNextWord();
}, 150);
function addNextWord() {
var nextWord = myWords.pop();
if(nextWord !== undefined) {
var textNow = $(".write-here").text() + " " + nextWord;
$(".write-here").text(textNow);
}
}
What do you think of this?
JSfiddle
create an array with whichever words you want to print one at a time.
const sentence = 'this sentence will be displayed one word at a time';
var arrayOfWords = sentence.split(' ');
for (var i = sentence.length - 1; i >= 0; i--) {
setTimeout(function(){
console.log(sentence[i]); // or display another way
}, 400) // set this to your desired interval
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I'm trying to create a function that inserts a randomly chosen word from an existing array into a div. I figured this thing out and now I need to insert an image at the end of every word generated after pressing a button. Any ideas? thanks!
var words = function() {
var wordsArray = new Array('Sun', 'Door', 'Table', 'Moon', 'Sky', 'Chair');
var i;
for (i = 0; i < wordsArray.length; i++) {
var newWords = wordsArray[Math.floor(Math.random() * wordsArray.length)];
document.getElementById('wordhere').innerText = newWords;
}
};
<button class="generate-button" onclick="words();">generate!</button>
<div class="title h4" id="wordhere"></div>
Inside the loop you can replace the statement with:
document.getElementById("wordhere").innerHTML = `${newWords}<img src = "path">`
Basically you can write any node inside that template literal
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
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.
Improve this question
I have the following
var string = '1,7,12,15,16,29';
if I just want to replace the number 1, i will use the following
removeStr(1);
function removeStr(str1)
{
var string = '1,7,12,15,16,29';
var newstr = string.replace(str1, '');
alert('new val is ' + newstr);
}
But doing this, will end up removing the number 1 in 12,15,16.
How do I just remove the exact match 1 in this example.
Thanks
You could use boundaries (\b) in a regexp that to match a whole word only. Changed your test string to one where your question would be applicable
function removeStr(str1)
{
var string = '11,71,12,1,16,21';
var newstr = string.replace(new RegExp("\\b"+str1+"\\b"), "");
console.log('new val is ' + newstr);
}
removeStr("1");
function replaceOne(str1, str2){
var arr = str2.split(",");
var newStr = "";
for(var i=0; i<arr.length; i++){
if(arr[i]!=str1){
newStr = (newStr=="")?arr[i]:newStr+","+arr[i];
}
}
console.log(newStr);
}
You are trying to do it on strings.
you might consider to make it an array
var string = '1,7,12,15,16,29';
var arr=string.split(",");
var newArr=arr.splice("1");
string=newArr.join(",");
console.log(string);
Hope this helps
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 6 years ago.
Improve this question
I am attempting to build an array whose entries are the letters of any given word. I though I had written a clever bit of code, but it doesn't work in the slightest! And feedback or help would be greatly appreciated:
var inputWord = prompt("PALINDROME CHECKER:");
var numberOfLetters = inputWord.length;
var letters = [];
for(i=0; i++; i<numberOfLetters){
letters[i] = inputWord.substring(i,i+1);
};
Thanks,
CPR
If you want an array with the letters of a string, just split the string with no pattern:
var string = "My string is this";
var array = string.split("");
console.log(array);
Your for loop is wrong. Try:
var inputWord = prompt("PALINDROME CHECKER:");
var numberOfLetters = inputWord.length;
var letters = [];
for(i=0; i<numberOfLetters; i++){
letters[i] = inputWord.substring(i,i+1);
};
The order of the for loop parameters should be iterator, then condition, then action - basically, "for my variable i, if i is less than the number of letters, then increment i"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a left tree navigation with a search box on top.When the user enters some text in the Search box,the matching nodes of the tree navigation should get highlighted.
I want to do it using Java script,Can anyone point me to any such examples or documentation for this.
Thanks
Without any html etc can't really see what your setup is! But I assume you want something like the following:
http://jsfiddle.net/cwc66a3d/3/
Use indexOf to find any matching cases among the options, then using the position given, insert a span to contain the matching text with a yellow background to give the impression of highlighting.
The timeout is simply because of delays in event firing, sure to make sure it highlights in real time it is needed.
document.onkeydown = function () {
setTimeout(function () { //delay so can take the event fire into account
var list = document.getElementsByClassName('tree');
for (var i = 0; i < list.length; i++) {
list[i].innerHTML = list[i].innerText;
var pos = (list[i].innerHTML).indexOf(s.value);
if (s.value !== '' && pos != -1) {
var a = list[i].innerHTML.substring(0, pos) + '<span style="background: yellow;">' + list[i].innerHTML.substring(pos,1+ (list[i].innerHTML).indexOf(s.value.substr(s.value.length-1, 1))) + '</span>' + (list[i].innerHTML).substring(1+ (list[i].innerHTML).indexOf(s.value.substr(s.value.length-1, 1)), (list[i].innerHTML).length);
list[i].innerHTML = a;
}
}
}, 50);
};
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
How to do that witn Pure JavaScript?
I need clear code or just guidelines.
From what I gathered from your question this what I believe you need. Hopefully it will get you going in the right direction.
var textAreaID = "user-input";
//turn the text area content into an array
var content = document.getElementById(textAreaID).innerHTML.split("\n");
//create array to hold new Content
var newContent = [];
//loop through and add line numbers
for (var i = 0; i < content.length; i++) { //begin for loop
//append the line numbers and the new value to the newContent array
newContent.push((i + 1) + content[i] + "\n");
} //end for loop
//update the content of textArea with line numbers
document.getElementById(textAreaID).innerHTML = newContent.join("");
<textarea id="user-input" name="user-input" rows="15" cols="40">
Hello is it working?
I think so.
</textarea>