How do I check spelling of a user input? [closed] - javascript

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 2 years ago.
Improve this question
I'm creating a website where the user types whatever he hears in an audio file. On submit, I want to compare his input to the actual paragraph and then display the misspelled words. How can I do that?

// answerParagraph is the true answer (string)
// inputParagraph is the input from the user (string)
let mistakes = 0;
let mistakedWords = [];
for(let i = 0; i < answerParagraph.length; i++){
if(answerParagraph[i] != inputParagraph[i]){
mistakes++
mistakedWords.push(answerParagraph[i])
}
}
alert("You have " + mistakes + " typos!")
I think this should work. :)
Edit: Well if you want to check every word one by one instead of the whole paragraph then the code is:
// answerParagraph is the true answer (string)
// inputParagraph is the input from the user (string)
let mistakes = 0;
let answerWords = answerParagraph.split(" ")
let inputWords = inputParagraph.split(" ")
for(let i = 0; i < answerWords.length; i++){
if(answerWords[i] != inputWords[i]){
mistakes++
}
}
alert("You have mistakes in " + mistakes + " words!")

Related

Invalid expression term '[' using c# [closed]

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 1 year ago.
Improve this question
Can someone assist me what's wrong in my code. I'm try to convert JavaScript code to C# code
public class SplitString
{
public static string[] Solutions(string str)
{
arr = [];
for(var i = 0; i < str.Length; i += 2){
second = str[i+1] || '_';
arr.push(str[i] + second);
}
return arr;
}
}
And i encounter this error
src/Solution.cs(5,11): error CS1525: Invalid expression term '['
src/Solution.cs(5,12): error CS0443: Syntax error; value expected
javascript and C# are completely different languages; .NET / C# arrays are fixed size - so, you might want a list here:
var arr = new List<string>();
for(var i = 0; i < str.Length; i += 2){
var second = str[i+1]; // || '_'; <== this on the right makes no sense in C#
arr.Add(str[i] + second);
}
return arr.ToArray();

Why does random number generator not show on html page? [closed]

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 2 years ago.
Improve this question
I am trying to get a random number of customers to "have gone on the website today". Why does the number not show on the page. I am sorry if this is something obvious but I am very new.
var customerNumber = document.getElementById("customer-number")
customerNumber.textcontent = randomCustomerNumber()
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36)
return(i)
}
<div class="customers-today">
<h4>You are customer number <span id="customer-number"></span> today.</h4>
</div>
element.textcontent => element.textContent
Also you should prob change return(i) to return i;, both work but the first one makes it seem like return is a function.
You need to use textContent instead of textcontent :
var customerNumber = document.getElementById("customer-number")
customerNumber.textContent = randomCustomerNumber()
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36)
return(i)
}
<div class="customers-today">
<h4>You are customer number <span id="customer-number"></span> today.</h4>
</div>
Try doing:
var customerNumber = document.getElementById("customer-number");
customerNumber.innerHTML = randomCustomerNumber();
function randomCustomerNumber(){
var i = Math.floor(Math.random() * 36);
return i;
}

How can I replace the text between two delimiters with a blank? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a message, received via an input field. For example:
This is *red* colour.
I want to replace everything between the two asterisks with a blank line ("___") so the outcome would be:
This is ___ colour.
How can I achieve this?
function myFunction() {
var str = "This is *red* color";
var startIndex = nthIndex(str,'*',1);
var endIndex = nthIndex(str,'*',2);
var output = str.replace(str.substring(startIndex, (endIndex+1)), "_");
}
function nthIndex(str, pat, n){
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}

What alternative for padStart [closed]

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 4 years ago.
Improve this question
I'm using padStart in javascript but it's not working for internet explorer
I would like to know if someone know an alternative to replace this.
Thank you
There's a polyfill at the MDN:
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength,padString) {
targetLength = targetLength>>0; //truncate if number or convert non-number to 0;
padString = String((typeof padString !== 'undefined' ? padString : ' '));
if (this.length > targetLength) {
return String(this);
}
else {
targetLength = targetLength-this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength/padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0,targetLength) + String(this);
}
};
}
You can copy it in your script.

How to restore sentence in javascript [closed]

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 this
var input = "([lazy({(jumps{fox([quick(The)]brown)})over}the)]dog)";
I want to get
The quick brown fox jumps over the lazy dog.
Any ideas? I tried to use RegEx but can not find.
We need to have stack kind of approach with the array here. See the below implementation.
var res = "([lazy({(jumps{fox([quick(The)]brown)})over}the)]dog)".split("");
var txt = [],lvl=-1;
res.forEach(function(e,i){
if(e=='('||e=='{'||e=='['){
lvl++;
} else if(e==')'||e=='}'||e==']'){
lvl--;
} else {
if(typeof txt[lvl]=='undefined'){
txt[lvl] = e;
} else {
txt[lvl] = txt[lvl] + e;
}
}
});
txt = txt.reverse().join(" ");
console.log(txt);
if(lvl!=-1) {
//this will alert if any missing parenthesis
alert("Pattern error in input");
}
Edit : updated as per Question owner's description of the pattern in input.

Categories