Lesson 6 of 14

Lesson 06 — Math and string/array helpers for QA automation

Title: Rounding, Bounds, Random Data, String Search, and Queue Operations

Description: Built-in JavaScript methods are used for common test tasks like rounding numbers, finding minimum and maximum values, generating random test data, normalizing strings, splitting text into tokens, and working with arrays as simple queues or stacks. This includes methods like Math.ceil, Math.floor, Math.min, Math.max, string methods like toLowerCase() and trim(), and array operations like push, pop, shift, and unshift.

Why it matters for QA: Test assertions often depend on calculations like SLA limits, checking text inside logs or error messages, working with lists of values (like selectors or tokens), and simulating queues. Using built-in methods correctly helps avoid extra dependencies and keeps test code simple and reliable.


1. Powers and roots

Prefer ** for integer powers; Math.pow still appears in older codebases.

javascriptjavascript
console.log(2 ** 10); // 1024
console.log(Math.pow(3, 3)); // 27

console.log(Math.sqrt(49)); // 7

2. Rounding: round, ceil, floor, and toFixed

javascriptjavascript
let ms = 87.6;
console.log(Math.round(ms)); // 88
console.log(Math.ceil(ms));  // 88
console.log(Math.floor(ms)); // 87

let ratio = 0.333333;
console.log(ratio.toFixed(2)); // "0.33" — string, not number
console.log(Number(ratio.toFixed(2))); // 0.33

Treat toFixed as string formatting suitable for tolerant display assertions;

3. Math.max and Math.min

javascriptjavascript
let durations = [120, 340, 90, 410];
console.log(Math.max(...durations)); // 410
console.log(Math.min(...durations)); // 90

console.log(Math.max(5, 2, 9)); // 9

4. Math.random

Returns [0, 1).

javascriptjavascript
let suffix = Math.floor(Math.random() * 10_000);
console.log("run-" + suffix);

// random index in array
let tags = ["smoke", "api", "ui"];
let pick = tags[Math.floor(Math.random() * tags.length)];
console.log(pick);

Note: Math.random is not CSPRNG-grade—adequate for test entropy only; never for secrets, signing material, or credential derivation.


5. String case

javascriptjavascript
let env = "StAgInG";
console.log(env.toLowerCase()); // "staging"
console.log(env.toUpperCase()); // "STAGING"

Useful before comparing user input or headers case-insensitively.


6. slice (substring by index)

Does not mutate the string. End index is exclusive.

javascriptjavascript
let url = "https://api.example.com/v2/items";
console.log(url.slice(0, 5));   // "https"
console.log(url.slice(-5));     // "items"

7. Search helpers

javascriptjavascript
let msg = "AssertionError: locator not visible";
console.log(msg.includes("visible"));     // true
console.log(msg.startsWith("Assert"));    // true
console.log(msg.endsWith("visible"));     // true
console.log(msg.indexOf("locator"));      // index or -1

8. split and join

javascriptjavascript
let csv = "one,two,three";
let parts = csv.split(",");
console.log(parts);

let joined = ["2026", "05", "03"].join("-");
console.log(joined); // "2026-05-03"

9. Array ends: push, pop, shift, unshift

All mutate the array in place.

javascriptjavascript
let queue = [];
queue.push("first", "second");
console.log(queue.pop());   // "second" — from end
queue.push("third");
console.log(queue.shift()); // "first" — from start

queue.unshift("priority");
console.log(queue); // ["priority", "third"]
MethodEndEffect
pushrightadd one+
poprightremove last
unshiftleftadd one+
shiftleftremove first

Official docs


Quick recap

NeedAPI / pattern
Integer powerbase ** exp
Even roundingMath.round
Always up/downMath.ceil / Math.floor
Pretty decimaltoFixed(n) then Number(...) if needed
Substring checkincludes / startsWith / endsWith

Suggested exercises

  1. Given let latency = 149.9, store SLA breach as boolean: latency > 100 after rounding up to whole ms.
  2. Parse "GET /api/orders HTTP/1.1" into method and path using split.

Homework

Short tasks (about 10–15 minutes). Click a task title to reveal the prompt.

Task 1: SLA with ceil

Let latencyMs = 99.1. Define const isBreach = ... as true when the value rounded up to a whole millisecond is strictly greater than 99. Log isBreach.

Task 2: method and path from a log line

From "POST /api/v1/login HTTP/1.1", use split and array indices to log the HTTP method and the path separately (ignore HTTP/1.1).

Task 3: mini queue

Start with []. Call push three times, then shift twice, then push once more. Log the final array and explain the order in one sentence.

Task 4: soft match on an error

Given let msg = "TimeoutError: locator not found", log whether the message includes("locator") and whether it startsWith("Error").

Task 5: case-insensitive tag

Compare let a = "SMOKE" and let b = "smoke" as equal after normalizing both with toLowerCase(). Log the boolean result.

Task 6: max duration

Given let ms = [120, 340, 90], log Math.max(...ms).

Task 7: join a date

Log ["2026", "05", "04"].join("-") and confirm the output looks like an ISO-style date fragment.

Task 8: extract file extension

Create let file = "report.final.json". Split the string by "." and print the file extension(json) using the last item from the array, without directly writing the index number.

Task 9: trim user input

Create let raw = " smoke test ". First, print the length of the original string. Then print the length after using trim(). In one short sentence, explain why removing extra spaces is helpful before checking or validating user input.