aboutsummaryrefslogtreecommitdiff
path: root/files/de/learn/javascript
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2021-07-15 13:33:44 -0400
committerGitHub <noreply@github.com>2021-07-15 13:33:44 -0400
commit3d809d2737f9b0fe0ef0f3d26c8c785d9d95cd09 (patch)
treee3b7a3de218ccbf82cc039ecc1ee72387f472fd0 /files/de/learn/javascript
parent037a4118c4324d39fdef8bd23f9dd21b02f50946 (diff)
downloadtranslated-content-3d809d2737f9b0fe0ef0f3d26c8c785d9d95cd09.tar.gz
translated-content-3d809d2737f9b0fe0ef0f3d26c8c785d9d95cd09.tar.bz2
translated-content-3d809d2737f9b0fe0ef0f3d26c8c785d9d95cd09.zip
delete pages that were never translated from en-US (de, part 2) (#1552)
Diffstat (limited to 'files/de/learn/javascript')
-rw-r--r--files/de/learn/javascript/first_steps/useful_string_methods/index.html656
1 files changed, 0 insertions, 656 deletions
diff --git a/files/de/learn/javascript/first_steps/useful_string_methods/index.html b/files/de/learn/javascript/first_steps/useful_string_methods/index.html
deleted file mode 100644
index e0df907ade..0000000000
--- a/files/de/learn/javascript/first_steps/useful_string_methods/index.html
+++ /dev/null
@@ -1,656 +0,0 @@
----
-title: Useful string methods
-slug: Learn/JavaScript/First_steps/Useful_string_methods
-translation_of: Learn/JavaScript/First_steps/Useful_string_methods
----
-<div>{{LearnSidebar}}</div>
-
-<div>{{PreviousMenuNext("Learn/JavaScript/First_steps/Strings", "Learn/JavaScript/First_steps/Arrays", "Learn/JavaScript/First_steps")}}</div>
-
-<p class="summary">Jetzt, da wir die Basics kennengelernt haben, gehen wir einen Schritt weiter und sehen uns hilfreiche Methoden an, die wir im Umgang mit Strings anwenden können. Dazu zählt zum Beispiel die Länge eines Textes, hinzufügen oder splitten von Strings, das Austauschen eines Buchstaben in einem Text-String und mehr...</p>
-
-<table class="learn-box standard-table">
- <tbody>
- <tr>
- <th scope="row">Voraussetzungen:</th>
- <td>Grundlegende Computerkenntnisse, ein grundlegendes Verständnis von HTML und CSS, ein Verständnis dafür, was JavaScript ist.</td>
- </tr>
- <tr>
- <th scope="row">Ziel:</th>
- <td>Zu verstehen, dass Zeichenketten Objekte sind, und zu lernen, wie man einige der grundlegenden Methoden, die auf diesen Objekten verfügbar sind, verwendet, um Zeichenketten zu manipulieren.</td>
- </tr>
- </tbody>
-</table>
-
-<h2 id="Zeichenketten_als_Objekte">Zeichenketten als Objekte</h2>
-
-<p id="Useful_string_methods">Die meisten Dinge in JavaScript sind Objekte. Wenn Sie einen String erstellen, zum Beispiel durch die Verwendung von</p>
-
-<pre class="brush: js">let string = 'This is my string';</pre>
-
-<p>wird Ihre Variable zu einer String-Objektinstanz und hat als Ergebnis eine große Anzahl von Eigenschaften und Methoden zur Verfügung. Sie können dies sehen, wenn Sie auf die {{jsxref("String")}} Objektseite gehen und die Liste auf der Seite nach unten scrollen!</p>
-
-<p><strong>Sooo, <u>bevor</u> Du jetzt Kopfschmerzen bekommst:</strong> Die meisten der Methoden must du jetzt am Anfang noch nicht wirklich kennen. Allerdings gibt es da ein paar, die Du am Anfang und später ziemlich oft nutzen wirst. Werfen wir also einen Blick darauf:</p>
-
-<p>Starten wir mit ein paar Beispielen in der <a href="/en-US/docs/Learn/Common_questions/What_are_browser_developer_tools">browser developer console</a>.</p>
-
-<h3 id="Länge_einer_Zeichenkette">Länge einer Zeichenkette</h3>
-
-<p>Das ist einfach. Nutze einfach {{jsxref("String.prototype.length", "length")}} . Probiere einfach mal folgenden Code:</p>
-
-<pre class="brush: js">let browserType = 'mozilla';
-browserType.length;</pre>
-
-<p>Das sollte Dir eine "7" zurückgeben, denn "mozilla" ist 7 Zeichen lang. Das kann man für verschiedene Dinge nutzen; Zum Beispiel: Du möchtest die Zeichenlänge einer Reihe von Namen herausfinden, um diese in der Reihenfolge ihrer Länge auszugeben. Oder lasse einen Nutzer wissen, das seine gerade getätigte Eingabe des Usernamens viel zu lang ist und nicht den Vorgaben entspricht.</p>
-
-<h3 id="Retrieving_a_specific_string_character">Retrieving a specific string character</h3>
-
-<p>On a related note, you can return any character inside a string by using <strong>square bracket notation</strong> — this means you include square brackets (<code>[]</code>) on the end of your variable name. Inside the square brackets you include the number of the character you want to return, so for example to retrieve the first letter you'd do this:</p>
-
-<pre class="brush: js">browserType[0];</pre>
-
-<p>Remember: computers count from 0, not 1! You could use this to, for example, find the first letter of a series of strings and order them alphabetically.</p>
-
-<p>To retrieve the last character of <em>any</em> string, we could use the following line, combining this technique with the <code>length</code> property we looked at above:</p>
-
-<pre class="brush: js">browserType[browserType.length-1];</pre>
-
-<p>The length of "mozilla" is 7, but because the count starts at 0, the character position is 6; using  <code>length-1</code> gets us the last character.</p>
-
-<h3 id="Finding_a_substring_inside_a_string_and_extracting_it">Finding a substring inside a string and extracting it</h3>
-
-<ol>
- <li>Sometimes you'll want to find if a smaller string is present inside a larger one (we generally say <em>if a substring is present inside a string</em>). This can be done using the {{jsxref("String.prototype.indexOf()", "indexOf()")}} method, which takes a single {{glossary("parameter")}} — the substring you want to search for. Try this:
-
- <pre class="brush: js">browserType.indexOf('zilla');</pre>
- This gives us a result of 2, because the substring "zilla" starts at position 2 (0, 1, 2  — so 3 characters in) inside "mozilla". Such code could be used to filter strings. For example, we may have a list of web addresses and only want to print out the ones that contain "mozilla".</li>
-</ol>
-
-<ol start="2">
- <li>This can be done in another way, which is possibly even more effective. Try the following:
- <pre class="brush: js">browserType.indexOf('vanilla');</pre>
- This should give you a result of <code>-1</code> — this is returned when the substring, in this case 'vanilla', is not found in the main string.<br>
- <br>
- You could use this to find all instances of strings that <strong>don't</strong> contain the substring 'mozilla', or <strong>do,</strong> if you use the negation operator, as shown below. You could do something like this:
-
- <pre class="brush: js">if(browserType.indexOf('mozilla') !== -1) {
- // do stuff with the string
-}</pre>
- </li>
- <li>When you know where a substring starts inside a string, and you know at which character you want it to end, {{jsxref("String.prototype.slice()", "slice()")}} can be used to extract it. Try the following:
- <pre class="brush: js">browserType.slice(0,3);</pre>
- This returns "moz" — the first parameter is the character position to start extracting at, and the second parameter is the character position after the last one to be extracted. So the slice happens from the first position, up to, but not including, the last position. In this example, since the starting index is 0, the second parameter is equal to the length of the string being returned.<br>
-  </li>
- <li>Also, if you know that you want to extract all of the remaining characters in a string after a certain character, you don't have to include the second parameter! Instead, you only need to include the character position from where you want to extract the remaining characters in a string. Try the following:
- <pre class="brush: js">browserType.slice(2);</pre>
- This returns "zilla" — this is because the character position of 2 is the letter z, and because you didn't include a second parameter, the substring that was returned was all of the remaining characters in the string. </li>
-</ol>
-
-<div class="note">
-<p><strong>Note</strong>: The second parameter of <code>slice()</code> is optional: if you don't include it, the slice ends at the end of the original string. There are other options too; study the {{jsxref("String.prototype.slice()", "slice()")}} page to see what else you can find out.</p>
-</div>
-
-<h3 id="Changing_case">Changing case</h3>
-
-<p>The string methods {{jsxref("String.prototype.toLowerCase()", "toLowerCase()")}} and {{jsxref("String.prototype.toUpperCase()", "toUpperCase()")}} take a string and convert all the characters to lower- or uppercase, respectively. This can be useful for example if you want to normalize all user-entered data before storing it in a database.</p>
-
-<p>Let's try entering the following lines to see what happens:</p>
-
-<pre class="brush: js">let radData = 'My NaMe Is MuD';
-radData.toLowerCase();
-radData.toUpperCase();</pre>
-
-<h3 id="Updating_parts_of_a_string">Updating parts of a string</h3>
-
-<p>You can replace one substring inside a string with another substring using the {{jsxref("String.prototype.replace()", "replace()")}} method. This works very simply at a basic level, although there are some advanced things you can do with it that we won't go into yet.</p>
-
-<p>It takes two parameters — the string you want to replace, and the string you want to replace it with. Try this example:</p>
-
-<pre class="brush: js">browserType.replace('moz','van');</pre>
-
-<p>This returns "vanilla" in the console. But if you check the value of <code>browserType</code>, it is still "mozilla'. To actually update the value of the <code>browserType</code> variable in a real program, you'd have to set the variable value to be the result of the operation; it doesn't just update the substring value automatically. So you'd have to actually write this: <code>browserType = browserType.replace('moz','van');</code></p>
-
-<h2 id="Active_learning_examples">Active learning examples</h2>
-
-<p>In this section we'll get you to try your hand at writing some string manipulation code. In each exercise below, we have an array of strings, and a loop that processes each value in the array and displays it in a bulleted list. You don't need to understand arrays or loops right now — these will be explained in future articles. All you need to do in each case is write the code that will output the strings in the format that we want them in.</p>
-
-<p>Each example comes with a "Reset" button, which you can use to reset the code if you make a mistake and can't get it working again, and a "Show solution" button you can press to see a potential answer if you get really stuck.</p>
-
-<h3 id="Filtering_greeting_messages">Filtering greeting messages</h3>
-
-<p>In the first exercise we'll start you off simple — we have an array of greeting card messages, but we want to sort them to list just the Christmas messages. We want you to fill in a conditional test inside the <code>if( ... )</code> structure, to test each string and only print it in the list if it is a Christmas message.</p>
-
-<ol>
- <li>First think about how you could test whether the message in each case is a Christmas message. What string is present in all of those messages, and what method could you use to test whether it is present?</li>
- <li>You'll then need to write a conditional test of the form <em>operand1 operator operand2</em>. Is the thing on the left equal to the thing on the right? Or in this case, does the method call on the left return the result on the right?</li>
- <li>Hint: In this case it is probably more useful to test whether the method call <em>isn't</em> equal to a certain result.</li>
-</ol>
-
-<div class="hidden">
-<h6 id="Playable_code">Playable code</h6>
-
-<pre class="brush: html">&lt;h2&gt;Live output&lt;/h2&gt;
-
-&lt;div class="output" style="min-height: 125px;"&gt;
-
-&lt;ul&gt;
-
-&lt;/ul&gt;
-
-&lt;/div&gt;
-
-&lt;h2&gt;Editable code&lt;/h2&gt;
-&lt;p class="a11y-label"&gt;Press Esc to move focus away from the code area (Tab inserts a tab character).&lt;/p&gt;
-
-&lt;textarea id="code" class="playable-code" style="height: 290px; width: 95%"&gt;
-const list = document.querySelector('.output ul');
-list.innerHTML = '';
-let greetings = ['Happy Birthday!',
- 'Merry Christmas my love',
- 'A happy Christmas to all the family',
- 'You\'re all I want for Christmas',
- 'Get well soon'];
-
-for (let i = 0; i &lt; greetings.length; i++) {
- let input = greetings[i];
- // Your conditional test needs to go inside the parentheses
- // in the line below, replacing what's currently there
- if (greetings[i]) {
- let listItem = document.createElement('li');
- listItem.textContent = input;
- list.appendChild(listItem);
- }
-}
-&lt;/textarea&gt;
-
-&lt;div class="playable-buttons"&gt;
- &lt;input id="reset" type="button" value="Reset"&gt;
- &lt;input id="solution" type="button" value="Show solution"&gt;
-&lt;/div&gt;
-</pre>
-
-<pre class="brush: css">html {
- font-family: sans-serif;
-}
-
-h2 {
- font-size: 16px;
-}
-
-.a11y-label {
- margin: 0;
- text-align: right;
- font-size: 0.7rem;
- width: 98%;
-}
-
-body {
- margin: 10px;
- background: #f5f9fa;
-}</pre>
-
-<pre class="brush: js">const textarea = document.getElementById('code');
-const reset = document.getElementById('reset');
-const solution = document.getElementById('solution');
-let code = textarea.value;
-let userEntry = textarea.value;
-
-function updateCode() {
- eval(textarea.value);
-}
-
-reset.addEventListener('click', function() {
- textarea.value = code;
- userEntry = textarea.value;
- solutionEntry = jsSolution;
- solution.value = 'Show solution';
- updateCode();
-});
-
-solution.addEventListener('click', function() {
- if(solution.value === 'Show solution') {
- textarea.value = solutionEntry;
- solution.value = 'Hide solution';
- } else {
- textarea.value = userEntry;
- solution.value = 'Show solution';
- }
- updateCode();
-});
-
-const jsSolution = 'const list = document.querySelector(\'.output ul\');' +
-'\nlist.innerHTML = \'\';' +
-'\nlet greetings = [\'Happy Birthday!\',' +
-'\n                 \'Merry Christmas my love\',' +
-'\n                 \'A happy Christmas to all the family\',' +
-'\n                 \'You\\\'re all I want for Christmas\',' +
-'\n                 \'Get well soon\'];' +
-'\n' +
-'\nfor (let i = 0; i &lt; greetings.length; i++) {' +
-'\n  let input = greetings[i];' +
-'\n  if (greetings[i].indexOf(\'Christmas\') !== -1) {' +
-'\n    let result = input;' +
-'\n    let listItem = document.createElement(\'li\');' +
-'\n    listItem.textContent = result;' +
-'\n    list.appendChild(listItem);' +
-'\n  }' +
-'\n}';
-
-let solutionEntry = jsSolution;
-
-textarea.addEventListener('input', updateCode);
-window.addEventListener('load', updateCode);
-
-// stop tab key tabbing out of textarea and
-// make it write a tab at the caret position instead
-
-textarea.onkeydown = function(e){
- if (e.keyCode === 9) {
- e.preventDefault();
- insertAtCaret('\t');
- }
-
- if (e.keyCode === 27) {
- textarea.blur();
- }
-};
-
-function insertAtCaret(text) {
- const scrollPos = textarea.scrollTop;
- const caretPos = textarea.selectionStart;
- const front = (textarea.value).substring(0, caretPos);
- const back = (textarea.value).substring(textarea.selectionEnd, textarea.value.length);
-
- textarea.value = front + text + back;
- caretPos = caretPos + text.length;
- textarea.selectionStart = caretPos;
- textarea.selectionEnd = caretPos;
- textarea.focus();
- textarea.scrollTop = scrollPos;
-}
-
-// Update the saved userCode every time the user updates the text area code
-
-textarea.onkeyup = function(){
- // We only want to save the state when the user code is being shown,
- // not the solution, so that solution is not saved over the user code
- if(solution.value === 'Show solution') {
- userEntry = textarea.value;
- } else {
- solutionEntry = textarea.value;
- }
-
- updateCode();
-};</pre>
-</div>
-
-<p>{{ EmbedLiveSample('Playable_code', '100%', 590, "", "", "hide-codepen-jsfiddle") }}</p>
-
-<h3 id="Fixing_capitalization">Fixing capitalization</h3>
-
-<p>In this exercise we have the names of cities in the United Kingdom, but the capitalization is all messed up. We want you to change them so that they are all lower case, except for a capital first letter. A good way to do this is to:</p>
-
-<ol>
- <li>Convert the whole of the string contained in the <code>input</code> variable to lower case and store it in a new variable.</li>
- <li>Grab the first letter of the string in this new variable and store it in another variable.</li>
- <li>Using this latest variable as a substring, replace the first letter of the lowercase string with the first letter of the lowercase string changed to upper case. Store the result of this replace procedure in another new variable.</li>
- <li>Change the value of the <code>result</code> variable to equal to the final result, not the <code>input</code>.</li>
-</ol>
-
-<div class="note">
-<p><strong>Note</strong>: A hint — the parameters of the string methods don't have to be string literals; they can also be variables, or even variables with a method being invoked on them.</p>
-</div>
-
-<div class="hidden">
-<h6 id="Playable_code_2">Playable code 2</h6>
-
-<pre class="brush: html">&lt;h2&gt;Live output&lt;/h2&gt;
-
-&lt;div class="output" style="min-height: 125px;"&gt;
-
-&lt;ul&gt;
-
-&lt;/ul&gt;
-
-&lt;/div&gt;
-
-&lt;h2&gt;Editable code&lt;/h2&gt;
-&lt;p class="a11y-label"&gt;Press Esc to move focus away from the code area (Tab inserts a tab character).&lt;/p&gt;
-
-&lt;textarea id="code" class="playable-code" style="height: 250px; width: 95%"&gt;
-const list = document.querySelector('.output ul');
-list.innerHTML = '';
-let cities = ['lonDon', 'ManCHESTer', 'BiRmiNGHAM', 'liVERpoOL'];
-
-for (let i = 0; i &lt; cities.length; i++) {
- let input = cities[i];
- // write your code just below here
-
- let result = input;
- let listItem = document.createElement('li');
- listItem.textContent = result;
- list.appendChild(listItem);
-}
-&lt;/textarea&gt;
-
-&lt;div class="playable-buttons"&gt;
- &lt;input id="reset" type="button" value="Reset"&gt;
- &lt;input id="solution" type="button" value="Show solution"&gt;
-&lt;/div&gt;
-</pre>
-
-<pre class="brush: css">html {
- font-family: sans-serif;
-}
-
-h2 {
- font-size: 16px;
-}
-
-.a11y-label {
- margin: 0;
- text-align: right;
- font-size: 0.7rem;
- width: 98%;
-}
-
-body {
- margin: 10px;
- background: #f5f9fa;
-}</pre>
-
-<pre class="brush: js">const textarea = document.getElementById('code');
-const reset = document.getElementById('reset');
-const solution = document.getElementById('solution');
-let code = textarea.value;
-let userEntry = textarea.value;
-
-function updateCode() {
- eval(textarea.value);
-}
-
-reset.addEventListener('click', function() {
- textarea.value = code;
- userEntry = textarea.value;
- solutionEntry = jsSolution;
- solution.value = 'Show solution';
- updateCode();
-});
-
-solution.addEventListener('click', function() {
- if(solution.value === 'Show solution') {
- textarea.value = solutionEntry;
- solution.value = 'Hide solution';
- } else {
- textarea.value = userEntry;
- solution.value = 'Show solution';
- }
- updateCode();
-});
-
-const jsSolution = 'const list = document.querySelector(\'.output ul\');' +
-'\nlist.innerHTML = \'\';' +
-'\nlet cities = [\'lonDon\', \'ManCHESTer\', \'BiRmiNGHAM\', \'liVERpoOL\'];' +
-'\n' +
-'\nfor (let i = 0; i &lt; cities.length; i++) {' +
-'\n  let input = cities[i];' +
-'\n  let lower = input.toLowerCase();' +
-'\n  let firstLetter = lower.slice(0,1);' +
-'\n  let capitalized = lower.replace(firstLetter,firstLetter.toUpperCase());' +
-'\n  let result = capitalized;' +
-'\n  let listItem = document.createElement(\'li\');' +
-'\n  listItem.textContent = result;' +
-'\n  list.appendChild(listItem);' +
-'\n' +
-'\n}';
-
-let solutionEntry = jsSolution;
-
-textarea.addEventListener('input', updateCode);
-window.addEventListener('load', updateCode);
-
-// stop tab key tabbing out of textarea and
-// make it write a tab at the caret position instead
-
-textarea.onkeydown = function(e){
- if (e.keyCode === 9) {
- e.preventDefault();
- insertAtCaret('\t');
- }
-
- if (e.keyCode === 27) {
- textarea.blur();
- }
-};
-
-function insertAtCaret(text) {
- const scrollPos = textarea.scrollTop;
- const caretPos = textarea.selectionStart;
- const front = (textarea.value).substring(0, caretPos);
- const back = (textarea.value).substring(textarea.selectionEnd, textarea.value.length);
-
- textarea.value = front + text + back;
- caretPos = caretPos + text.length;
- textarea.selectionStart = caretPos;
- textarea.selectionEnd = caretPos;
- textarea.focus();
- textarea.scrollTop = scrollPos;
-}
-
-// Update the saved userCode every time the user updates the text area code
-
-textarea.onkeyup = function(){
- // We only want to save the state when the user code is being shown,
- // not the solution, so that solution is not saved over the user code
- if(solution.value === 'Show solution') {
- userEntry = textarea.value;
- } else {
- solutionEntry = textarea.value;
- }
-
- updateCode();
-};</pre>
-</div>
-
-<p>{{ EmbedLiveSample('Playable_code_2', '100%', 550, "", "", "hide-codepen-jsfiddle") }}</p>
-
-<h3 id="Making_new_strings_from_old_parts">Making new strings from old parts</h3>
-
-<p>In this last exercise, the array contains a bunch of strings containing information about train stations in the North of England. The strings are data items that contain the three-letter station code, followed by some machine-readable data, followed by a semicolon, followed by the human-readable station name. For example:</p>
-
-<pre>MAN675847583748sjt567654;Manchester Piccadilly</pre>
-
-<p>We want to extract the station code and name, and put them together in a string with the following structure:</p>
-
-<pre>MAN: Manchester Piccadilly</pre>
-
-<p>We'd recommend doing it like this:</p>
-
-<ol>
- <li>Extract the three-letter station code and store it in a new variable.</li>
- <li>Find the character index number of the semicolon.</li>
- <li>Extract the human-readable station name using the semicolon character index number as a reference point, and store it in a new variable.</li>
- <li>Concatenate the two new variables and a string literal to make the final string.</li>
- <li>Change the value of the <code>result</code> variable to equal to the final string, not the <code>input</code>.</li>
-</ol>
-
-<div class="hidden">
-<h6 id="Playable_code_3">Playable code 3</h6>
-
-<pre class="brush: html">&lt;h2&gt;Live output&lt;/h2&gt;
-
-&lt;div class="output" style="min-height: 125px;"&gt;
-
-&lt;ul&gt;
-
-&lt;/ul&gt;
-
-&lt;/div&gt;
-
-&lt;h2&gt;Editable code&lt;/h2&gt;
-&lt;p class="a11y-label"&gt;Press Esc to move focus away from the code area (Tab inserts a tab character).&lt;/p&gt;
-
-&lt;textarea id="code" class="playable-code" style="height: 285px; width: 95%"&gt;
-const list = document.querySelector('.output ul');
-list.innerHTML = '';
-let stations = ['MAN675847583748sjt567654;Manchester Piccadilly',
- 'GNF576746573fhdg4737dh4;Greenfield',
- 'LIV5hg65hd737456236dch46dg4;Liverpool Lime Street',
- 'SYB4f65hf75f736463;Stalybridge',
- 'HUD5767ghtyfyr4536dh45dg45dg3;Huddersfield'];
-
-for (let i = 0; i &lt; stations.length; i++) {
- let input = stations[i];
- // write your code just below here
-
- let result = input;
- let listItem = document.createElement('li');
- listItem.textContent = result;
- list.appendChild(listItem);
-}
-&lt;/textarea&gt;
-
-&lt;div class="playable-buttons"&gt;
- &lt;input id="reset" type="button" value="Reset"&gt;
- &lt;input id="solution" type="button" value="Show solution"&gt;
-&lt;/div&gt;
-</pre>
-
-<pre class="brush: css">html {
- font-family: sans-serif;
-}
-
-h2 {
- font-size: 16px;
-}
-
-.a11y-label {
- margin: 0;
- text-align: right;
- font-size: 0.7rem;
- width: 98%;
-}
-
-body {
- margin: 10px;
- background: #f5f9fa;
-}
-</pre>
-
-<pre class="brush: js">const textarea = document.getElementById('code');
-const reset = document.getElementById('reset');
-const solution = document.getElementById('solution');
-let code = textarea.value;
-let userEntry = textarea.value;
-
-function updateCode() {
- eval(textarea.value);
-}
-
-reset.addEventListener('click', function() {
- textarea.value = code;
- userEntry = textarea.value;
- solutionEntry = jsSolution;
- solution.value = 'Show solution';
- updateCode();
-});
-
-solution.addEventListener('click', function() {
- if(solution.value === 'Show solution') {
- textarea.value = solutionEntry;
- solution.value = 'Hide solution';
- } else {
- textarea.value = userEntry;
- solution.value = 'Show solution';
- }
- updateCode();
-});
-
-const jsSolution = 'const list = document.querySelector(\'.output ul\');' +
-'\nlist.innerHTML = \'\';' +
-'\nlet stations = [\'MAN675847583748sjt567654;Manchester Piccadilly\',' +
-'\n                \'GNF576746573fhdg4737dh4;Greenfield\',' +
-'\n                \'LIV5hg65hd737456236dch46dg4;Liverpool Lime Street\',' +
-'\n                \'SYB4f65hf75f736463;Stalybridge\',' +
-'\n                \'HUD5767ghtyfyr4536dh45dg45dg3;Huddersfield\'];' +
-'\n' +
-'\nfor (let i = 0; i &lt; stations.length; i++) {' +
-'\n let input = stations[i];' +
-'\n let code = input.slice(0,3);' +
-'\n let semiC = input.indexOf(\';\');' +
-'\n let name = input.slice(semiC + 1);' +
-'\n let result = code + \': \' + name;' +
-'\n let listItem = document.createElement(\'li\');' +
-'\n listItem.textContent = result;' +
-'\n list.appendChild(listItem);' +
-'\n}';
-
-let solutionEntry = jsSolution;
-
-textarea.addEventListener('input', updateCode);
-window.addEventListener('load', updateCode);
-
-// stop tab key tabbing out of textarea and
-// make it write a tab at the caret position instead
-
-textarea.onkeydown = function(e){
- if (e.keyCode === 9) {
- e.preventDefault();
- insertAtCaret('\t');
- }
-
- if (e.keyCode === 27) {
- textarea.blur();
- }
-};
-
-function insertAtCaret(text) {
- const scrollPos = textarea.scrollTop;
- const caretPos = textarea.selectionStart;
- const front = (textarea.value).substring(0, caretPos);
- const back = (textarea.value).substring(textarea.selectionEnd, textarea.value.length);
-
- textarea.value = front + text + back;
- caretPos = caretPos + text.length;
- textarea.selectionStart = caretPos;
- textarea.selectionEnd = caretPos;
- textarea.focus();
- textarea.scrollTop = scrollPos;
-}
-
-// Update the saved userCode every time the user updates the text area code
-
-textarea.onkeyup = function(){
- // We only want to save the state when the user code is being shown,
- // not the solution, so that solution is not saved over the user code
- if(solution.value === 'Show solution') {
- userEntry = textarea.value;
- } else {
- solutionEntry = textarea.value;
- }
-
- updateCode();
-};</pre>
-</div>
-
-<p>{{ EmbedLiveSample('Playable_code_3', '100%', 585, "", "", "hide-codepen-jsfiddle") }}</p>
-
-<h2 id="Conclusion">Conclusion</h2>
-
-<p>You can't escape the fact that being able to handle words and sentences in programming is very important — particularly in JavaScript, as websites are all about communicating with people. This article has given you the basics that you need to know about manipulating strings for now. This should serve you well as you go into more complex topics in the future. Next, we're going to look at the last major type of data we need to focus on in the short term — arrays.</p>
-
-<p>{{PreviousMenuNext("Learn/JavaScript/First_steps/Strings", "Learn/JavaScript/First_steps/Arrays", "Learn/JavaScript/First_steps")}}</p>
-
-<h2 id="In_this_module">In this module</h2>
-
-<ul>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript">What is JavaScript?</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/A_first_splash">A first splash into JavaScript</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong">What went wrong? Troubleshooting JavaScript</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/Variables">Storing the information you need — Variables</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/Math">Basic math in JavaScript — numbers and operators</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/Strings">Handling text — strings in JavaScript</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods">Useful string methods</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/Arrays">Arrays</a></li>
- <li><a href="/en-US/docs/Learn/JavaScript/First_steps/Silly_story_generator">Assessment: Silly story generator</a></li>
-</ul>