Każde ćwiczenie zawiera krótką instrukcję. Kliknij "Pokaż rozwiązanie", aby zobaczyć kod.
function checkType() {
let value = document.getElementById("input1").value;
document.getElementById("wynik1").innerHTML = typeof value;
}
function convertToNumber() {
let value = document.getElementById("input2").value;
let number = Number(value);
document.getElementById("wynik2").innerHTML =
number + " (typ: " + typeof number + ")";
}
function convertToString() {
let num = 42;
let text = String(num);
document.getElementById("wynik3").innerHTML =
text + " (typ: " + typeof text + ")";
}
function convertToBoolean() {
let value = document.getElementById("input4").value;
let boolValue = Boolean(value);
document.getElementById("wynik4").innerHTML =
boolValue + " (typ: " + typeof boolValue + ")";
}
Uwaga: typeof null zwraca "object".
function checkNull() {
let value = null;
document.getElementById("wynik5").innerHTML = (value === null);
}
function checkUndefined() {
let value;
document.getElementById("wynik6").innerHTML = (value === undefined);
}
function checkIfNumber() {
let value = document.getElementById("input7").value;
let num = Number(value);
let isNumber = !Number.isNaN(num);
document.getElementById("wynik7").innerHTML =
isNumber + " (po konwersji: " + num + ")";
}
function checkIfObject() {
let raw = document.getElementById("input8").value;
try {
let value = JSON.parse(raw);
let isPlainObject =
typeof value === "object" &&
value !== null &&
!Array.isArray(value);
document.getElementById("wynik8").innerHTML =
isPlainObject + " (typ: " + typeof value + ")";
} catch (e) {
document.getElementById("wynik8").innerHTML = "Niepoprawny JSON";
}
}
function checkIfArray() {
let raw = document.getElementById("input9").value;
try {
let value = JSON.parse(raw);
let isArray = Array.isArray(value);
document.getElementById("wynik9").innerHTML =
isArray + " (typ: " + typeof value + ")";
} catch (e) {
document.getElementById("wynik9").innerHTML = "Niepoprawny JSON";
}
}
function checkStringLength() {
let text = document.getElementById("input10").value;
document.getElementById("wynik10").innerHTML = "Długość: " + text.length;
}
function checkSymbol() {
let desc = document.getElementById("input11").value || "id";
let s = Symbol(desc);
document.getElementById("wynik11").innerHTML =
String(s) + " (typ: " + typeof s + ")";
}
function checkBigint() {
let raw = document.getElementById("input12").value;
try {
let big = BigInt(raw);
document.getElementById("wynik12").innerHTML =
big + " (typ: " + typeof big + ")";
} catch (e) {
document.getElementById("wynik12").innerHTML =
"Nie można skonwertować na BigInt";
}
}
function checkFunctionType() {
function greet(name) {
return "Cześć, " + name + "!";
}
document.getElementById("wynik13").innerHTML =
"typeof greet: " + (typeof greet) +
", wynik greet('Jan'): " + greet("Jan");
}