How to print a concatened sentence? [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 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}`);

Related

Show words count in alert box [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 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>

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!

Javascript programming error [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 6 years ago.
Improve this question
I want to print the following value in console,
root 1 = "The number to be printed" //
my code is
console.log("root 1 = " root1);
root1 variable contains the value of root 1, can't locate the error in my code, please help
It should be
console.log("root 1 = " + root1); //+ was missing here
You need to concatenate the variable value (root1) with the string literal "root 1 = ".
This is because as per spec when JS engine encountered the closing double quotes, it is either looking for a comma ,, a closing brace ) , operators like + - * /, or closing square bracket ] if a property is accessed.
console.log("root 1 = " , root1);
You are also missing the variable name deceleration you can't define variable name with space like "root 1" it should be like "root1", "root-1" ....
and second one is concat sign
console.log("root1" + root1);
You should remove the space between root and 1
root1 = "The number to be printed"
And also use + sign for appending:
console.log("root 1 = " + root1);

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