Skip to content

Instantly share code, notes, and snippets.

@rogerwelin
Last active September 15, 2022 19:18
Show Gist options
  • Save rogerwelin/4215e4194be2e3e21594b87eabf33df0 to your computer and use it in GitHub Desktop.
Save rogerwelin/4215e4194be2e3e21594b87eabf33df0 to your computer and use it in GitHub Desktop.
js exercises
  1. Write a function that takes an array of numbers and returns an array of strings
function stringItUp(arr){
  // your code here
}

console.log(stringItUp([2, 5, 100])); // ["2", "5", "100"]
  1. Write a function that returns a passed string with letters in alphabetical order. Example
function alphabeticalOrder(word) {
  // your code here
}

console.log( alphabeticalOrder("webmaster") ) // should return  abeemrstw
  1. Write a function that accepts a string as a parameter and find the longest word within the string. Example:
function findLongestWord(word) {
  // your code here
}

console.log( findLongestWord("It was a magical evening in hogwarts") ) // should return hogwarts
  1. Write a function to convert an amount to coins. Example:
function amountToCoins(amount, count_arr) {
  // your code here
}

console.log( amountToCoins(46, [25, 10, 5, 2, 1)
// 46 is the amount. and 25, 10, 5, 2, 1 are coins. Output : 25, 10, 10, 1
  1. merge two arrays and remove duplicates
function mergeArray(arr1, arr2) {
  // your code
}

let array1 = [1, 2, 3];
let array2 = [2, 30, 1];
console.log(merge_array(array1, array2));
[3, 2, 30, 1]
  1. Write a function that counts the occurrance of a specific word or number in array
function countOccurrance(word_to_look_for, array) {
  // your code
}

console.log( countOccurrance(2, [1, 2, 3, 4, 5, 2, 2]) ) // should return 3
  1. Eliminate all odd numbers and return even numbers
function onlyEven(arr) {
// your code
}

console.log( onlyEven([1, 2, 3, 3, 6, 6, 7, 8]) ) // should return [2, 6, 6, 8]
  1. Build a calculator app. Write a function to do the calculation. You can get the user input by using the following code:
let input = process.argv

you then run your program like so:

$ node calculator.js 4 + 1
5
$ node calculator.js 10 - 2
8
$ node calculator.js 8 / 2
4
// multiply character * is special, we need to write it "*"
$ node calculator.js 8 "*" 2
16
  1. Make our calculator app more sturdy by validating user input. Validate:
  • that the input array is of expected size
  • that we only get numbers from the user
  • that we only get the combination of + - / *

If user supplies wrong information exit the program and let the user know what he/she did wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment