Search for errors in the with conditions in JavaScript
In the following tasks, some programmer has written code and may have made mistakes in it. You should check if the code does what is described. If the code doesn't work correctly, you need to fix the errors.
The code should check the sum of the numbers:
let num1 = 1;
let num2 = 2;
if (num1 + num2 === 3) {
console.log('+++');
} else {
console.log('---');
}
The code should check the sum of the numbers:
let num1 = '1';
let num2 = '2';
if (num1 + num2 === 3) {
console.log('+++');
} else {
console.log('---');
}
The code should check the sum of the numbers:
let num1 = '1';
let num2 = '2';
if (Number(num1 + num2) === 3) {
console.log('+++');
} else {
console.log('---');
}
The code should check the first digit of the number:
let num = 123;
if (num[0] === 1) {
console.log('+++');
} else {
console.log('---');
}
The code should check the first digit of the number:
let num = 123;
if (String(num[0]) === 1) {
console.log('+++');
} else {
console.log('---');
}
The code should check the first digit of the number:
let num = 123;
if (String(num)[0] === 1) {
console.log('+++');
} else {
console.log('---');
}
The code should check the first digit of the number:
let num = 123;
let first = String(num)[0];
if (first === 1) {
console.log('+++');
} else {
console.log('---');
}
The code should check that there are exactly two digits in the number:
let num = 12;
if (num.length === 2) {
console.log('+++');
} else {
console.log('---');
}
The code should check that there are exactly two digits in the number:
let num = 12;
let str = Number(str);
if (str.length === 2) {
console.log('+++');
} else {
console.log('---');
}
The code should check that there are exactly two digits in the number:
let num = 12;
if (String(num.length) === 2) {
console.log('+++');
} else {
console.log('---');
}
The code should check that there are exactly two digits in the number:
let num = 12;
if (String(num).length === String(2)) {
console.log('+++');
} else {
console.log('---');
}
The code should check that there are exactly two digits in the number:
let num = 12;
if (String(num).lenght === 2) {
console.log('+++');
} else {
console.log('---');
}
The code should check if the sum of the first three digits of a number is equal to the sum of the second three numbers:
let num = '123033'; // put it in quotes to refer to numbers
let sum1 = num[0] + num[1] + num[2];
let sum2 = num[3] + num[4] + num[5];
if (sum1 == sum2) {
console.log('sums are equal');
} else {
console.log('sums are not equal');
}