JavaScript | 자바스크립트 | 'FOR' LOOPS IN JAVASCRIPT 둘
JavaScript | 자바스크립트 | 'FOR' LOOPS IN JAVASCRIPT 둘
Search Text for Your Name | Codecademy
Searching large blocks of text can be tedious, but with JavaScript, it's a breeze! In this project, we'll show you how to search long blocks of text for important information (such as your name).
1/7 What you'll be building
File1 _ script.js
/*jshint multistr:true */text = "Blah blah blah blah blah blah Eric \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";
var myName = "Eric";
var hits = [];
// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
if (text[i] === "E") {
// If we find it, add characters up to
// the length of my name to the array
for(var j = i; j < (myName.length + i); j++) {
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
| 처음 보는 코드군요.
2/7 Declare your variables
File1 _ script.js
/*jshint multistr:true */
text = "JONE save and submit \
code reset code JONE stuck? \
get a hint! JONE";
var myName = "JONE";
var hits = [];
3/7 Your first "for" loop
File1 _ script.js
/*jshint multistr:true */
text = "JONE save and submit \
code reset code JONE stuck? \
get a hint! JONE";
var myName = "JONE";
var hits = [];
for (var i=0; i<text.length; i++) {
}
4/7 Your "if" statement
File1 _ script.js
/*jshint multistr:true */
text = "JONE save and submit \
code reset code JONE stuck? \
get a hint! JONE";
var myName = "JONE";
var hits = [];
for (var i=0; i<text.length; i++) {
if (text[i] === "J") {
}
}
5/7 Your second "for" loop
File1 _ script.js
/*jshint multistr:true */
text = "JONE save and submit \
code reset code JONE stuck? \
get a hint! JONE";
var myName = "JONE";
var hits = [];
for (var i=0; i<text.length; i++) {
if (text[i] === "J") {
for (var j=i; j<(myName.length + i); j++) {
hits.push(text[j]);
}
}
}
| 두번째 for loop는 아직 이해하기가 어렵네요. 특히, j의 범위를 지정하는 부분에서 myName.length + i가 어떤 의미인지 잘 모르겠습니다. 아직은요. 일단은 Go.
6/7 Log it!
File1 _ script.js
/*jshint multistr:true */
text = "JONE save and submit \
code reset code JONE stuck? \
get a hint! JONE";
var myName = "JONE";
var hits = [];
for (var i=0; i<text.length; i++) {
if (text[i] === "J") {
for (var j=i; j<(myName.length + i); j++) {
hits.push(text[j]);
}
}
}
if (hits.length === 0) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
7/7 Victory!
File1 _ script.js
Next Course: Introduction to 'While' Loops in JS
