generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bac35dc
commit 02ac931
Showing
11 changed files
with
151 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function calculate(str) { | ||
//your code here... | ||
str = `+${str}`; | ||
str = str.replaceAll("plus", "+"); | ||
str = str.replaceAll("minus", "-"); | ||
let num; | ||
while (str.length > 0) { | ||
let lastMinus = str.lastIndexOf("-"); | ||
let lastPlus = str.lastIndexOf("+"); | ||
|
||
if (lastPlus > lastMinus) { | ||
num += Number(str.slice(lastPlus + 1)); | ||
str = str.slice(0, lastPlus); | ||
} else if (lastMinus > lastPlus) { | ||
num -= Number(str.slice(lastMinus + 1)); | ||
str = str.slice(0, lastMinus); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return num.toString(); | ||
} | ||
console.log(calculate("1plus2plus3plus4")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/* Answer | ||
there's a syntax error in the function definition. Function parameters should be variables, not specific values or numbers. In this case, 3 is being used as a parameter, which isn't allowed in JavaScript function declarations. | ||
// Predict and explain first... | ||
// this function should square any number but instead we're going to get an error | ||
// what is happening? How can we fix it? | ||
|
||
function square(3) { | ||
return num * num; | ||
To fix this, we should declare the function with a parameter representing the number we want to square. */ | ||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
|
||
console.log(square(4)); | ||
console.log(square(3)); | ||
console.log(square(5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters