-
-
Notifications
You must be signed in to change notification settings - Fork 279
Mickey Haile JavaScript-Core-1-Coursework-Week3 #169
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,25 +12,26 @@ | |
| // Example 1 | ||
| let a; | ||
| console.log(a); | ||
|
|
||
| /* a is not defined */ | ||
|
|
||
| // Example 2 | ||
| function sayHello() { | ||
| let message = "Hello"; | ||
| } | ||
|
|
||
| /* there is no return */ | ||
| let hello = sayHello(); | ||
| console.log(hello); | ||
|
|
||
| /* hello is a variable not a function */ | ||
|
|
||
| // Example 3 | ||
| function sayHelloToUser(user) { | ||
| console.log(`Hello ${user}`); | ||
| } | ||
|
|
||
| sayHelloToUser(); | ||
|
|
||
| /*needs a parameter*/ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. technically, it's an |
||
|
|
||
| // Example 4 | ||
| let arr = [1,2,3]; | ||
| console.log(arr[3]); | ||
| /* there is no index 3 */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,14 @@ | |
|
|
||
| function evenNumbers(n) { | ||
| // TODO | ||
| i = 0; | ||
| let even = []; | ||
| while (i < 2 * n){ | ||
| even.push(i);} | ||
| i += 2 | ||
|
Comment on lines
+12
to
+14
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think your syntax is off here; you're incrementing Also, for checking even numbers, the modulo |
||
| return even.toString() | ||
| } | ||
| console.log(evenNumbers(3)); | ||
|
|
||
| evenNumbers(3); // should output 0,2,4 | ||
| evenNumbers(0); // should output nothing | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,9 @@ const AGES = [ | |
| ]; | ||
|
|
||
| // TODO - Write for loop code here | ||
|
|
||
| for (let i=0;i<WRITERS.length-1;i++){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as you're starting from 0 and just using a |
||
| console.log ( WRITERS[i]+" is "+AGES[i]+" years old") | ||
| } | ||
| /* | ||
| The output should look something like this: | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,12 @@ | |
| Implement the function below, which will return a new array containing only article titles which will fit. | ||
| */ | ||
| function potentialHeadlines(allArticleTitles) { | ||
| // TODO | ||
| let newArr = []; | ||
| for (let title of allArticleTitles) { | ||
| if (title.length < 65) newArr.push(title); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to keep things readable, I'd advise always using |
||
| } | ||
| return newArr; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -15,6 +20,25 @@ function potentialHeadlines(allArticleTitles) { | |
| */ | ||
| function titleWithFewestWords(allArticleTitles) { | ||
| // TODO | ||
| for (var i = 0; i < allArticleTitles.length; i++) { | ||
| // Last i elements are already in place | ||
| for (var j = 0; j < allArticleTitles.length - i - 1; j++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar to above, you don't need to |
||
| // Checking if the item at present iteration | ||
| // is greater than the next iteration | ||
| if ( | ||
| allArticleTitles[j].split(" ").length > | ||
| allArticleTitles[j + 1].split(" ").length | ||
| ) { | ||
| // If the condition is true then swap them | ||
| var temp = allArticleTitles[j]; | ||
| allArticleTitles[j] = allArticleTitles[j + 1]; | ||
| allArticleTitles[j + 1] = temp; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return allArticleTitles[0]; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -24,6 +48,15 @@ function titleWithFewestWords(allArticleTitles) { | |
| */ | ||
| function headlinesWithNumbers(allArticleTitles) { | ||
| // TODO | ||
| let newArr = []; | ||
| for (let title of allArticleTitles) { | ||
| if (/[0-9]/.test(title) === true) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could simplify this a little with a shorthand character class in regex |
||
| newArr.push(title); | ||
| } | ||
| } | ||
|
|
||
| return newArr; | ||
|
|
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -32,6 +65,13 @@ function headlinesWithNumbers(allArticleTitles) { | |
| */ | ||
| function averageNumberOfCharacters(allArticleTitles) { | ||
| // TODO | ||
| let sum = 0; | ||
|
|
||
| for (let title of allArticleTitles) { | ||
| sum += title.length; | ||
| } | ||
|
|
||
| return Math.round(sum / allArticleTitles.length); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if articleTitle.length is 0? |
||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In javascript, functions can be assigned to variables e.g.
So the reason that it is undefined is because you assign
helloto the result of callingsayHello(); assayHello()doesn't return anything,helloisundefined.