0
0
Javascriptprogramming~10 mins

Unary operators in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to increase the value of count by 1 using a unary operator.

Javascript
let count = 5;
count[1];
console.log(count);
Drag options to blanks, or click blank then click option'
A-
B++
C+=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the value.
Using '+=' which is not a unary operator.
Using '-' which subtracts but is not unary increment.
2fill in blank
medium

Complete the code to convert the string numStr to a number using a unary operator.

Javascript
let numStr = "42";
let num = [1]numStr;
console.log(typeof num);
Drag options to blanks, or click blank then click option'
A+
BNumber
CparseInt
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseInt which is a function, not a unary operator.
Using Number which is a function, not a unary operator.
Using '-' which negates the number but does not convert string to number.
3fill in blank
hard

Fix the error in the code by choosing the correct unary operator to negate the value of value.

Javascript
let value = 10;
let negated = [1]value;
console.log(negated);
Drag options to blanks, or click blank then click option'
A-
B+
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' which increments the value.
Using '+' which does not change the sign.
Using '--' which decrements the value.
4fill in blank
hard

Fill both blanks to create an object flags where keys are numbers and values are their negations using unary operators.

Javascript
const numbers = [1, 2, 3];
const flags = {};
for (const num of numbers) {
  flags[[1]] = [2];
}
console.log(flags);
Drag options to blanks, or click blank then click option'
Anum
B-num
Cnum.toString()
Dnum * -1
Attempts:
3 left
💡 Hint
Common Mistakes
Using number directly as key without converting to string.
Using multiplication instead of unary minus for negation.
Using 'num' as value which is not negated.
5fill in blank
hard

Fill both blanks to create a new array results where each element is the negation of the numeric conversion of strings in arr.

Javascript
const arr = ["5", "-3", "10"];
const results = arr.map(item => [1][2]item);
console.log(results);
Drag options to blanks, or click blank then click option'
A-
B+
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using logical NOT ! which converts to boolean.
Not converting string to number before negation.
Placing operators incorrectly causing syntax errors.