Show words count in alert box [closed] - javascript

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 days ago.
Improve this question
How can i show the number of words in the alert after hitting the button?
function countWords(self) {
var spaces = self.value.match(/\S+/g);
var words = spaces ? spaces.length : 0;
document.getElementById("wrd-count").innerHTML = words + " words";
}
<textarea onkeyup="countWords(this);"></textarea>
<button type="button" value="words" onclick=alert(document.getElementByID( "wrd-count")>button</button>
<span id="wrd-count"></span>

Related

How to print a concatened sentence? [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 1 year ago.
Improve this question
I am doing an exercise on concatenation.
I have a doubt, I have to print this sentence from Shakespeare "to be or nor to be".
var sentence1 = 'to be';
var sentence2 = 'or not';
console.log(sentence1 + ' ' + ' ' + sentence2 + ' ' + sentence1);
But it gives me an error:
Code is incorrect
The first string should be 'to be'
What seems to be the problem?
You can use the ES6 template literals to make it more cleaner, Try this:
const sentence1 = 'to be';
const sentence2 = 'or not';
console.log(`${sentence1} ${sentence2} ${sentence1}`);

Regex to test an expression [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 3 years ago.
Improve this question
I have this expression:
/width*$/.test(oldStyle)
And oldStyle has this inside of it:
width:90%;height:inherit;padding-left:20px;
The expression should return true as i have tested it in a online JavaScript regex expression tool.
Why is this returning false?
Can i use this expression the way i have coded?
You need to use . to match any character followed by the * to match 1 or more.
const oldStyle = 'width:90%;height:inherit;padding-left:20px;';
const result = /width.*$/.test(oldStyle);
document.getElementById('result').innerHTML = "Result: " + result;
<div id="result"></div>

My prompt won't return a string, it only returns NaN [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 4 years ago.
Improve this question
I'm quite new to this and cannot understand what is going on.
My code is as follows:
let p = prompt("Enter your name, please", "");
let o = prompt("Enter your age, please.", "");
let fname = parseInt(p);
let age = parseInt(o);
let year = (2018 - age);
document.getElementById("p1");
p1.innerHTML = fname + ", you were born in " + year;
When all the is entered on the browser, I end up with something that reads:
"NaN, you were born in 1990"
The number part works, but the string doesn't.
You're running parseInt() on your first name field. Just remove that parseInt call since your name is not an integer!

Email Regex for Javascript Which not start with Number [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 4 years ago.
Improve this question
^[a-zA-Z]\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,4}$
i had tried this regex for email but it allow following cases
123#mail.com
example.mail#mail.com
Here is the code which searches for the number at the start.
If number is matched it prints message in console.
let regex = /^[0-9]/;
let object = [{email:'123#mail.com'},{email:'example.mail#mail.com'}];
for(let i =0;i<object.length;i++){
if(object[i].email.match(regex)){
console.log('E-mail ',object[i].email,' is not valid.')
}
}
This is the used regex: ^[0-9]

How can I make this cost calculator script shorter? [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 8 years ago.
Improve this question
function Method(Distance,Cost,Milage) {
result1=parseInt(Distance/Cost*Milage);
result2=parseInt(Distance/46*Cost);
alert('Your trip will costs '+ result1 +' $');
alert('Your trip in hybrid costs '+ result2 +' $');
if ((result1-result2)<0)
alert('You will save '+(result2-result1)+' $');
else
alert('You will save '+(result1-result2)+' $');
}
}
What can I do to make this shorter?
Yes:
alert("You will save "+Math.abs(result1-result2)+" $");
This will always output the "gap" as a positive number.
As an aside, parseInt is redundant since you are already casting to numbers with the / and * operators.
yes and no, at least you can write shorter :
instead off calling
Method(Distance,Cost,Milage);
everytime.
you could just alert
alert("You will save "+Math.abs((Distance/Cost*Milage)-(Distance/46*Cost)+" $");
everytime

Categories

Resources