How to had space when concatenation a JavaScript sentence [duplicate] - javascript

This question already has answers here:
Joining two strings with a comma and space between them
(9 answers)
Closed 3 years ago.
I am inserting specific word into a sentence with concatenation but the words a running together with no space between them.
var adjective1 = + 'amazing';
var adjective2 = + 'fun';
var adjective3 = + 'entertaining';
var madLib = 'The Intro to JavaScript course is' + adjective1 + 'james and Julia are so' +
adjective2 + 'I cannot wait to work through the rest of this' + adjective3 + 'content!';
console.log(madLib);

Just add spaces
var madLib = 'The Intro to JavaScript course is ' + adjective1 + ' james and Julia are so' +
adjective2 + 'I cannot wait to work through the rest of this ' + adjective3 + ' content!';
You also shouldn't have + before the strings in the variable assignments. That will try to convert the strings to numbers, and will produce NaN since they're not numeric.
var adjective1 = 'amazing';
var adjective2 = 'fun';
var adjective3 = 'entertaining';

Related

Adding the first 3 words in a string to the last 3 words in a string and skip what's in the middle in JS [duplicate]

This question already has answers here:
How to get first 2 words?
(3 answers)
Closed 2 months ago.
How can I get the first 3 words from a string and add them to the last 3 words of the same string and skip the text in the middle.
For ex.,
The string "I have something important to tell you guys"
The result should be the string "I have something tell you guys" (first 3 words and last 3 words)
My code:
let myString = "I have something important to tell you guys";
let myArray = "I have something important to tell you guys";
myArray = myArray.split(' ')
let firstThreeWords = myArray[0] + " " + myArray[1] + " " + myArray[2];
let lastThreeWords = ... //not sure what to put here
console.log(firstThreeWords + lastThreeWords)
I got the first 3 words but how can I get the last 3 words if we don't know the exact number of words in a string?
let myString = 'I have something important to tell you guys';
let myArray = 'I have something important to tell you guys';
myArray = myArray.split(' ');
let firstThreeWords = myArray[0] + ' ' + myArray[1] + ' ' + myArray[2] + ' ';
let lastThreeWords =
myArray[myArray.length - 3] +
' ' +
myArray[myArray.length - 2] +
' ' +
myArray[myArray.length - 1];
console.log(firstThreeWords + lastThreeWords);
get you're last three words from the string by decreasing the length of the array.
You can try something like that
let myString = "I have something important to tell you guys";
let myArray = myString.split(' ');
const firstThree = myArray.slice(0, 3).join(' ');
const lastThree = myArray.slice(-3).join(' ');
const result = firstThree + " " + lastThree;
console.log(result);

How to freeze text from moving in console.log

So I have a simple console.log script that prints this
Now when I add a letter is moves
any way to freeze it please?
code
It would probably make more sense to make all of your cells contain a space character if they are "empty". Take a look here:
var Cell_1 = "a";
var Cell_2 = " ";
var Cell_3 = " ";
var Cell_4 = " ";
var Cell_5 = " ";
var Cell_6 = " ";
var Cell_7 = " ";
var Cell_8 = " ";
var Cell_9 = " ";
console.log(
Cell_1 + "|" + Cell_2 + "|" + Cell_3 + "\n" +
Cell_5 + "|" + Cell_6 + "|" + Cell_6 + "\n" +
Cell_7 + "|" + Cell_8 + "|" + Cell_9 + "\n" +
)
This way all of your variables are the same width - one character.
For future reference, here's some code that would probably look a bit nicer:
// This is called a 2d array: essentially an array containing other arrays.
// Its good for storing grids or tables of information.
var cells = [
['a', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
// This uses Array.reduce() to generate the string.
// Google it once you feel more confident :)
console.log(
cells.reduce(
(totalString, currentRow) => totalString + currentRow.join('|') + '\n',
''
)
)
The question isn't very clear but I am assuming that you want to keep a grid aligned, the grid having multiple cells that can contain a character or not.
The problem is that the empty cells are initialised to "" (empty string) which is of size 0, but when a character is set the size will be 1, so it will shift all the following cells of 1.
An easy solution is to use a " " (space) for the empty cell instead of a "". As a result the size of a cell will always be 1 and the whole grid won't be shifted.

DRY programming with JavaScript Mad Libs Challenge

I am new to programming.
I know that I could use functions and loops to keep from repeating this code, but I need help. Anyone?
var questions = 3;
var questionsCount = ' [' + questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsCount);
questions -= 1;
questionsCount = ' [' + questions + ' questions left]';
var verb = prompt('Please type a verb' + questionsCount);
questions -= 1;
questionsCount = ' [' + questions + ' questions left]';
var noun = prompt('Please type a noun' + questionsCount);
alert('All done. Ready for the message?');
var sentence = "There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.';
document.write(sentence);
I'd use a string template which contains, eg, {{noun}} to be replaced with a noun, which uses a regular expression to prompt the user for replacements to make:
const template = 'There once was a {{adjective}} programmer who wanted to use JavaScript to {{verb}} the {{noun}}.';
let questions = 3;
const result = template.replace(
/{{(.*?)}}/g,
(_, typeOfSpeechNeeded) => prompt(`Please type a ${typeOfSpeechNeeded}, ${questions--} question(s) left`)
);
console.log(result);
The regular expression
{{(.*?)}}
matches {{, followed by some characters, followed by }}, where those characters are put into a capturing group - this allows the .replace to examine the capturing group to determine what typeOfSpeechNeeded to display in the prompt.
The /g in the regular expression makes it global, which replaces all matches, not just the first match.
The backtick string is just a more readable way of interpolating strings:
prompt(`Please type a ${typeOfSpeechNeeded}, ${questions--} question(s) left`)
is equivalent to
prompt('Please type a ' + typeOfSpeechNeeded + ', ' + questions-- + ' question(s) left')

How to find hyphenated Strings

I search for specific words in a text and find them too. However, if the word I am looking for is divided into two lines by a hyphenation, the word will not be found. Here is a sample code.
searchString = "Hollywood";
newString = "";
text = "In India Hollywood is called Bollywood.";
var i = 0;
i = text.indexOf(searchString, i);
newString += text.substring(0, i) + " <<here begins my searchString>> " + text.substr(i, searchString.length) + " <<here ends my searchString>> " +
text.substring(i + searchString.length);
console.log(newString);
If the searchString Hollywood looks like
Holly-<br>wood
it will not be found.
How can I solve this problem in my Javascript code?
There are a few ways you could do it, but one of them would be to get rid of the - altogether if they're present:
searchString = "Hollywood";
newString = "";
text = "In India Holly-<br>wood is called Bollywood.";
filteredText = text.replace(/-<br>/,'');
var i = 0;
i = filteredText.indexOf(searchString, i);
newString += filteredText.substring(0, i) + " <<here begins my searchString>> " + filteredText.substr(i, searchString.length) + " <<here ends my searchString>> " +
filteredText.substring(i + searchString.length);
console.log(newString);
In this case, we just replace the -<br> characters with an empty string. It might not be the best approach, but refining it would depend on the context in which you intend to use it.
I hope that the regex and replace idea can help you customize a solution that best fit your needs.

telephone number format with jquery&regex done [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
telephone number format with jquery&regex
i need to verify and convert any input val into telephone number format, i.e
input er+f375g25123435s67 i need to convert into +375 25 1234567
a most suitable code is:
$('input').live({
keyup: function(){
ipt = $(this).val().replace(/[^\d]*/g, "");
// remove non-digits
ipt = "+" + ipt.substring(0, 3) + " " + ipt.substring(4, 6) + " " + ipt.substring(7, 14);
$(this).val(ipt);
}
});
but i can't enter numbers after +375
1) how to enable numbers after +375
2) how to convert ipt.substring(0, 3) + " " + ipt.substring(4, 6) + " " + ipt.substring(7, 14) into regular expression?
HERE'S AN ANSWER: http://jsfiddle.net/5UvJr/
You may possibly want to look at this: http://digitalbush.com/projects/masked-input-plugin/
The substring indexes are wrong, try this:
ipt = "+" + ipt.substring(0, 3) + " " + ipt.substring(3, 5) + " " + ipt.substring(5, 12);
here is an answer: http://jsfiddle.net/5UvJr/
$('input').live({
keyup: function(){
var phone = $(this).val().replace(/\D/g, '');
phone = phone.substring(0,12);
var myRegexp = /(\d{3})(\d{2})(\d*)/g
var mphone = myRegexp.exec(phone);
$(this).val('+' + mphone [1] + ' ' + mphone [2] + ' ' + mphone [3]);
}
});

Categories

Resources