TwilioQuest JavaScript Questions

I’ve found the game “TwilioQuest”, and I’m on the JavaScript research station.

First question: I’m currently on the challenge of extending the bridge to rescue the botanist. The game wants me to type up a JavaScript program that will type the message “Extending the Bridge” if we type in the word “Extend”.

I embellished it with an else-if condition. Here’s the text:

const argumentValue = process.argv[2];

if (argumentValue = ‘Extend’) {
console.log(‘Extending bridge!’);
else if
console.log(‘Command not recognized; bridge inactive.’)
};


When I attempt to run the Javascript file via either Command Prompt or PowerShell, with a string other than “extend” to test if the else statement works, I get this error message:

PS C:\JavaScript\My_JS_Code> node northBridgeControl Hello
C:\JavaScript\My_JS_Code\northBridgeControl.js:5
else if
^^^^

SyntaxError: Unexpected token ‘else’
←[90m at Object.compileFunction (node:vm:352:18)←[39m
←[90m at wrapSafe (node:internal/modules/cjs/loader:1025:15)←[39m
←[90m at Module._compile (node:internal/modules/cjs/loader:1059:27)←[39m
←[90m at Object.Module._extensions…js (node:internal/modules/cjs/loader:1147:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:975:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m

Obviously I’m curious as to how to tweak, or rewrite, my code so that it shows either message depending on whether or not one types in “Extend”.

I’d also welcome a 411 on how to read the specific error messages I got and how I can apply these to understanding JavaScript in general. That’s peanuts next to fixing the code, granted. In part because these seem to be about particular node files anyway.

Your braces need to agree.

By “braces” I assume you mean parentheses and/or brackets.

Does this mean I should change the first line to

if {argumentValue='Extend}{

?

By braces I mean braces. Changing from parentheses to braces (or randomly changing any braces/brackets/parentheses) away from syntax shown in examples rarely helps.

Your braces need to be balanced. If you put an opening one at the head of an if statement code block, then you need to put a closing one at the end. That is what ‘balanced’ means. Braces need to come in complete pairs in the correct places.

I’m also guessing you didn’t mean to use an assignment operator here.

if (argumentValue = 'Extend')

More likely an equality operator === or == is what you want.

if (argumentValue === 'Extend')


fCC: introducing-else-if-statements

2 Likes

Aye, I have made the correction to the operator.

The bracket-balancing/placement/agreement thing is still throwing me, so I just simplified the line to:

const argumentValue = process.argv[2];

if (argumentValue === ‘EXTEND’) console.log(‘Extending bridge!’);

I still receive a committee of error lines.

What’s the rest? Please show all your code

2 Likes

Do you have one opening brace and once closing brace in each place where you use a brace?

if (cond) {
  // I opened a brace so I have to close it
} else if (other cond)
  // no opening brace, so no closing brace

It helps if you post your full code when you update something. Partial code is partial information.

1 Like

What errors are you getting? They are usually fairly good at telling you what is wrong so you should learn to read them.

Post the error log.

2 Likes

The challenge is a very simple one, so in each comment shared here I am sharing all of the code.

I have changed it now to

const argumentValue = process.argv[2];

if (argumentValue === ‘EXTEND’) {
console.log(‘Extending bridge!’);
}

This yields a recognizable suite of error messages. Opening choice of mention this time is “SyntaxError: Invalid or unexpected token”.

Aye, very well. Here’s the yield from my newest try.

PS C:\JavaScript\My_JS_Code> node northBridgeControl.js EXTEND
C:\JavaScript\My_JS_Code\northBridgeControl.js:3
if (argumentValue === ‘EXTEND’) {

SyntaxError: Invalid or unexpected token
←[90m at Object.compileFunction (node:vm:352:18)←[39m
←[90m at wrapSafe (node:internal/modules/cjs/loader:1025:15)←[39m
←[90m at Module._compile (node:internal/modules/cjs/loader:1059:27)←[39m
←[90m at Object.Module._extensions…js (node:internal/modules/cjs/loader:1147:10)←[39m
←[90m at Module.load (node:internal/modules/cjs/loader:975:32)←[39m
←[90m at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m at node:internal/main/run_main_module:17:47←[39m

Is that absolutely all of your code?

Those three lines alone look OK to me.

1 Like

Is that all of it?

You do have some odd quotes in your code. At first I just assumed it was the forum that did that but make sure you use single '' or "" double quotes.

2 Likes

Update: I have finally found the solution.

Via input from a good Samaritan, I have learned that JavaScript is particular about the shape of the apostrophes/‘half-quotes’ used. I had a vertical half-quote at the end of “EXTEND”, but the half-quote at the beginning was curved. I changed that first half-quote to a simple vertical apostrophe, and the code works as intended now.

JeremyLT, if you’re reading this, you may relish smugly: You were right. A pair was mismatched and needed to be made identical. In the case of the final repairing touch, though, it was not the braces.

To clarify, your code had 3 errors that were all causing trouble:

  1. You were using = instead of ===

  2. You were using ‘’ instead of ''

  3. You had mismatched {}s

These sorts of small details are very important.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.