All quizzesEasy
Pure Functions & Immutability — Series 3
Preview — 3 of 10 questions
Given these four functions, which one is pure?
javascript
let total = 0;
const a = (n) => n * 2;
const b = (n) => (total += n);
const c = (n) => { console.log(n); return n; };
const d = () => Math.random();Aa
Bb
Cc
Dd
This function has no side effects, yet it is still impure. Why?
javascript
function greet(name) {
return `Hello ${name}, it is ${new Date().getFullYear()}`;
}AIt is pure — it never modifies anything outside itself.
BTemplate literals are impure because string building allocates memory.
CIt reads external state (the clock), so the same input can produce different output over time.
DIt is impure because it takes a parameter instead of reading a constant.
What is logged?
javascript
const base = Object.freeze([1, 2]);
const extended = base.concat(3);
console.log(base.length, extended.length, Object.isFrozen(extended));A3 3 true
B2 3 false
C2 2 true
DTypeError: Cannot add property 2, object is not extensible
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.