JSFiddle - React, Tailwind, and code Playground

by Vasilii Chugunov

JavaScript

// 1. QUESTION
console.log(1.1 - 0.2)
// console output
// 0.9000000000000001

/* ANSWER
// We can get the correct result using toFixed
console.log(+(1.1 - 0.2).toFixed(1))
*/


// 2 QUESTION
let sampleArr = [1, 2, 3, 4, 5];
let modifiedArr = addArrLength(sampleArr);
function addArrLength(arr) {
  arr.push(arr.length);
  return arr;
}

console.log(sampleArr); // changed: [1, 2, 3, 4, 5]
console.log(modifiedArr);

/* ANSWER
function addArrLength(arr) {
  return [...arr, arr.length];
}
*/


// 3 QUESTION
// Correct the code without changing the object sample.
let user = {
  name: "John",
  logName() {
    console.log(`My name is ${this.name}!`);
  }
};
setTimeout(user.logName, 1000);

/* Answer
// using wrapper
setTimeout(function() {
  user.logName(); // My name is John!
}, 1000);

// using bind
let logName = user.logName.bind(user);
setTimeout(logName, 1000);
*/


// 4 QUESTION
var num = prompt('Type the number');
var res1 = (num * 10) + 2;
alert(res1);

var res2 = (num + 2) * 10;
alert(res2);

/* ANSWER
var num = parseInt(prompt('Type the number'));
*/


// 5 QUESTION
var niсkname = 'John';
console.log(`Hello, ${nickname}!`); // ReferenceError: nickname is not defined

/* ANSWER
need to change letter "c" in nickname variable (another encoding)
*/