Skip to content

Instantly share code, notes, and snippets.

@fushang318
Last active December 19, 2015 05:49
Show Gist options
  • Save fushang318/5907301 to your computer and use it in GitHub Desktop.
Save fushang318/5907301 to your computer and use it in GitHub Desktop.
3 'For' Loops in JavaScript

来源

http://www.codecademy.com/zh/courses/javascript-beginner-en-NhsaT/0/1?curriculum_id=506324b3a7dffd00020bf661

章节名

第一章 认识 for 循环

课件名

第一课 for 循环基础 

json

// course_ware.export_json 可以获取到

{"title":"for \u5faa\u73af\u57fa\u7840","desc":"","kind":"javascript","total_count":5,"steps":[{"content":"1 \u7528\u4e94\u4e2a console.log \u6253\u5370 1 - 5 \u8fd9\u4e9b\u6570\u5b57\r\n2 \u4e0d\u8981\u62b1\u6028\u8fd9\u4e2a\u7e41\u7410\u7684\u7ec3\u4e60\r\n3 \u4e0b\u4e2a\u7ec3\u4e60\u6211\u4eec\u5c1d\u8bd5\u7528 for \u5faa\u73af\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898","rule":"\r\nif (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nif (code.match(/console\\.log\\s*\\(/g) == undefined || code.match(/console\\.log\\s*\\(/g).length < 5) {\r\n  return \"Did you remember to use 5 separate console.log() statements?\";\r\n}\r\n\r\nfor (var i = 1; i < 6; i++) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"It looks like you didn't log \" + i + \" to the console.\";\r\n  }\r\n}\r\n \r\nreturn true;","title":"\u4e3a\u4ec0\u4e48\u4f7f\u7528 for \u5faa\u73af","desc":"\u6211\u4eec\u5b66\u4e60\u7f16\u7a0b\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u628a\u7e41\u7410\u91cd\u590d\u7684\u5de5\u4f5c\u4ea4\u7ed9\u7535\u8111\u53bb\u505a\r\n\r\n\u8fd9\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u6b63\u597d\u53ef\u4ee5\u6f14\u793a\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u5b66\u4e60 for \u5faa\u73af","hint":"\u786e\u8ba4\u7528\u4ece 1 \u5230 5 \u8fd9\u4e9b\u6570\u5b57\u8c03\u7528\u4e86 console.log\r\n\r\nconsole.log() \u9700\u8981\u8fd9\u6837\u7528\uff1a\r\nconsole.log(/* \u60f3\u8981\u6253\u5370\u7684\u5185\u5bb9 */);","init_code":"// \u5199 5 \u4e2a console.log","code_reset":true},{"content":"1 \u521d\u59cb\u5316\u7684 for \u5faa\u73af\u4ee3\u7801\u4f1a\u6253\u5370 1-5\r\n2 \u628a 6 \u4fee\u6539\u6210 11\uff0c\u5e76\u70b9\u51fb \u201c\u63d0\u4ea4\u6211\u7684\u4ee3\u7801\u201d,\u4f1a\u6253\u5370\u51fa 1-10","rule":"if (error)\r\n  return \"Your code doesn't look quite right! Make sure you're just replacing the 6 on line 3 with an 11.\";\r\n\r\nfor (var i = 1; i < 11; i++) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"It looks like you didn't log \" + i + \" to the console!\";\r\n  }\r\n}\r\n\r\nif (MT.printed(\"11\")) {\r\n  return \"It looks like you printed 11! Make sure you replaced the 6 on line 3 with an 11, and not some higher number.\";\r\n}\r\n\r\n  \r\nreturn true;","title":"\u7b2c\u4e00\u4e2a loop","desc":"\u76f8\u6bd4\u8f83\u4e8e\u624b\u52a8\u8c03\u7528\u4e94\u6b21 console.log\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 for \u5faa\u73af\u6765\u8fbe\u5230\u540c\u6837\u7684\u76ee\u7684\r\n\u8fd9\u4e2a\u7ec3\u4e60\u4ec5\u4ec5\u6f14\u793a\u5982\u4f55\u4f7f\u7528 for \u5faa\u73af\r\n\r\n\u6211\u4eec\u5148\u5c1d\u8bd5\u4f7f\u7528\u7b80\u5355\u7684 for \u5faa\u73af\uff0c\u5230\u7b2c\u4e09\u4e2a\u7ec3\u4e60\uff0c\u6211\u4eec\u4f1a\u5c1d\u8bd5\u4e00\u4e0b\u66f4\u6709\u8da3\u7684\u4e1c\u897f","hint":"\u6211\u4eec\u8981\u6253\u5370 1-10\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4ee3\u7801\u4e2d\u662f 11 \u5462\uff1f\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u60f3\u8981\u8ba9\u5faa\u73af\u8fd0\u884c\u5230\u8ba1\u6570\u5668\u5c0f\u4e8e11\uff0c\u4e00\u65e6\u7b49\u4e8e11\uff0c\u5faa\u73af\u5c31\u505c\u6b62","init_code":"// loop \u793a\u4f8b\r\n\r\nfor (var counter = 1; counter < 6; counter++) {\r\n  console.log(counter);\r\n}","code_reset":true},{"content":"1 \u8fd9\u4e2a\u5faa\u73af\u4ece1\u5f00\u59cb\uff0c\u523010\u7ed3\u675f\r\n2 \u628a\u5b83\u6539\u4e3a\u4ece5\u5f00\u59cb","rule":"\r\nif (error)\r\n  return \"Your code doesn't look quite right! Check the Hint if you need help.\";\r\n\r\nfor (var i = 1; i < 5; i++) {\r\n  if (MT.printed(i.toString())) {\r\n    return \"It looks like your loop printed \" + i + \", but it shouldn't have!\";\r\n  }\r\n}\r\n\r\nfor (var i = 5; i < 11; i++) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"It looks like your loop didn't log \" + i + \" to the console.\";\r\n  }\r\n}\r\n\r\nif (MT.printed(\"11\")) {\r\n  return \"It looks like you printed 11! Make sure not to change any part of the loop except the var i = 1 bit.\";\r\n}\r\n\r\n\r\n\r\nreturn true;","title":"for \u5faa\u73af\u7684\u5f00\u59cb\u503c","desc":"","hint":"","init_code":"for (var i = 1; i < 11; i = i + 1){\r\n  console.log(i);\r\n}","code_reset":true},{"content":"1 \u6211\u4eec\u770b\u5230\u8fd9\u4e2a\u5faa\u73af\u662f\u4ece 1 \u5230 10\r\n2 \u628a\u5b83\u6539\u4e3a\u4ece 4 \u5f00\u59cb\uff08\u5305\u62ec4\uff09\r\n3 \u628a\u5b83\u6539\u4e3a\u5230 23 \u7ed3\u675f(\u5305\u62ec23\uff0c\u4e0d\u5305\u62ec24)","rule":"if (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nfor (var i = 0; i < 4; i++) {\r\n  if (MT.printed(i.toString())) {\r\n    return \"Your loop logged \" + i + \" to the console, but shouldn't have!\";\r\n  }\r\n}\r\n\r\nfor (var i = 4; i < 24; i++) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"Your loop should have logged \" + i + \" to the console, but didn't!\";\r\n  }\r\n}\r\n\r\nif (MT.printed(\"24\")) {\r\n  return \"Whoops--it looks like you logged 24 to the console! Your loop should print up to (and including) 23.\";\r\n}\r\n\r\n\r\n\r\n\r\nreturn true;\r\n\r\n","title":"for \u5faa\u73af\u7684\u7ed3\u675f\u503c","desc":"","hint":"","init_code":"for (var i = 1; i < 11; i = i + 1) {\r\n  console.log(i);\r\n}","code_reset":true},{"content":"1 \u8fd9\u4e2a\u5faa\u73af\u4ece0\u5f00\u59cb\uff0c\u523035\u7ed3\u675f\r\n2 \u628a\u5b83\u6539\u4e3a\u4ece5\u5f00\u59cb\r\n3 \u628a\u5b83\u6539\u4e3a\u6253\u5370\u51fa50\u65f6\u5c31\u505c\u6b62\r\n4 \u9700\u8981\u6539\u4e3a\u6bcf5\u4e2a\u6570\u5b57\u6253\u5370\u4e00\u6b21\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u628a i \u589e\u91cf\u6539\u4e3a 5","rule":"//arr = ['5','10','15','20','25','30','35','40','45','50']\r\n//return MT.array_equals(arr, MT.prints)\r\n\r\nif (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nfor (var i = 0; i < 5; i++) {\r\n  if (MT.printed(i.toString())) {\r\n    return \"Your for loop logged \" + i + \" to the console, but shouldn't have!\";\r\n  }\r\n}\r\n\r\nfor (var i = 5; i < 51; i += 5) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"It looks like your loop didn't log \" + i + \" to the console.\";\r\n  }\r\n}\r\n\r\nif (MT.prints.length > 10) {\r\n  return \"Did you remember to only log every fifth number?\";\r\n}\r\n\r\nreturn true;\r\n","title":"\u63a7\u5236 for \u5faa\u73af","desc":"","hint":"","init_code":"for (var i = 0; i < 36; i++) {\r\n  console.log(i);\r\n}","code_reset":true}]}

来源

http://www.codecademy.com/zh/courses/javascript-beginner-en-NhsaT/1/1?curriculum_id=506324b3a7dffd00020bf661

章节名

第一章 认识 for 循环

课件名

第二课 深入学习 for 循环 

json

// course_ware.export_json 可以获取到

{"title":"\u6df1\u5165\u5b66\u4e60 for \u5faa\u73af","desc":"","kind":"javascript","total_count":3,"steps":[{"content":"1 \u628a\u5faa\u73af\u6539\u4e3a\u4ece8\u5f00\u59cb\r\n2 \u628a\u5faa\u73af\u6539\u4e3a i < 120 \u65f6\uff0c\u5c31\u4e00\u76f4\u5faa\u73af\r\n3 \u628a\u589e\u91cf\u6539\u4e3a 12","rule":"if (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nfor (var i = 0; i < 8; i++) {\r\n  if (MT.printed(i.toString())) {\r\n    return \"Your loop logged \" + i + \" to the console, but shouldn't have!\";\r\n  }\r\n}\r\n\r\nfor (var i = 8; i < 120; i += 12) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"It looks like your loop didn't log \" + i + \" to the console.\";\r\n  }\r\n}\r\n\r\nif (MT.prints.length > 10) {\r\n  return \"Did you remember to count up by 12s?\";\r\n}\r\n\r\n\r\n\r\nreturn true;","title":"\u5de5\u4f5c\u673a\u5236","desc":"","hint":"","init_code":"for (var i = 1; i < 10; i++) {\r\n  console.log(i);\r\n}","code_reset":true},{"content":"1 \u4fee\u6539\u4ee3\u7801\uff0c\u4f7f\u5176\u4ece\u8ba1\u6570\u566810\u5f00\u59cb\r\n2 \u4fee\u6539\u4ee3\u7801\uff0c\u4f7f\u5176\u8ba1\u6570\u5668\u52300\u7ed3\u675f\r\n3 \u4fee\u6539\u4ee3\u7801\uff0c\u4f7f\u5176\u6b65\u957f\u662f-1","rule":"\r\nif (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nfor (var i = 10; i >= 0; i--) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"It looks like your loop didn't log \" + i + \" to the console.\";\r\n  }\r\n}\r\n\r\n\r\nif(MT.prints[2] !== '8')\r\n  return \"It looks like you aren't counting down.\";\r\n\r\nreturn true;","title":"\u8ba1\u6570\u5668\u51cf\u5c11","desc":"","hint":"","init_code":"for (var i = 1; i >= 1; i++) {\r\n  console.log(i);\r\n}","code_reset":true},{"content":"\u81ea\u5df1\u5199\u4e00\u4e2a for \u5faa\u73af\uff0c\u5faa\u73af\u4ece100\u5f00\u59cb\u52300\u7ed3\u675f\uff08\u4e0d\u5305\u62ec0\uff09\uff0c\u6b65\u957f\u662f-5","rule":"\r\nif (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nfor (var i = 100; i > 0; i -= 5) {\r\n  if (!MT.printed(i.toString())) {\r\n    return \"Your loop didn't log \" + i + \" to the console!\";\r\n  }\r\n}\r\n\r\nif (MT.printed(\"0\")) {\r\n  return \"Your code shouldn't print out 0!\";\r\n}\r\n\r\n\r\n\r\nif(MT.prints[2] !== '90')\r\n  return \"Did you remember to count down from 100 by 5s?\";\r\n\r\nif(MT.prints.length > 20) {\r\n  return \"Did you remember to count down by 5s?\";\r\n}\r\n\r\nreturn true;\r\n","title":"","desc":"","hint":"","init_code":"// \u5199\u51fa\u5c5e\u4e8e\u4f60\u81ea\u5df1\u7684 for \u5faa\u73af","code_reset":true}]}

来源

http://www.codecademy.com/zh/courses/javascript-beginner-en-NhsaT/2/1?curriculum_id=506324b3a7dffd00020bf661

章节名

第一章 认识 for 循环

课件名

第三课 数组和循环

json

// course_ware.export_json 可以获取到

{"title":"\u6570\u7ec4\u548c\u5faa\u73af","desc":"","kind":"javascript","total_count":6,"steps":[{"content":"1 \u5b9a\u4e49\u4e00\u4e2a\u53eb\u505a junk \u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u6709\u56db\u4e2a\u5143\u7d20\uff08\u524d\u4e24\u4e2a\u662f\u5b57\u7b26\u4e32\uff0c\u540e\u4e24\u4e2a\u662f\u6570\u5b57\uff09\r\n2 \u7528 console.log \u6253\u5370 junk","rule":"try {\r\n  junk;\r\n} catch(ReferenceError) {\r\n  return \"\u4f60\u6ca1\u6709\u5b9a\u4e49 junk \u53d8\u91cf\";\r\n}\r\n\r\nif (error)\r\n  return \"\u4f60\u7684\u4ee3\u7801\u6709\u9519\u8bef\";\r\n\r\nif (!(junk instanceof Array))\r\n  return \"\u4f60\u6ca1\u6709\u628a junk \u5b9a\u4e49\u6210\u6570\u7ec4\";\r\n\r\nif (junk.length !== 4)\r\n  return \"junk \u9700\u8981\u6709\u56db\u4e2a\u5143\u7d20\uff08\u524d\u4e24\u4e2a\u662f\u5b57\u7b26\u4e32\uff0c\u540e\u4e24\u4e2a\u662f\u6570\u5b57\uff09\";\r\n\r\nif (typeof(junk[0]) !== 'string')\r\n  return \"junk \u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e0d\u662f\u5b57\u7b26\u4e32\";\r\n\r\nif (typeof(junk[1]) !== 'string')\r\n  return \"junk \u7684\u7b2c\u4e8c\u4e2a\u5143\u7d20\u4e0d\u662f\u5b57\u7b26\u4e32\";\r\n\r\nif (typeof(junk[2]) !== 'number')\r\n  return \"junk \u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u4e0d\u662f\u6570\u5b57\";\r\n\r\nif (typeof(junk[3]) !== 'number')\r\n  return \"junk \u7684\u7b2c\u56db\u4e2a\u5143\u7d20\u4e0d\u662f\u6570\u5b57\";\r\n\r\nif (!MT.printed(JSON.stringify(junk))){\r\n  return \"\u9700\u8981\u7528 console.log \u6253\u5370 junk\"\r\n}\r\n\r\nreturn true;","title":"\u8ba4\u8bc6\u6570\u7ec4","desc":"","hint":"","init_code":"// \u5b9a\u4e49 junk \u53d8\u91cf\r\n// \u7528 console.log \u6253\u5370","code_reset":true},{"content":"\u6253\u5370\u51fa\u6570\u7ec4\u7684\u7b2c\u56db\u4e2a\u5143\u7d20\r\n\r\n1 \u5c1d\u8bd5\u5982\u679c\u8868\u8fbe\u6570\u7ec4\u7684\u7b2c\u56db\u4e2a\u5143\u7d20\r\n2 \u7528 console.log() \u6253\u5370\u51fa\u6765","rule":"if (error)\r\n  return \"\u4f60\u7684\u4ee3\u7801\u6709\u9519\u8bef\";\r\n\r\nif (MT.printed(\"undefined\"))\r\n  return \"\u6570\u7ec4\u7684\u89d2\u6807\u4ece0\u5f00\u59cb,\u6240\u4ee5 junkData[4] \u53d6\u5230\u7684\u4e0d\u662f\u7b2c\u56db\u4e2a\u5143\u7d20\";\r\n\r\nif (!MT.printed(junkData[3] + \"\"))\r\n  return \"\u4f60\u9700\u8981\u6253\u5370 \" + junkData[3] + \", \u5b83\u662f\u7b2c\u56db\u4e2a\u5143\u7d20\";\r\n\r\nif (code.match(/junkData\\s*\\[/) === null)\r\n  return \"\u8bf7\u786e\u5b9a\u4f60\u4f7f\u7528\u4e86 junkData[]\";\r\n\r\nreturn true;","title":"\u6570\u7ec4\u7684\u89d2\u6807","desc":"","hint":"","init_code":"var junkData = [\"Eddie Murphy\", 49, \"peanuts\", 31];","code_reset":true},{"content":"1 Change the elements in the cities array. You can put in as many elements as you like.\r\n2 Run the for loop and see them all printed out!\r\n","rule":"if (error)\r\n  return \"\u4f60\u7684\u4ee3\u7801\u6709\u9519\u8bef\";\r\n\r\nif (cities.length === 0)\r\n  return \"\u786e\u5b9a cities \u4e0d\u662f\u7a7a\";\r\n\r\nif (MT.prints.length === 0){\r\n  return \"\u6ca1\u6709\u6253\u5370 cities \u4e2d\u7684\u5185\u5bb9\";\r\n}\r\n\r\nvar original_cities = [\"Melbourne\", \"Amman\", \"Helsinki\", \"NYC\"];\r\n\r\nif (MT.array_equals(original_cities, cities)){\r\n  return \"\u9700\u8981\u4fee\u6539 cities \u4e2d\u7684\u5143\u7d20\";\r\n}\r\n\r\nreturn true;","title":"Loops and arrays I","desc":"","hint":"","init_code":"var cities = [\"Melbourne\", \"Amman\", \"Helsinki\", \"NYC\"];\r\n\r\nfor (var i = 0; i < cities.length; i++) {\r\n    console.log(\"I would like to visit \" + cities[i]);\r\n}","code_reset":true},{"content":"1 Create an array called names filled with 5 names.\r\n\r\n2 Write a for loop that prints \"I know someone called \" followed by names[i]. Make sure there's a space between \"called\" and the name!\r\n\r\n3 Run your code and the five sentences should print out.","rule":"if (error) {\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\"\r\n}\r\n\r\ntry {\r\n  names;\r\n} catch(ReferenceError) {\r\n  return \"Did you define a variable called names?\";\r\n}\r\n\r\nif (!(names instanceof Array)) {\r\n  return \"It looks like names isn't an array.\";\r\n}\r\n\r\nif (names.length !== 5) {\r\n  return \"It looks like there aren't exactly five names in the names array.\";\r\n}\r\n\r\nfor (var i = 0; i < names.length; i++) {\r\n  if (!MT.printed(\"I know someone called \" + names[i])) {\r\n    return \"Your code should have printed 'I know someone called \" + names[i] + \"', but didn't. Make sure to type the 'I know someone called ' part exactly right, and don't forget the space between that phrase and the name!\";\r\n  }\r\n}\r\n\r\nif (MT.printed(\"I know someone called undefined\"))\r\n  return \"Whoops--it looks like you looped past the end of the array!\";\r\n\r\nreturn true;","title":"Loops and arrays II","desc":"","hint":"","init_code":"","code_reset":true},{"content":"Write a for loop that finds the largest element in array. Then log that element to the console. How do we do this?\r\n\r\n1 Write a for loop starting on line 4, with an if statement inside it.\r\n\r\n2 Compare the first element of the array to the variable largest. If this element is bigger than largest, make largest equal to this element.\r\n\r\n3 The loop will then start again. You will then compare the second element of the array to largest. And so on.\r\n\r\n4 You will only change the value of the variable if the current array element you are looking at is greater than the variable value.\r\n\r\n5 Your for loop will go through each array element and your variable will end up with the highest value in the array.","rule":"if (error)\r\n  return \"Your code doesn't look quite right. Check the Hint if you need help!\";\r\n\r\nif (code.match(/for\\s*\\(/) === null)\r\n  return \"Did you remember to add in your for loop?\";\r\n\r\nif (code.match(/if\\s*\\(/) === null && code.match(/\\?/) === null)\r\n  return \"Did you remember to add in your if statement?\";\r\n\r\n\r\nif (typeof largest === \"undefined\"){\r\n  return \"You did not create a variable largest\";\r\n}\r\n\r\nif (largest !== 89)\r\n  return \"It looks like the largest variable hasn't been set to the largest number in the array.\";\r\n\r\nreturn true;","title":"Loops and arrays III","desc":"","hint":"","init_code":"var array = [3, 6, 2, 56, 32, 5, 89, 32];\r\nvar largest = 0;\r\n// Write your code below!\r\n\r\n\r\n\r\n\r\n\r\nconsole.log(largest);","code_reset":true},{"content":"The best way to get better at programming is to use what you have learned to build something! Click Save & Submit Code to finish. ","rule":"return true","title":"Conclusion","desc":"","hint":"","init_code":"","code_reset":true}]}

来源

http://www.codecademy.com/zh/courses/javascript-beginner-en-XEDZA/0/1?curriculum_id=506324b3a7dffd00020bf661

章节名

第二章  Project: Search Text for Your Name

课件名

第一课 Project: Search Text for Your Name

json

{"title":"Project: Search Text for Your Name","desc":"","kind":"javascript","total_count":7,"steps":[{"content":"Some of this code will be new to you, but don't worry\u2014we'll go through it step-by-step. Ready? Click Save & Submit Code to begin!","rule":"return true","title":"What you'll be building","desc":"","hint":"","init_code":"text = \"Blah blah blah blah blah blah Eric \\\r\nblah blah blah Eric blah blah Eric blah blah \\\r\nblah blah blah blah blah Eric\";\r\n\r\nvar myName = \"Eric\";\r\nvar hits = [];\r\n\r\n// Look for \"E\" in the text\r\nfor(var i = 0; i < text.length; i++) {\r\n  if (text[i] == \"E\") {\r\n\t\t// If we find it, add characters up to\r\n\t\t// the length of my name to the array\r\n\t\tfor(var j = i; j < (myName.length + i); j++) {\r\n\t\t\thits.push(text[j]);\r\n\t\t}\r\n\t}\r\n}\r\n\r\nif (hits.length === 0) {\r\n\tconsole.log(\"Your name wasn't found!\");\r\n} else {\r\n\tconsole.log(hits);\r\n}","code_reset":true},{"content":"Declare and set the following three variables:\r\n\r\n1 text, and make it a string containing some text. Place your name in there a couple of times!\r\n\r\n2 myName, and make it a string containing just your name.\r\n\r\n3 hits, and make it an empty array.","rule":"\r\n\r\ntry {\r\n  text;\r\n}\r\ncatch(ReferenceError) {\r\n  return \"It looks like you haven't declared a variable called text!\";\r\n}\r\n\r\nif (typeof(text) !== 'string') {\r\n  return \"Did you set text equal to a string?\";\r\n}\r\n\r\ntry {\r\n  myName;\r\n}\r\ncatch(ReferenceError){\r\n  return \"It looks like you haven't declared a variable called myName!\";\r\n}\r\n\r\nif (typeof(myName) !== 'string') {\r\n  return \"Did you set myName equal to your name as a string?\";\r\n}\r\n\r\ntry {\r\n  hits;\r\n}\r\ncatch(ReferenceError) {\r\n  return \"It looks like you haven't declared a variable called hits!\";\r\n}\r\n\r\nif(!(hits instanceof(Array))){\r\n  return \"Did you remember to make your hits variable an array?\"\r\n}\r\n\r\nif (error) {\r\n  return \"You have an error in your code. Check the error message for more details\";\r\n}\r\nreturn true;","title":"Declare your variables","desc":"","hint":"","init_code":"","code_reset":true},{"content":"Below your existing code, create a for loop that starts at 0, continues until it reaches the end of text, and increments by 1 each time. (This means it will check each character in the string.) There's no need to write anything between the {}s of your loop just yet.\r\n\r\nCheck the Hint if you need help!","rule":"if(code.match('for') === null) {\r\n  return \"It looks like you didn't include a 'for' loop!\";\r\n}\r\n\r\nif(code.match('0') === null) {\r\n  return \"Does your 'for' loop start at 0?\";\r\n}\r\n\r\nif(code.match(/text\\.length/) === null) {\r\n  return \"Does your 'for' loop iterate until it hits text.length? Check the Hint if you need help!\";\r\n}\r\n\r\nif(code.match(/(?:\\+\\+|\\+=\\s*1|\\+\\s*1)/) === null) {\r\n  return \"Did you remember to increment your 'for' loop by 1 each time?\";\r\n}\r\n\r\nif(error instanceof(SyntaxError)) {\r\n  return \"Check your 'for' loop syntax! It doesn't look quite right. (Try the Hint if you need help.)\"\r\n}\r\n\r\nif (error) {\r\n  return \"It looks like you have an error in your code. Check the error message for more details.\";\r\n}\r\nreturn true;","title":"Your first \"for\" loop","desc":"","hint":"","init_code":"","code_reset":false},{"content":"Add your if statement in the body of your for loop. It should check to see whether the current letter is equal to the first letter of your name. (Capitalization counts!)\r\n\r\nThere's no need to put anything between the {}s of your if just yet.","rule":"if(error instanceof(SyntaxError)) {\r\n  return \"Your 'if' statement syntax doesn't look quite right. Check the Hint if you need help!\";\r\n}\r\n\r\nif (error) {\r\n  return \"Your code seems to have an error. See the error message for more details.\";\r\n}\r\n\r\nif(code.match('if') === null){\r\n  return \"Did you remember to include your 'if' statement?\";\r\n}\r\n\r\nif(code.match(/text\\[/) === null){\r\n  return \"Remember how to check the value of an element in an array? You simply do arrayName[i]. Check the Hint if you need help!\";\r\n}\r\n\r\nreturn true;","title":"Your \"if\" statement","desc":"","hint":"","init_code":"","code_reset":false},{"content":"Okay! Go ahead and add that second for loop inside the body of your if statement.","rule":"if(error instanceof(SyntaxError)) {\r\n  return \"Check your second 'for' loop syntax--it doesn't look quite right!\";\r\n}\r\n\r\nif(code.match(/(:?.+\\s*\\+\\s*myName\\.length|myName\\.length\\s*\\+\\s*.+)/) === null) {\r\n  return \"Careful: your second 'for' loop should stop when it reaches its current point in the string + myName.length.\";\r\n}\r\n\r\nif (hits.length === 0) {\r\n  return \"It looks like your second 'for' loop isn't pushing values to the hits array. Make sure it's working properly and that myName's text appears somewhere in the text variable.\";\r\n}\r\n\r\nif(typeof(hits[0]) !== \"string\") {\r\n  return \"Make sure you're pushing letters to the hits array!\";\r\n}\r\n\r\nif (error) {\r\n  return \"You seem to have an error in your code. Check out the error message in the console for more details.\";\r\n}\r\n\r\nreturn true;","title":"Your second \"for\" loop","desc":"","hint":"","init_code":"","code_reset":false},{"content":"Under your existing code (and outside all your loops!), set up an if/else statement. If you don't have any hits, log \"Your name wasn't found!\" to the console. Otherwise, log the hits array to the console.","rule":"var output = MT.prints;\r\n\r\nif(output.length === 0){\r\n  return \"Did you remember to log the result of your search to the console?\";\r\n}\r\n\r\nif (error) {\r\n  return \"There seems to be an error in your code. Check the console for more details.\";\r\n}\r\n\r\nreturn true;","title":"Log it!","desc":"","hint":"","init_code":"","code_reset":false},{"content":"In the meantime, click Save & Submit Code to finish this course and move on to more JavaScript goodness.","rule":"return true","title":"Victory!","desc":"","hint":"","init_code":"","code_reset":false}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment