// As funções abaixo estão sintaticamente corretas, mas não gerando os resultados corretos.
// Observe os testes e veja como você pode corrigi-los.
function mood() {
let isHappy = true;
if (isHappy) {
return "I am happy";
} else {
return "I am not happy";
}
}
function greaterThan10() {
let num = 10;
let isBigEnough;
if (isBigEnough) {
return "num is greater than or equal to 10";
} else {
return "num is not big enough";
}
}
function sortArray() {
let letters = ["a", "n", "c", "e", "z", "f"];
let sortedLetters;
return sortedLetters;
}
function first5() {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
let sliced;
return sliced;
}
function get3rdIndex(arr) {
let index = 3;
let element;
return element;
}
/* ======= TESTS - DO NOT MODIFY ===== */
function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
console.log(`${test_name}: ${status}`);
}
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
test("mood function works", mood() === "I am not happy");
test(
"greaterThanTen function works",
greaterThan10() === "num is greater than or equal to 10"
);
test(
"sortArray function works",
arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"])
);
test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5]));
test(
"get3rdIndex function works - case 1",
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) ===
"strawberry"
);
test(
"get3rdIndex function works - case 2",
get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18
);
/ *
Escreva uma função que:
- recebe uma série de strings como entrada
- remove todos os espaços no início ou no final das strings
- remove quaisquer barras (/) nas strings
- torna a string toda em minúsculas
* /
function tidyUpString(strArr) {}
/ *
Conclua a função para verificar se a variável `num` atende aos seguintes requisitos:
- é um número
- é mesmo
- é menor ou igual a 100
Dica: use operadores lógicos
* /
function validate(num) {}
/ *
Escreva uma função que remove um elemento de uma matriz
A função deve:
- NÃO mude a matriz original
- retorna uma nova matriz com o item removido
- remove o item no índice especificado
* /
function remove(arr, index) {
return; // complete this statement
}
/ *
Escreva uma função que:
- pega uma matriz de números como entrada
- retorna uma matriz de strings formatadas como porcentagens (por exemplo, 10 => "10%")
- os números devem ser arredondados para 2 casas decimais
- números maiores de 100 devem ser substituídos por 100
* /
function formatPercentage(arr) {
}
/* ======= TESTS - DO NOT MODIFY ===== */
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
console.log(`${test_name}: ${status}`);
}
test(
"tidyUpString function works - case 1",
arraysEqual(tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "]), [
"daniel",
"irina",
"gordon",
"ashleigh"
])
);
test(
"tidyUpString function works - case 2",
arraysEqual(
tidyUpString([" /Sanyia ", " Michael ", "AnTHonY ", " Tim "]),
["sanyia", "michael", "anthony", "tim"]
)
);
test("validate function works - case 1", validate(10) === true);
test("validate function works - case 2", validate(18) === true);
test("validate function works - case 3", validate(17) === false);
test("validate function works - case 4", validate("Ten") === false);
test("validate function works - case 5", validate(108) === false);
test(
"remove function works - case 1",
arraysEqual(remove([10, 293, 292, 176, 29], 3), [10, 293, 292, 29])
);
test(
"remove function works - case 1",
arraysEqual(remove(["a", "b", "c", "d", "e", "f", "g"], 6), [
"a",
"b",
"c",
"d",
"e",
"f"
])
);
test(
"formatPercentage function works - case 1",
arraysEqual(formatPercentage([23, 18, 187.2, 0.372]), [
"23%",
"18%",
"100%",
"0.37%"
])
);