Browse Source

more-exercises

master
Bart De Lepeleer 2 years ago
parent
commit
6eb4643d75

+ 10
- 0
more-exercises/solution-exercise-1.js View File

@@ -0,0 +1,10 @@
function isAbecedarian(word) {
for (let i = 0; i < word.length-1; i++) {
if (word[i]>word[i+1]) {
return false;
}
return true;
}
}

console.log(isAbecedarian('adempt'));

+ 40
- 0
more-exercises/solution-exercise-2.js View File

@@ -0,0 +1,40 @@
function isDoubloon(word) {
word = word.toLowerCase();
// store all charactercodes to an array
let arr = [];
for (let i = 0; i < word.length; i++) {
let index = word.charCodeAt(i);
if (arr[index] === undefined) {
arr[index] = 1
} else {
arr[index]++;
}
}
//look if count is 2 for each character
for (let i = 0;i < arr.length; i++) {
if (arr[i] !== undefined && arr[i] !== 2) {
return false;
}
}
return true
}

function isDoubloon2(word) {
word = word.toLowerCase();
for (let i = 0; i < word.length; i++) {
count = 0;
for (let j = 0; j < word.length; j++) {
if (word[j] === word[i]) {
count++;
}
}
if (count !== 2){
return false;
}
}
return true;
}

console.log(isDoubloon('Anna'));
console.log(isDoubloon2('Anna'));

+ 13
- 0
more-exercises/solution-exercise-3.js View File

@@ -0,0 +1,13 @@
function canSpell(word,tiles) {
tiles=tiles.split('');
for (let i=0; i < word.length; i++) {
let index = tiles.indexOf(word[i]);
if (index < 0) {
return false;
}
tiles[index] = null;
}
return true;
}

console.log(canSpell('jibb','quijibo'));

+ 11
- 0
more-exercises/solution-exercise-4.js View File

@@ -0,0 +1,11 @@
function isPalindrome (input) {
const regex = /[\W_]/g;
input = input.toLowerCase().replace(regex,'');
console.log(input);
if (input === input.split('').reverse().join('')) {
return true;
}
return false;
}

console.log(isPalindrome('0_0 (: /-\ :) 0–0'));

+ 12
- 0
more-exercises/solution-exercise-5.js View File

@@ -0,0 +1,12 @@
function isPalindrome(input) {
const regex = /[\W_]/g;
input = input.toLowerCase().replace(regex, '');
let len = input.length;
for (let i = 0; i < len/2; i++) {
if (input[i] !== input[len - 1 - i]) {
return false;
}
}
return true;
}
console.log(isPalindrome("A man, a plan, a canal. Panama"));

+ 16
- 0
more-exercises/solution-exercise-6.js View File

@@ -0,0 +1,16 @@
'use strict'

function makeLowerCase(input) {
let output = '';
for (let i = 0; i < input.length; i++) {
let charCode = input.charCodeAt(i);
if (charCode >= 65 && charCode <= 95 ) {
output += String.fromCharCode(charCode + 32);
} else {
output += String.fromCharCode(charCode);
}
}
return output;
}

console.log(makeLowerCase('JAVASCRIPT'));

+ 14
- 0
more-exercises/solution-exercise-7.js View File

@@ -0,0 +1,14 @@
function squareRoot(a){

guess = a/2;
improvedGuess = (guess + (a/guess))/2;
while (Math.abs(guess - improvedGuess) > 0.0001){
guess = improvedGuess;
improvedGuess = (guess + (a/guess))/2;
}
return improvedGuess;
}

console.log(squareRoot(9));



+ 26
- 0
more-exercises/solution-exercise-8.js View File

@@ -0,0 +1,26 @@
function getPrimes(n) {
let sieve = [];
for (let i = 0; i <= n; i++){
sieve.push(true);
}

for (let i = 2; i <=n; i++) {
if (sieve[i]) {
for (let j = i * 2; j <= n; j += i) {
sieve[j] = false;
}
}
}

let primes = [];
for (let i = 2; i <=n; i++) {
if (sieve[i]) {
primes.push(i);
}
}
return primes;

}

console.log(getPrimes(100));


+ 25
- 0
more-exercises/solution-exercise-9.js View File

@@ -0,0 +1,25 @@
/*
BASE LOGIC: https://sebhastian.com/bubble-sort-javascript/

Compacted the code by use of :
- https://www.samanthaming.com/tidbits/8-swap-variables-with-destructuring/
- https://flexiple.com/javascript-pass-by-reference-or-value/

*/

function bubbleSort(arr) {
let sorted = false;
while (!sorted) {
sorted = true;
for (i = 0; i < arr.length; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i],arr[i + 1]] = [arr[i+1],arr[i]];
sorted = false;
}
}
}
}

test = [5,7,99,1,3,796,-1,0];
bubbleSort(test)
console.log(test);

Loading…
Cancel
Save